size

package
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2023 License: MIT Imports: 5 Imported by: 3

Documentation

Index

Examples

Constants

View Source
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"
)

See: http://en.wikipedia.org/wiki/Binary_prefix

Variables

View Source
var (
	DecimalSizeRegexp = regexp.MustCompile(`(?m)^(\d+[\d\.]+?) ?([kKmMgGtTpPbB][bB]?)$`)

	BinarySizeRegexp = regexp.MustCompile(`(?m)^(\d+[\d\.]+?) ?([kKmMgGtTpPbB][iI][bB]?)$`)
)

Functions

func FormatBinary

func FormatBinary(unit uint64) string

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)))
Output:

func FormatHuman

func FormatHuman(unit uint64) string

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)))
Output:

func FormatSize

func FormatSize(format string, unit uint64, suffixes Suffixes) string

FormatSize returns a human-readable approximation of the size using the given format and the given Suffixes specification

func FromBinarySize

func FromBinarySize(size string) (uint64, error)

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"))
Output:

func FromHumanSize

func FromHumanSize(size string) (uint64, error)

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"))
Output:

func FromSize

func FromSize(size string, units Units) (uint64, error)

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

func ParseSize(size string) (uint64, error)

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"))
Output:

Types

type Suffix

type Suffix string

type Suffixes

type Suffixes []*struct {
	Unit   uint64
	Suffix Suffix
}

Suffixes represents the specification of the ratio of size to suffix.

type Units

type Units map[string]uint64

Units provides a specification of the relationship of the suffix to the size.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL