Skip to content

Instantly share code, notes, and snippets.

@kebyn
Created February 26, 2021 05:58
Show Gist options
  • Save kebyn/bc20eb275df208291a48752b3e9ddb13 to your computer and use it in GitHub Desktop.
Save kebyn/bc20eb275df208291a48752b3e9ddb13 to your computer and use it in GitHub Desktop.
func bytes2human(inBytes int64) (result string) {
suffixes := []string{"B", "KB", "MB", "GB", "TB", "PB"}
suffix := 0
for inBytes > 9999 && suffix < len(suffixes) {
inBytes = inBytes >> 10
suffix++
}
if suffix >= len(suffixes) {
suffix--
}
return fmt.Sprintf("%d%s", inBytes, suffixes[suffix])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment