github.com/hbdrawn/golang@v0.0.0-20141214014649-6b835209aba2/doc/progs/eff_bytesize.go (about) 1 // cmpout 2 3 // Copyright 2009 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 package main 8 9 import "fmt" 10 11 type ByteSize float64 12 13 const ( 14 _ = iota // ignore first value by assigning to blank identifier 15 KB ByteSize = 1 << (10 * iota) 16 MB 17 GB 18 TB 19 PB 20 EB 21 ZB 22 YB 23 ) 24 25 func (b ByteSize) String() string { 26 switch { 27 case b >= YB: 28 return fmt.Sprintf("%.2fYB", b/YB) 29 case b >= ZB: 30 return fmt.Sprintf("%.2fZB", b/ZB) 31 case b >= EB: 32 return fmt.Sprintf("%.2fEB", b/EB) 33 case b >= PB: 34 return fmt.Sprintf("%.2fPB", b/PB) 35 case b >= TB: 36 return fmt.Sprintf("%.2fTB", b/TB) 37 case b >= GB: 38 return fmt.Sprintf("%.2fGB", b/GB) 39 case b >= MB: 40 return fmt.Sprintf("%.2fMB", b/MB) 41 case b >= KB: 42 return fmt.Sprintf("%.2fKB", b/KB) 43 } 44 return fmt.Sprintf("%.2fB", b) 45 } 46 47 func main() { 48 fmt.Println(YB, ByteSize(1e13)) 49 }