mirror of
https://github.com/oarkflow/mq.git
synced 2025-09-27 20:32:15 +08:00
32 lines
576 B
Go
32 lines
576 B
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"unsafe"
|
|
)
|
|
|
|
func ToByte(s string) []byte {
|
|
p := unsafe.StringData(s)
|
|
b := unsafe.Slice(p, len(s))
|
|
return b
|
|
}
|
|
|
|
func FromByte(b []byte) string {
|
|
p := unsafe.SliceData(b)
|
|
return unsafe.String(p, len(b))
|
|
}
|
|
|
|
func FormatBytes(bytes int64) string {
|
|
units := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
|
|
if bytes == 0 {
|
|
return fmt.Sprintf("0 B")
|
|
}
|
|
size := float64(bytes)
|
|
unitIndex := 0
|
|
for size >= 1024 && unitIndex < len(units)-1 {
|
|
size /= 1024
|
|
unitIndex++
|
|
}
|
|
return fmt.Sprintf("%.2f %s", size, units[unitIndex])
|
|
}
|