package mathutilimport// DataSize format bytes number friendly. eg: 1024 => 1KB, 1024*1024 => 1MB//// Usage://// file, err := os.Open(path)// fl, err := file.Stat()// fmtSize := DataSize(fl.Size())func ( uint64) string {switch {case < 1024:returnfmt.Sprintf("%dB", )case < 1024*1024:returnfmt.Sprintf("%.2fK", float64()/1024)case < 1024*1024*1024:returnfmt.Sprintf("%.2fM", float64()/1024/1024)default:returnfmt.Sprintf("%.2fG", float64()/1024/1024/1024) }}// FormatBytes Format the byte size to be a readable string. eg: 1024 => 1 KBfunc ( int) string {const = 1024if < {returnfmt.Sprintf("%d B", ) } , := int64(), 0for := / ; >= ; /= { *= ++ }returnfmt.Sprintf("%.2f %cB", float64()/float64(), "KMGTPE"[])}var timeFormats = [][]int{ {0}, {1}, {2, 1}, {60}, {120, 60}, {3600}, {7200, 3600}, {86400}, {172800, 86400}, // second elem is unit. {2592000}, {2592000 * 2, 2592000},}var timeMessages = []string{"< 1 sec", "1 sec", "secs", "1 min", "mins", "1 hr", "hrs", "1 day", "days", "1 month", "months",}// HowLongAgo format a seconds, get how lang ago. eg: 1 day, 1 weekfunc ( int64) string { := int() := len(timeFormats)for , := rangetimeFormats {if >= [0] { := + 1 := falseif < { // next exists := timeFormats[]if < [0] { // current <= intVal < next = true } } elseif == { // current is last = true }if { // match successiflen() == 1 {returntimeMessages[] }returnfmt.Sprintf("%d %s", /[1], timeMessages[]) } } }return"unknown"// He should never happen}
The pages are generated with Goldsv0.8.4. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.