Golang helpers for data sizes (kilobytes, petabytes), human readable sizes, parsing
Go
165
19 commits
updated Dec 15, 2023
Golang helpers for data sizes
Just like time package provides time.Second, time.Day constants datasize provides:
datasize.B 1 bytedatasize.KB 1 kilobytedatasize.MB 1 megabytedatasize.GB 1 gigabytedatasize.TB 1 terabytedatasize.PB 1 petabytedatasize.EB 1 exabyteJust like time package provides duration.Nanoseconds() uint64 , duration.Hours() float64 helpers datasize has.
ByteSize.Bytes() uint64ByteSize.Kilobytes() float64ByteSize.Megabytes() float64ByteSize.Gigabytes() float64ByteSize.Terabytes() float64ByteSize.Petabytes() float64ByteSize.Exabytes() float64Warning: see limitations at the end of this document about a possible precision loss
datasize.ByteSize implements TextUnmarshaler interface and will automatically parse human readable strings into correct values where it is used:
"10 MB" -> 10* datasize.MB"10240 g" -> 10 * datasize.TB"2000" -> 2000 * datasize.B"1tB" -> datasize.TB"5 peta" -> 5 * datasize.PB"28 kilobytes" -> 28 * datasize.KB"1 gigabyte" -> 1 * datasize.GBYou can also do it manually:
var v datasize.ByteSize
err := v.UnmarshalText([]byte("100 mb"))
Bytesize.String() uses largest unit allowing an integer value:
(102400 * datasize.MB).String() -> "100GB"(datasize.MB + datasize.KB).String() -> "1025KB"Use %d format string to get value in bytes without a unit.
Both TextMarshaler and TextUnmarshaler interfaces are implemented - JSON will just work. Other encoders will work provided they use those interfaces.
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
(102400 * datasize.MB).String() -> "100.0 GB"(datasize.MB + 512 * datasize.KB).String() -> "1.5 MB"data.ByteSize is uint64, so values outside of 0 to 2^64-1 range will overflowByteSize.Kilobytes()) return float64, which can't represent all possible values of uint64 accurately:
(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(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 partMb, Tb, etc units will return a syntax error, because capital followed by lower case is commonly used for bits, not bytes125 followers · starred Jun 2018
25 followers · starred Oct 2017
Golang helpers for data sizes (kilobytes, petabytes), human readable sizes, parsing
Go
165
19 commits
updated Dec 15, 2023
Golang helpers for data sizes
Just like time package provides time.Second, time.Day constants datasize provides:
datasize.B 1 bytedatasize.KB 1 kilobytedatasize.MB 1 megabytedatasize.GB 1 gigabytedatasize.TB 1 terabytedatasize.PB 1 petabytedatasize.EB 1 exabyteJust like time package provides duration.Nanoseconds() uint64 , duration.Hours() float64 helpers datasize has.
ByteSize.Bytes() uint64ByteSize.Kilobytes() float64ByteSize.Megabytes() float64ByteSize.Gigabytes() float64ByteSize.Terabytes() float64ByteSize.Petabytes() float64ByteSize.Exabytes() float64Warning: see limitations at the end of this document about a possible precision loss
datasize.ByteSize implements TextUnmarshaler interface and will automatically parse human readable strings into correct values where it is used:
"10 MB" -> 10* datasize.MB"10240 g" -> 10 * datasize.TB"2000" -> 2000 * datasize.B"1tB" -> datasize.TB"5 peta" -> 5 * datasize.PB"28 kilobytes" -> 28 * datasize.KB"1 gigabyte" -> 1 * datasize.GBYou can also do it manually:
var v datasize.ByteSize
err := v.UnmarshalText([]byte("100 mb"))
Bytesize.String() uses largest unit allowing an integer value:
(102400 * datasize.MB).String() -> "100GB"(datasize.MB + datasize.KB).String() -> "1025KB"Use %d format string to get value in bytes without a unit.
Both TextMarshaler and TextUnmarshaler interfaces are implemented - JSON will just work. Other encoders will work provided they use those interfaces.
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
(102400 * datasize.MB).String() -> "100.0 GB"(datasize.MB + 512 * datasize.KB).String() -> "1.5 MB"data.ByteSize is uint64, so values outside of 0 to 2^64-1 range will overflowByteSize.Kilobytes()) return float64, which can't represent all possible values of uint64 accurately:
(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(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 partMb, Tb, etc units will return a syntax error, because capital followed by lower case is commonly used for bits, not bytes125 followers · starred Jun 2018
25 followers · starred Oct 2017