units

package module
Version: v0.0.0-...-b94a6e3 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2021 License: MIT Imports: 3 Imported by: 290

README

Go Reference

Units - Helpful unit multipliers and functions for Go

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

It allows for code like this:

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

Documentation

Overview

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

Constants

View Source
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.

View Source
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.

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

SI unit multiples.

Variables

This section is empty.

Functions

func MakeUnitMap

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

func ParseStrictBytes

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

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

func ToString

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

Types

type Base2Bytes

type Base2Bytes int64

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

func ParseBase2Bytes

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) Floor

func (b Base2Bytes) Floor() Base2Bytes

Floor returns Base2Bytes with all but the largest unit zeroed out. So that e.g. 1GiB1MiB1KiB → 1GiB.

func (Base2Bytes) MarshalText

func (b Base2Bytes) MarshalText() ([]byte, error)

MarshalText implement encoding.TextMarshaler to process json/yaml.

func (Base2Bytes) Round

func (b Base2Bytes) Round(n int) Base2Bytes

Round returns Base2Bytes with all but the first n units zeroed out. So that e.g. 1GiB1MiB1KiB → 1GiB1MiB, if n is 2.

func (Base2Bytes) String

func (b Base2Bytes) String() string

func (*Base2Bytes) UnmarshalText

func (b *Base2Bytes) UnmarshalText(text []byte) error

UnmarshalText implement encoding.TextUnmarshaler to process json/yaml.

type MetricBytes

type MetricBytes SI

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

func ParseMetricBytes

func ParseMetricBytes(s string) (MetricBytes, error)

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

func (MetricBytes) Floor

func (b MetricBytes) Floor() MetricBytes

Floor returns MetricBytes with all but the largest unit zeroed out. So that e.g. 1GB1MB1KB → 1GB.

func (MetricBytes) Round

func (b MetricBytes) Round(n int) MetricBytes

Round returns MetricBytes with all but the first n units zeroed out. So that e.g. 1GB1MB1KB → 1GB1MB, if n is 2.

func (MetricBytes) String

func (m MetricBytes) String() string

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

type SI

type SI int64

SI units.

Jump to

Keyboard shortcuts

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