Documentation
¶
Overview ¶
Package bytesize provides functionality for handling byte size values using IEC binary units (powers of 1024). It offers parsing, formatting, and arithmetic operations for byte sizes from Bytes up to Pebibytes.
The package focuses on IEC binary units (KiB, MiB, etc.) rather than SI decimal units (KB, MB, etc.) to avoid ambiguity in size representations. It supports:
- Parsing size strings with units (e.g., "42.5MiB", "1.2GiB")
- Converting between different units
- Basic arithmetic operations on sizes
- Handling negative and floating-point values
- Range limited to what int64 can represent (approximately 9 EiB)
Index ¶
Constants ¶
const ( // Base IEC binary units in bytes, each a power of 1024 Byte int64 = 1 << (10 * iota) // 1 byte Kibi Mebi Gibi Tebi Pebi // IEC binary unit symbols ByteSymbol = "B" // Byte KibiSymbol = "KiB" // Kibibyte MebiSymbol = "MiB" // Mebibyte GibiSymbol = "GiB" // Gibibyte TebiSymbol = "TiB" // Tebibyte PebiSymbol = "PiB" // Pebibyte // Error messages ErrEmptyString = "A Size string value cannot be empty" ErrNoValue = "No numeric value found in the given string" ErrInvalidIEC = "Invalid IEC unit symbol in Size string" ErrIntegerOverflow = "Size value is too large to be represented as an int64" )
const (
ErrJSONInvalidType = "invalid JSON byte Size type, it should be a string or a number"
)
Variables ¶
var ByteSymbolIEC = [...]string{ ByteSymbol, KibiSymbol, MebiSymbol, GibiSymbol, TebiSymbol, PebiSymbol, }
ByteSymbolIEC contains the string symbols for each IEC binary unit in ascending order. The index of each symbol corresponds to the same index in ByteValueIEC.
ByteValueIEC contains the byte values for each IEC binary unit in ascending order. This slice is used internally for unit conversion and formatting.
Functions ¶
func Parse ¶
Parse parses a string representation of a byte size with IEC binary units and returns its components.
It accepts strings in the format "NUMBER[UNIT]" where:
- number can be an integer, floating-point, or negative value
- unit is an optional IEC binary unit (B, KiB, MiB, GiB, TiB, PiB)
Truncated value are simple truncation toward zero with no rounding.
- We don't want to risk an overflow by rounding to the nearest value (-2.5 => -2, 2.5 => 3) or flooring to the nearest negative value (-2.5 => -3, 2.5 => 2).
- We "can't" use partial byte like "1.5 Bytes = 8 bits + 4 bits",
that require to set an arbitrary Byte width, which is most likely not what you want.
The function returns:
- truncated: the size in bytes as an int64, truncated toward zero
- fractional: the exact size in bytes as a float64
- representation: the canonical IEC string representation
- error: if the input is invalid or the value is too large
Examples:
Parse("42.42M") returns (44480593, 44480593.92, "42.42MiB", nil)
Parse("1024") returns (1024, 1024.0, "1KiB", nil)
Parse("-2.5KiB") returns (-2560, -2560.0, "-2.5KiB", nil)
If a short unit symbol is used (e.g., "M" instead of "MiB"), it is automatically converted to the canonical IEC to avoid ambiguity with SI decimal units.
Types ¶
type Size ¶
type Size struct {
// contains filtered or unexported fields
}
Size represents a byte size value with both truncated integer and exact floating-point representations and stores the IEC string representation of the size.
func New ¶
New creates a new Size from a string representation. It uses Parse and returns a Size struct or an error if the input is invalid.
func NewInt ¶
NewInt creates a new Size from an int64 value representing bytes. This is useful when you have a byte count and want to convert it to a human-readable format with appropriate IEC binary units.
func (*Size) Add ¶
Add adds the given size string to the current Size. The size string must be in a valid format as accepted by Parse. Returns an error if the input string is invalid or if the result would overflow.
func (*Size) AddInt ¶
AddInt adds the given number of bytes to the current Size. This is a more efficient alternative to Add when working with raw byte counts.
func (Size) Bytes ¶
Bytes returns the Byte count of the Size as an int64. It is truncated toward zero across positive and negative values (e.g., "42.42MiB" => 44480593)
func (Size) Exact ¶
Exact returns the floating-point value of the byte size as a float64. (e.g., "42.42MiB" => 44480593.92)
func (Size) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (Size) String ¶
String is the stringer method for the Size struct. It returns the canonical IEC string representation of the Size.
func (*Size) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.