units

package
v0.0.0-...-1a1319b Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2020 License: Apache-2.0, MIT Imports: 3 Imported by: 0

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 represents 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.

func (Base2Bytes) String

func (b Base2Bytes) String() string

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

func (m MetricBytes) String() string

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