-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
48 lines (40 loc) · 985 Bytes
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package kakaxi
import (
"fmt"
"os"
"path"
"strconv"
"strings"
"unsafe"
"github.com/shopspring/decimal"
)
func FormatBytes(size float64) string {
units := []string{" B", " KB", " MB", " GB", " TB"}
i := 0
for i = 0; size >= 1024 && i < 4; i++ {
res := decimal.NewFromFloat(size).Div(decimal.NewFromInt(1024))
val, _ := res.Float64()
size = val
}
result := strings.ReplaceAll(fmt.Sprintf("%.2f", FloatRound(size, 2)), "0000", "") + units[i]
return result
}
func FloatRound(f float64, n int) float64 {
format := "%." + strconv.Itoa(n) + "f"
res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
return res
}
func FileExist(savePath string) bool {
_ = os.MkdirAll(path.Dir(savePath), 0777)
//判断文件是否存在
if _, err := os.Stat(savePath); err == nil {
return true
}
return false
}
func Bytes2string(bs []byte) string {
return *(*string)(unsafe.Pointer(&bs))
}
func String2Bytes(s string) []byte {
return *(*[]byte)(unsafe.Pointer(&s))
}