Documentation
¶
Index ¶
- Constants
- Variables
- func FormatBinary(unit uint64) string
- func FormatHuman(unit uint64) string
- func FormatSize(format string, unit uint64, suffixes Suffixes) string
- func FromBinarySize(size string) (uint64, error)
- func FromHumanSize(size string) (uint64, error)
- func FromSize(size string, units Units) (uint64, error)
- func ParseSize(size string) (uint64, error)
- type Suffix
- type Suffixes
- type Units
Examples ¶
Constants ¶
const ( ByteBase = 1 DecimalBase = 1000 BinaryBase = 1024 KB = DecimalBase MB = DecimalBase * KB GB = DecimalBase * MB TB = DecimalBase * GB PB = DecimalBase * TB KiB = BinaryBase MiB = BinaryBase * KiB GiB = BinaryBase * MiB TiB = BinaryBase * GiB PiB = BinaryBase * TiB Byte Suffix = "B" KiloByte Suffix = "kB" MegaByte = "MB" GigaByte = "GB" TeraByte = "TB" PetaByte = "PB" KibiByte Suffix = "KiB" MebiByte = "MiB" GibiByte = "GiB" TebiByte = "TiB" PebiByte = "PiB" FormatDefault = "%.4g%s" )
Variables ¶
var ( DecimalSizeRegexp = regexp.MustCompile(`(?m)^(\d+[\d\.]+?) ?([kKmMgGtTpPbB][bB]?)$`) BinarySizeRegexp = regexp.MustCompile(`(?m)^(\d+[\d\.]+?) ?([kKmMgGtTpPbB][iI][bB]?)$`) )
Functions ¶
func FormatBinary ¶
FormatBinary returns a human-readable size in bytes, kibibytes, mebibytes, gibibytes, or tebibytes (eg. "512kiB", "4PiB").
Example ¶
MiB5_6 := 5.6 * MiB GiB8_6 := 8.6 * GiB PiB2_7 := 2.7 * PiB fmt.Println(FormatBinary(1)) fmt.Println(FormatBinary(1024)) fmt.Println(FormatBinary(1024 * 1024)) fmt.Println(FormatBinary(1048576)) fmt.Println(FormatBinary(1024 * 1024 * 1024)) fmt.Println(FormatBinary(uint64(MiB5_6))) fmt.Println(FormatBinary(uint64(GiB8_6))) fmt.Println(FormatBinary(uint64(PiB2_7)))
func FormatHuman ¶
FormatHuman returns a human-readable approximation of a size capped at 4 valid numbers (eg. "25MB", "22GB").
Example ¶
MB5_6 := 5.6 * MB GB8_6 := 8.6 * GB PB2_7 := 2.7 * PB fmt.Println(FormatHuman(1)) fmt.Println(FormatHuman(1000)) fmt.Println(FormatHuman(1000 * 1000)) fmt.Println(FormatHuman(1000000)) fmt.Println(FormatHuman(1048576)) fmt.Println(FormatHuman(1000 * 1000 * 1000)) fmt.Println(FormatHuman(uint64(MB5_6))) fmt.Println(FormatHuman(uint64(GB8_6))) fmt.Println(FormatHuman(uint64(PB2_7)))
func FormatSize ¶
FormatSize returns a human-readable approximation of the size using the given format and the given Suffixes specification
func FromBinarySize ¶
FromBinarySize parses a human-readable string representing an amount of RAM in bytes, kibibytes, mebibytes, gibibytes, or tebibytes and returns the number of bytes or returns an error if it fails, units are case-insensitive, and the 'b' suffix is optional.
Example ¶
fmt.Println(FromBinarySize("512"))
fmt.Println(FromBinarySize("512B"))
fmt.Println(FromBinarySize("512KiB"))
fmt.Println(FromBinarySize("512Ki"))
fmt.Println(FromBinarySize("512kib"))
fmt.Println(FromBinarySize("512MiB"))
fmt.Println(FromBinarySize("512Mi"))
fmt.Println(FromBinarySize("512mi"))
fmt.Println(FromBinarySize("512GiB"))
fmt.Println(FromBinarySize("512Gi"))
fmt.Println(FromBinarySize("512TiB"))
fmt.Println(FromBinarySize("512Ti"))
fmt.Println(FromBinarySize("512PiB"))
fmt.Println(FromBinarySize("512Pi"))
func FromHumanSize ¶
FromHumanSize returns an integer from a human-readable specification of a size using SI standard (eg. "512kB", "20MB") or returns an error if it fails, units are case-insensitive, and the 'b' suffix is optional.
Example ¶
fmt.Println(FromHumanSize("512"))
fmt.Println(FromHumanSize("512B"))
fmt.Println(FromHumanSize("512kB"))
fmt.Println(FromHumanSize("512KB"))
fmt.Println(FromHumanSize("512K"))
fmt.Println(FromHumanSize("512MB"))
fmt.Println(FromHumanSize("512M"))
fmt.Println(FromHumanSize("512m"))
fmt.Println(FromHumanSize("512GB"))
fmt.Println(FromHumanSize("512gB"))
fmt.Println(FromHumanSize("512TB"))
fmt.Println(FromHumanSize("512T"))
fmt.Println(FromHumanSize("512PB"))
fmt.Println(FromHumanSize("512pb"))
func FromSize ¶
FromSize parses the human-readable size string into the amount it represents, according to the given specification or returns an error if it fails.
func ParseSize ¶
ParseSize defines the IEC/SI prefix and returns int64 as an integer or returns an error if it fails, units are case-insensitive, and the 'b' suffix is optional.
Example ¶
fmt.Println(ParseSize("512"))
fmt.Println(ParseSize("512B"))
fmt.Println(ParseSize("512KiB"))
fmt.Println(ParseSize("512Ki"))
fmt.Println(ParseSize("512kib"))
fmt.Println(ParseSize("512MiB"))
fmt.Println(ParseSize("512Mi"))
fmt.Println(ParseSize("512mi"))
fmt.Println(ParseSize("512GiB"))
fmt.Println(ParseSize("512Gi"))
fmt.Println(ParseSize("512TiB"))
fmt.Println(ParseSize("512Ti"))
fmt.Println(ParseSize("512PiB"))
fmt.Println(ParseSize("512Pi"))
fmt.Println(ParseSize("512"))
fmt.Println(ParseSize("512B"))
fmt.Println(ParseSize("512kB"))
fmt.Println(ParseSize("512KB"))
fmt.Println(ParseSize("512K"))
fmt.Println(ParseSize("512MB"))
fmt.Println(ParseSize("512M"))
fmt.Println(ParseSize("512m"))
fmt.Println(ParseSize("512GB"))
fmt.Println(ParseSize("512gB"))
fmt.Println(ParseSize("512TB"))
fmt.Println(ParseSize("512T"))
fmt.Println(ParseSize("512PB"))
fmt.Println(ParseSize("512pb"))