2023-10-11 17:55:07 +00:00
|
|
|
package format
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
2023-10-12 16:34:16 +00:00
|
|
|
const (
|
|
|
|
Byte = 1
|
|
|
|
KiloByte = Byte * 1000
|
|
|
|
MegaByte = KiloByte * 1000
|
|
|
|
GigaByte = MegaByte * 1000
|
|
|
|
)
|
|
|
|
|
2023-10-11 17:55:07 +00:00
|
|
|
func HumanBytes(b int64) string {
|
|
|
|
switch {
|
2023-10-12 16:34:16 +00:00
|
|
|
case b > GigaByte:
|
|
|
|
return fmt.Sprintf("%d GB", b/GigaByte)
|
|
|
|
case b > MegaByte:
|
|
|
|
return fmt.Sprintf("%d MB", b/MegaByte)
|
|
|
|
case b > KiloByte:
|
|
|
|
return fmt.Sprintf("%d KB", b/KiloByte)
|
2023-10-11 17:55:07 +00:00
|
|
|
default:
|
|
|
|
return fmt.Sprintf("%d B", b)
|
|
|
|
}
|
|
|
|
}
|