units: github.com/alecthomas/units Index | Files

package units

import "github.com/alecthomas/units"

Package units provides helpful unit multipliers and functions for Go.

The goal of this package is to have functionality similar to the time [1] package.

[1] http://golang.org/pkg/time/

It allows for code like this:

n, err := ParseBase2Bytes("1KB")
// n == 1024
n = units.Mebibyte * 512

Index

Package Files

bytes.go doc.go si.go util.go

Constants

const (
    Kibibyte Base2Bytes = 1024
    KiB                 = Kibibyte
    Mebibyte            = Kibibyte * 1024
    MiB                 = Mebibyte
    Gibibyte            = Mebibyte * 1024
    GiB                 = Gibibyte
    Tebibyte            = Gibibyte * 1024
    TiB                 = Tebibyte
    Pebibyte            = Tebibyte * 1024
    PiB                 = Pebibyte
    Exbibyte            = Pebibyte * 1024
    EiB                 = Exbibyte
)

Base-2 byte units.

const (
    Kilobyte MetricBytes = 1000
    KB                   = Kilobyte
    Megabyte             = Kilobyte * 1000
    MB                   = Megabyte
    Gigabyte             = Megabyte * 1000
    GB                   = Gigabyte
    Terabyte             = Gigabyte * 1000
    TB                   = Terabyte
    Petabyte             = Terabyte * 1000
    PB                   = Petabyte
    Exabyte              = Petabyte * 1000
    EB                   = Exabyte
)

SI base-10 byte units.

const (
    Kilo SI  = 1000
    Mega     = Kilo * 1000
    Giga     = Mega * 1000
    Tera     = Giga * 1000
    Peta     = Tera * 1000
    Exa      = Peta * 1000
)

SI unit multiples.

func MakeUnitMap Uses

func MakeUnitMap(suffix, shortSuffix string, scale int64) map[string]float64

func ParseStrictBytes Uses

func ParseStrictBytes(s string) (int64, error)

ParseStrictBytes supports both iB and B suffixes for base 2 and metric, respectively. That is, KiB represents 1024 and kB, KB represent 1000.

func ParseUnit Uses

func ParseUnit(s string, unitMap map[string]float64) (int64, error)

func ToString Uses

func ToString(n int64, scale int64, suffix, baseSuffix string) string

type Base2Bytes Uses

type Base2Bytes int64

Base2Bytes is the old non-SI power-of-2 byte scale (1024 bytes in a kilobyte, etc.).

func ParseBase2Bytes Uses

func ParseBase2Bytes(s string) (Base2Bytes, error)

ParseBase2Bytes supports both iB and B in base-2 multipliers. That is, KB and KiB are both 1024. However "kB", which is the correct SI spelling of 1000 Bytes, is rejected.

func (Base2Bytes) String Uses

func (b Base2Bytes) String() string

type MetricBytes Uses

type MetricBytes SI

MetricBytes are SI byte units (1000 bytes in a kilobyte).

func ParseMetricBytes Uses

func ParseMetricBytes(s string) (MetricBytes, error)

ParseMetricBytes parses base-10 metric byte units. That is, KB is 1000 bytes.

func (MetricBytes) String Uses

func (m MetricBytes) String() string

TODO: represents 1000B as uppercase "KB", while SI standard requires "kB".

type SI Uses

type SI int64

SI units.

Package units imports 3 packages (graph) and is imported by 85 packages. Updated 2020-04-09. Refresh now. Tools for package owners.