(bytes int64)
| 19 | ) |
| 20 | |
| 21 | func ByteSize(bytes int64) string { |
| 22 | unit := "" |
| 23 | value := float32(bytes) |
| 24 | |
| 25 | switch { |
| 26 | case bytes >= TERABYTE: |
| 27 | unit = "T" |
| 28 | value = value / TERABYTE |
| 29 | case bytes >= GIGABYTE: |
| 30 | unit = "G" |
| 31 | value = value / GIGABYTE |
| 32 | case bytes >= MEGABYTE: |
| 33 | unit = "M" |
| 34 | value = value / MEGABYTE |
| 35 | case bytes >= KILOBYTE: |
| 36 | unit = "K" |
| 37 | value = value / KILOBYTE |
| 38 | case bytes == 0: |
| 39 | return "0" |
| 40 | case bytes < KILOBYTE: |
| 41 | unit = "B" |
| 42 | } |
| 43 | |
| 44 | stringValue := fmt.Sprintf("%.1f", value) |
| 45 | stringValue = strings.TrimSuffix(stringValue, ".0") |
| 46 | return fmt.Sprintf("%s%s", stringValue, unit) |
| 47 | } |
| 48 | |
| 49 | func ToMegabytes(s string) (int64, error) { |
| 50 | parts := bytesPattern.FindStringSubmatch(strings.TrimSpace(s)) |
no outgoing calls
no test coverage detected