[Agent][GO] - optimize agent initialization

This commit is contained in:
zeaslity
2024-04-28 16:55:18 +08:00
parent f124972b84
commit 5d80d187a5
5 changed files with 52 additions and 11 deletions

View File

@@ -1,6 +1,7 @@
package utils
import (
"fmt"
"math/rand"
"time"
)
@@ -15,3 +16,36 @@ func GenerateRandomString(length int) string {
}
return string(b)
}
func ByteSizeToString(size uint64) string {
const (
B = 1
KB = 1024 * B
MB = 1024 * KB
GB = 1024 * MB
TB = 1024 * GB
)
var unit string
var value float64
switch {
case size >= TB:
value = float64(size) / TB
unit = "TB"
case size >= GB:
value = float64(size) / GB
unit = "GB"
case size >= MB:
value = float64(size) / MB
unit = "MB"
case size >= KB:
value = float64(size) / KB
unit = "KB"
default:
value = float64(size)
unit = "B"
}
return fmt.Sprintf("%.2f %s", value, unit)
}