go中格式化方法
将字典转换为json字符串
点击展开/收起代码
func FormatToJson(i any) string {
res, _ := json.MarshalIndent(i, "", " ")
return string(res)
}
将byte切片转换为json字符串
func FormatBytesToJsion(d []byte) string {
var data interface{}
err := json.Unmarshal(d, &data)
if err != nil {
fmt.Println("error unmarshalling json:", err)
os.Exit(1)
}
res, _ := json.MarshalIndent(data, "", " ")
return string(res)
}
将时间段转换成时间字符串
符合docker样式
func FormatTimeToString(d time.Duration) (string, error) {
if d < 0 {
return "", fmt.Errorf("时间段不应为负数")
}
seconds := d.Seconds()
if seconds < 60 {
return fmt.Sprintf("%-5.2f%6s", seconds, "seconds"), nil
}
minutes := d.Minutes()
if minutes < 60 {
return fmt.Sprintf("%-5.1f%6s", minutes, "minutes"), nil
}
hours := d.Hours()
if hours < 24 {
return fmt.Sprintf("%-5.1f%6s", hours, "hours"), nil
}
days := d.Hours() / 24
if days < 7 {
return fmt.Sprintf("%-5.0f%6s", days, "days"), nil
}
weeks := d.Hours() / (24 * 7)
if weeks < 4 {
return fmt.Sprintf("%-5.0f%6s", weeks, "weeks"), nil
}
months := d.Hours() / (24 * 30)
if months < 12 {
return fmt.Sprintf("%-5.0f%6s", months, "months"), nil
}
years := d.Hours() / (24 * 365)
return fmt.Sprintf("%-5.0f%6s", years, "years"), nil
}
// 形如
// 6.5 hours
// 2 days
// 3 weeks
// 3 months
// 2 years
符合k8s样式
func FormatTimeToString(d time.Duration) (string, error) {
if d < 0 {
return "", fmt.Errorf("时间段不应为负数")
}
seconds := d.Seconds()
if seconds < 60 {
return fmt.Sprintf("%5.2fs", seconds), nil
}
minutes := d.Minutes()
if minutes < 60 {
rs := int64(d.Seconds() / 60)
se := int64(d.Seconds()) - (rs * 60)
if se == 0 {
return fmt.Sprintf("%dm", rs), nil
}
return fmt.Sprintf("%dm%ds", rs, se), nil
}
hours := d.Hours()
if hours < 24 {
ho := int64(d.Minutes() / 60)
mi := int64(d.Minutes()) - (ho * 60)
if mi == 0 {
return fmt.Sprintf("%dh", ho), nil
}
return fmt.Sprintf("%dh%dm", ho, mi), nil
}
days := d.Hours() / 24
if days < 7 {
da := int64(d.Hours() / 24)
ho := int64(d.Hours()) - (da * 24)
if ho == 0 {
return fmt.Sprintf("%dd", da), nil
}
return fmt.Sprintf("%dd%dh", da, ho), nil
}
weeks := d.Hours() / (24 * 7)
if weeks < 4 {
we := int64(d.Hours() / 24 / 7)
da := int64(d.Hours()/24) - (we * 7)
if da == 0 {
return fmt.Sprintf("%dw", we), nil
}
return fmt.Sprintf("%dw%dd", we, da), nil
}
months := d.Hours() / (24 * 30)
if months < 12 {
mo := int64(d.Hours() / 24 / 30)
da := int64(d.Hours()/24) - (mo * 30)
if da == 0 {
return fmt.Sprintf("%dm", mo), nil
}
return fmt.Sprintf("%dm%dd", mo, da), nil
}
years := int64(d.Hours() / 24 / 365)
mo := int64(d.Hours()/24/30) - (years * 365)
return fmt.Sprintf("%dy%dm", years, mo), nil
}
将存储大小转换成字符串
func isInteger0(f float64) bool {
f1 := fmt.Sprintf("%.2f", f)
return strings.HasSuffix(f1, ".00")
}
func isInteger1(f float64) bool {
f1 := fmt.Sprintf("%.2f", f)
return strings.HasSuffix(f1, "0")
}
func formatNumber(f float64) string {
if isInteger0(f) {
return fmt.Sprintf("%.0f", f)
}
if isInteger1(f) {
return fmt.Sprintf("%.1f", f)
}
return fmt.Sprintf("%.2f", f)
}
func FormatStorageToString(bytes int64) (string, error) {
if bytes < 0 {
return "", fmt.Errorf("bytes should not be negative")
}
const (
ByteSize = 1
KiloByte = 1024
MegaByte = 1024 * KiloByte
GigaByte = 1024 * MegaByte
TeraByte = 1024 * GigaByte
)
if bytes < KiloByte {
return formatNumber(float64(bytes)) + " b", nil
} else if bytes < MegaByte {
return formatNumber(float64(bytes)/float64(KiloByte)) + "KB", nil
} else if bytes < GigaByte {
return formatNumber(float64(bytes)/float64(MegaByte)) + "MB", nil
} else if bytes < TeraByte {
return formatNumber(float64(bytes)/float64(GigaByte)) + "GB", nil
} else {
return formatNumber(float64(bytes)/float64(TeraByte)) + "TB", nil
}
}
// 形如
// 779.32MB
// 1.65GB
// 753.31MB
// 234.22KB
本文来自博客园,作者:厚礼蝎,转载请注明原文链接:https://www.cnblogs.com/guangdelw/p/17572577.html

浙公网安备 33010602011771号