Documentation
¶
Overview ¶
Package bytez encapsulates functionality for working with large byte sizes in a human-friendly way. (The 'z' in the name is not an attempt to be cute but to create a package name that is short yet unique.)
Unfortunately, the history is computing is one of inconsistency and ambiguity when it comes to specifying large byte sizes. For example, does 1KB mean 1000 bytes or 1024? It has been used to mean both. The unambiguous units KiB to indicate binary (powers of two) units were introduced in 1998 but are not universally used.
This package handles the ambiguity by using the letter case of the first letter of the units to determine the base: lowercase indicates base 10 and uppercase indicates base 2. (The "smaller" letter indicates the smaller units.) Thus, the following units all represent 4000 bytes: 4k, 4kb, 4kB. And the following units all represent 4096 bytes: 4K, 4KB, 4Kb, 4KiB
When converting from numbers to strings, this package uses the two-letter lowercase units (e.g. "mb") for powers of 10 and the three-letter mixed case (e.g. "MiB") for powers of 2.
Index ¶
Constants ¶
const ( Kilobyte uint64 = 1000 Megabyte = Kilobyte * 1000 Gigabyte = Megabyte * 1000 Terabyte = Gigabyte * 1000 Petabyte = Terabyte * 1000 Exabyte = Petabyte * 1000 )
Decimal (SI) constants that fit in 64 bits
const ( Kibibyte uint64 = 1024 Mebibyte = Kibibyte * 1024 Gibibyte = Mebibyte * 1024 Tebibyte = Gibibyte * 1024 Pebibyte = Tebibyte * 1024 Exbibyte = Pebibyte * 1024 )
Binary (ISO/IEC) constants that fit in 64 bits
Variables ¶
This section is empty.
Functions ¶
func AsInt ¶
AsInt accepts a byte size, like "4MiB", and returns the exact number of bytes, like 4194304. The leading number should be a whole number, but as a special case the fractions ".0" and ".5" are allowed, like "1.5mb" to indicate 1,500,000 bytes. A single space is allowed between the number and the units.
Types ¶
type Size ¶
type Size uint64
Size can be used to automatically marshal and unmarshal byte size specifications to and from text when parsing or outputting JSON, YAML, etc.
func (Size) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface. The size is formatted as a string using the largest units possible. Returned error is always nil.
func (*Size) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface.