From e8322a7fd750a00ee128db01f1d8110262f01ef3 Mon Sep 17 00:00:00 2001 From: Francisco Carriedo Date: Wed, 23 Jul 2014 23:55:48 -0700 Subject: [PATCH] pkg/units: Moved 'units' out of function No need to initialize every time the function executes since it works as a catalog. Docker-DCO-1.1-Signed-off-by: Francisco Carriedo (github: fcarriedo) --- units/size.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/units/size.go b/units/size.go index 6535540..027886e 100644 --- a/units/size.go +++ b/units/size.go @@ -16,17 +16,18 @@ func init() { } } +var bytePrefixes = [...]string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"} + // HumanSize returns a human-readable approximation of a size // using SI standard (eg. "44kB", "17MB") func HumanSize(size int64) string { - units := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"} i := 0 sizef := float64(size) for sizef >= 1000.0 { sizef = sizef / 1000.0 i++ } - return fmt.Sprintf("%.4g %s", sizef, units[i]) + return fmt.Sprintf("%.4g %s", sizef, bytePrefixes[i]) } // FromHumanSize returns an integer from a human-readable specification of a size