Compare commits

...

2 Commits

Author SHA1 Message Date
fc72fd123d add PrintJson 2025-08-22 12:15:55 +08:00
63a4653eb2 add CopyFile 2025-08-21 10:59:18 +08:00
2 changed files with 40 additions and 1 deletions

View File

@@ -1,6 +1,10 @@
package utils
import (
"bytes"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
)
@@ -12,7 +16,7 @@ func If(condition bool, trueValue, falseValue interface{}) interface{} {
return falseValue
}
//如果首字母是小写字母, 则变换为大写字母
// 如果首字母是小写字母, 则变换为大写字母
func FirstToUpper(str string) string {
if str == "" {
return ""
@@ -33,3 +37,11 @@ func ParseParams(in map[string]string) map[string]interface{} {
}
return out
}
func PrintJson(v any) {
jsonBy, _ := json.Marshal(v)
var out bytes.Buffer
json.Indent(&out, jsonBy, "", "\t")
out.WriteTo(os.Stdout)
fmt.Printf("\n")
}

View File

@@ -2,6 +2,7 @@ package utils
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
@@ -21,6 +22,32 @@ func StringToFile(path, content string) error {
return nil
}
// 文件复制
func CopyFile(src, dst string) (int64, error) {
sourceFileStat, err := os.Stat(src)
if err != nil {
return 0, err
}
if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
}
source, err := os.Open(src)
if err != nil {
return 0, err
}
defer source.Close()
destination, err := os.Create(dst)
if err != nil {
return 0, err
}
defer destination.Close()
nBytes, err := io.Copy(destination, source)
return nBytes, err
}
// 递归遍历文件夹
// rootDir: 文件夹根目录
// s: 存储文件名的切片