Converts numeric byte values to human-readable strings and parses string representations back to numeric values. Supports both SI decimal units (KB, MB, GB using powers of 1000) and IEC binary units (KiB, MiB, GiB using powers of 1024).
import "github.com/dustin/go-humanize"func Bytes(s uint64) stringFormats a byte count as a human-readable string using SI units (powers of 1000).
Parameters:
s - The size in bytesReturns: Human-readable string like "83 MB", "1.2 GB"
Example:
humanize.Bytes(82854982) // "83 MB"
humanize.Bytes(1024) // "1.0 KB"
humanize.Bytes(1000000000) // "1.0 GB"func IBytes(s uint64) stringFormats a byte count as a human-readable string using IEC units (powers of 1024).
Parameters:
s - The size in bytesReturns: Human-readable string like "79 MiB", "1.0 GiB"
Example:
humanize.IBytes(82854982) // "79 MiB"
humanize.IBytes(1024) // "1.0 KiB"
humanize.IBytes(1073741824) // "1.0 GiB"func ParseBytes(s string) (uint64, error)Parses a string representation of bytes into a uint64 value. Supports both SI and IEC units.
Parameters:
s - String like "42 MB", "42 mib", "1.5 GB"Returns:
uint64 - The parsed byte counterror - Error if parsing failsExample:
bytes, err := humanize.ParseBytes("42 MB") // 42000000, nil
bytes, err := humanize.ParseBytes("42 MiB") // 44040192, nil
bytes, err := humanize.ParseBytes("1.5 GB") // 1500000000, nilFor sizes that exceed uint64 capacity, the package provides equivalent functions using big.Int.
func BigBytes(s *big.Int) stringFormats a big.Int byte count as a human-readable string using SI units.
Parameters:
s - The size in bytes as a big.IntReturns: Human-readable string like "83 MB"
Example:
import "math/big"
size := big.NewInt(82854982)
humanize.BigBytes(size) // "83 MB"func BigIBytes(s *big.Int) stringFormats a big.Int byte count as a human-readable string using IEC units.
Parameters:
s - The size in bytes as a big.IntReturns: Human-readable string like "79 MiB"
Example:
import "math/big"
size := big.NewInt(82854982)
humanize.BigIBytes(size) // "79 MiB"func ParseBigBytes(s string) (*big.Int, error)Parses a string representation of bytes into a big.Int value. Supports both SI and IEC units.
Parameters:
s - String like "42 MB", "42 mib", "999 ZB"Returns:
*big.Int - The parsed byte counterror - Error if parsing failsExample:
bytes, err := humanize.ParseBigBytes("42 MB") // big.Int(42000000), nil
bytes, err := humanize.ParseBigBytes("42 MiB") // big.Int(44040192), nilThe package exports constants for common byte sizes:
// IEC binary size constants (powers of 1024)
const (
Byte = 1 << (iota * 10) // 1
KiByte // 1024
MiByte // 1048576
GiByte // 1073741824
TiByte
PiByte
EiByte
)
// SI decimal size constants (powers of 1000)
const (
IByte = 1
KByte = IByte * 1000 // 1000
MByte = KByte * 1000 // 1000000
GByte = MByte * 1000 // 1000000000
TByte = GByte * 1000
PByte = TByte * 1000
EByte = PByte * 1000
)For very large sizes, the package provides pre-computed big.Int variables:
// IEC binary size variables (powers of 1024)
var (
BigByte *big.Int // 1
BigKiByte *big.Int // 1024
BigMiByte *big.Int // 1024^2
BigGiByte *big.Int // 1024^3
BigTiByte *big.Int
BigPiByte *big.Int
BigEiByte *big.Int
BigZiByte *big.Int
BigYiByte *big.Int
BigRiByte *big.Int
BigQiByte *big.Int
)
// SI decimal size variables (powers of 1000)
var (
BigSIByte *big.Int // 1
BigKByte *big.Int // 1000
BigMByte *big.Int // 1000^2
BigGByte *big.Int // 1000^3
BigTByte *big.Int
BigPByte *big.Int
BigEByte *big.Int
BigZByte *big.Int
BigYByte *big.Int
BigRByte *big.Int
BigQByte *big.Int
)Example:
import "math/big"
// Calculate a very large size
largeSize := (&big.Int{}).Mul(humanize.BigGByte, big.NewInt(9999999))
fmt.Println(humanize.BigBytes(largeSize)) // "10 PB"ParseBytes and ParseBigBytes are case-insensitive and accept both "MB" and "mb" or "MiB" and "mib".