| 1 | # datasize [](https://travis-ci.org/c2h5oh/datasize) |
| 2 | |
| 3 | Golang helpers for data sizes |
| 4 | |
| 5 | ### Constants |
| 6 | |
| 7 | Just like `time` package provides `time.Second`, `time.Day` constants `datasize` provides: |
| 8 | |
| 9 | * `datasize.B` 1 byte |
| 10 | * `datasize.KB` 1 kilobyte |
| 11 | * `datasize.MB` 1 megabyte |
| 12 | * `datasize.GB` 1 gigabyte |
| 13 | * `datasize.TB` 1 terabyte |
| 14 | * `datasize.PB` 1 petabyte |
| 15 | * `datasize.EB` 1 exabyte |
| 16 | |
| 17 | ### Helpers |
| 18 | |
| 19 | Just like `time` package provides `duration.Nanoseconds() uint64 `, `duration.Hours() float64` helpers `datasize` has. |
| 20 | |
| 21 | * `ByteSize.Bytes() uint64` |
| 22 | * `ByteSize.Kilobytes() float64` |
| 23 | * `ByteSize.Megabytes() float64` |
| 24 | * `ByteSize.Gigabytes() float64` |
| 25 | * `ByteSize.Terabytes() float64` |
| 26 | * `ByteSize.Petabytes() float64` |
| 27 | * `ByteSize.Exabytes() float64` |
| 28 | |
| 29 | Warning: see limitations at the end of this document about a possible precision loss |
| 30 | |
| 31 | ### Parsing strings |
| 32 | |
| 33 | `datasize.ByteSize` implements `TextUnmarshaler` interface and will automatically parse human readable strings into correct values where it is used: |
| 34 | |
| 35 | * `"10 MB"` -> `10* datasize.MB` |
| 36 | * `"10240 g"` -> `10 * datasize.TB` |
| 37 | * `"2000"` -> `2000 * datasize.B` |
| 38 | * `"1tB"` -> `datasize.TB` |
| 39 | * `"5 peta"` -> `5 * datasize.PB` |
| 40 | * `"28 kilobytes"` -> `28 * datasize.KB` |
| 41 | * `"1 gigabyte"` -> `1 * datasize.GB` |
| 42 | |
| 43 | You can also do it manually: |
| 44 | |
| 45 | ```go |
| 46 | var v datasize.ByteSize |
| 47 | err := v.UnmarshalText([]byte("100 mb")) |
| 48 | ``` |
| 49 | |
| 50 | ### Printing |
| 51 | |
| 52 | `Bytesize.String()` uses largest unit allowing an integer value: |
| 53 | |
| 54 | * `(102400 * datasize.MB).String()` -> `"100GB"` |
| 55 | * `(datasize.MB + datasize.KB).String()` -> `"1025KB"` |
| 56 | |
| 57 | Use `%d` format string to get value in bytes without a unit. |
| 58 | |
| 59 | ### JSON and other encoding |
| 60 | |
| 61 | Both `TextMarshaler` and `TextUnmarshaler` interfaces are implemented - JSON will just work. Other encoders will work provided they use those interfaces. |
| 62 | |
| 63 | ### Human readable |
| 64 | |
| 65 | `ByteSize.HumanReadable()` or `ByteSize.HR()` returns a string with 1-3 digits, followed by 1 decimal place, a space and unit big enough to get 1-3 digits |
| 66 | |
| 67 | * `(102400 * datasize.MB).String()` -> `"100.0 GB"` |
| 68 | * `(datasize.MB + 512 * datasize.KB).String()` -> `"1.5 MB"` |
| 69 | |
| 70 | ### Limitations |
| 71 | |
| 72 | * The underlying data type for `data.ByteSize` is `uint64`, so values outside of 0 to 2^64-1 range will overflow |
| 73 | * size helper functions (like `ByteSize.Kilobytes()`) return `float64`, which can't represent all possible values of `uint64` accurately: |
| 74 | * if the returned value is supposed to have no fraction (ie `(10 * datasize.MB).Kilobytes()`) accuracy loss happens when value is more than 2^53 larger than unit: `.Kilobytes()` over 8 petabytes, `.Megabytes()` over 8 exabytes |
| 75 | * if the returned value is supposed to have a fraction (ie `(datasize.PB + datasize.B).Megabytes()`) in addition to the above note accuracy loss may occur in fractional part too - larger integer part leaves fewer bytes to store fractional part, the smaller the remainder vs unit the move bytes are required to store the fractional part |
| 76 | * Parsing a string with `Mb`, `Tb`, etc units will return a syntax error, because capital followed by lower case is commonly used for bits, not bytes |
| 77 | * Parsing a string with value exceeding 2^64-1 bytes will return 2^64-1 and an out of range error |