Documentation ¶
Overview ¶
Package units is a library for manipulating and converting between various units of measurement
Index ¶
- Variables
- func NewConversion(from, to Unit, formula string)
- func NewConversionFromFn(from, to Unit, f ConversionFn, formula string)
- func NewRatioConversion(from, to Unit, ratio float64)
- type Conversion
- type ConversionFn
- type FmtOptions
- type Unit
- func All() []Unit
- func Atto(b Unit, o ...UnitOption) Unit
- func Centi(b Unit, o ...UnitOption) Unit
- func Deca(b Unit, o ...UnitOption) Unit
- func Deci(b Unit, o ...UnitOption) Unit
- func Exa(b Unit, o ...UnitOption) Unit
- func Femto(b Unit, o ...UnitOption) Unit
- func Find(s string) (Unit, error)
- func Giga(b Unit, o ...UnitOption) Unit
- func Hecto(b Unit, o ...UnitOption) Unit
- func Kilo(b Unit, o ...UnitOption) Unit
- func Mega(b Unit, o ...UnitOption) Unit
- func Micro(b Unit, o ...UnitOption) Unit
- func Milli(b Unit, o ...UnitOption) Unit
- func Nano(b Unit, o ...UnitOption) Unit
- func NewUnit(name, symbol string, opts ...UnitOption) Unit
- func Peta(b Unit, o ...UnitOption) Unit
- func Pico(b Unit, o ...UnitOption) Unit
- func Tera(b Unit, o ...UnitOption) Unit
- type UnitList
- type UnitOption
- type Value
Constants ¶
This section is empty.
Variables ¶
var ( Bi = UnitOptionQuantity("bits") Data = UnitOptionQuantity("bytes") Byte = NewUnit("byte", "B", Data) KiloByte = NewUnit("kilobyte", "KB", Data) MegaByte = NewUnit("megabyte", "MB", Data) GigaByte = NewUnit("gigabyte", "GB", Data) TeraByte = NewUnit("terabyte", "TB", Data) PetaByte = NewUnit("petabyte", "PB", Data) ExaByte = NewUnit("exabyte", "", Data) ZettaByte = NewUnit("zettabyte", "", Data) YottaByte = NewUnit("yottabyte", "", Data) Bit = NewUnit("bit", "b", Bi) ExaBit = Exa(Bit) PetaBit = Peta(Bit) TeraBit = Tera(Bit) GigaBit = Giga(Bit) MegaBit = Mega(Bit) KiloBit = Kilo(Bit) Nibble = NewUnit("nibble", "", Data) )
var ( Length = UnitOptionQuantity("length") // metric Meter = NewUnit("meter", "m", Length, SI, UnitOptionAliases("metre")) ExaMeter = Exa(Meter) PetaMeter = Peta(Meter) TeraMeter = Tera(Meter) GigaMeter = Giga(Meter) MegaMeter = Mega(Meter) KiloMeter = Kilo(Meter) HectoMeter = Hecto(Meter) DecaMeter = Deca(Meter) DeciMeter = Deci(Meter) CentiMeter = Centi(Meter) MilliMeter = Milli(Meter) MicroMeter = Micro(Meter) NanoMeter = Nano(Meter) PicoMeter = Pico(Meter) FemtoMeter = Femto(Meter) AttoMeter = Atto(Meter) Angstrom = NewUnit("angstrom", "Å", Length, BI, UnitOptionPlural("angstroms")) Inch = NewUnit("inch", "in", Length, BI, UnitOptionPlural("inches")) Foot = NewUnit("foot", "ft", Length, BI, UnitOptionPlural("feet")) Yard = NewUnit("yard", "yd", Length, BI) Mile = NewUnit("mile", "", Length, BI) League = NewUnit("league", "lea", Length, BI) Furlong = NewUnit("furlong", "fur", Length, BI) )
var ( Mass = UnitOptionQuantity("mass") // metric Gram = NewUnit("gram", "g", Mass) ExaGram = Exa(Gram) PetaGram = Peta(Gram) TeraGram = Tera(Gram) GigaGram = Giga(Gram) MegaGram = Mega(Gram) KiloGram = Kilo(Gram) HectoGram = Hecto(Gram) DecaGram = Deca(Gram) DeciGram = Deci(Gram) CentiGram = Centi(Gram) MilliGram = Milli(Gram) MicroGram = Micro(Gram) NanoGram = Nano(Gram) PicoGram = Pico(Gram) FemtoGram = Femto(Gram) AttoGram = Atto(Gram) // imperial Grain = NewUnit("grain", "gr", Mass, BI) Drachm = NewUnit("drachm", "dr", Mass, BI) Ounce = NewUnit("ounce", "oz", Mass, BI) Pound = NewUnit("pound", "lb", Mass, BI) Stone = NewUnit("stone", "st", Mass, BI) Ton = NewUnit("ton", "t", Mass, BI) Slug = NewUnit("slug", "", Mass, BI) )
var ( Pressure = UnitOptionQuantity("pressure") // SI unit metric Pascal = NewUnit("pascal", "Pa", Pressure, SI) ExaPascal = Exa(Pascal) PetaPascal = Peta(Pascal) TeraPascal = Tera(Pascal) GigaPascal = Giga(Pascal) MegaPascal = Mega(Pascal) KiloPascal = Kilo(Pascal) HectoPascal = Hecto(Pascal) DecaPascal = Deca(Pascal) DeciPascal = Deci(Pascal) CentiPascal = Centi(Pascal) MilliPascal = Milli(Pascal) MicroPascal = Micro(Pascal) NanoPascal = Nano(Pascal) PicoPascal = Pico(Pascal) FemtoPascal = Femto(Pascal) AttoPascal = Atto(Pascal) // Other At = NewUnit("technical atmosphere", "at", Pressure, BI, UnitOptionPlural("technical atmospheres")) Atm = NewUnit("standard atmosphere", "atm", Pressure, BI, UnitOptionPlural("standard atmospheres")) Bar = NewUnit("bar", "bar", Pressure, BI, UnitOptionPlural("bars")) CentiBar = Centi(Bar) MilliBar = Milli(Bar) MicroBar = Micro(Bar) Barye = NewUnit("barye", "Ba", Pressure, BI, UnitOptionPlural("baryes")) InH2O = NewUnit("inch of Water Column", "inH2O", Pressure, BI) InHg = NewUnit("inch of Mercury", "inHg", Pressure, BI) MH2O = NewUnit("meter of Water Column", "mmH2O", Pressure, BI, UnitOptionPlural("meters of Water Column")) MmH2O = Milli(MH2O) CmH2O = Centi(MH2O) MHg = NewUnit("meter of Mercury", "mmHg", Pressure, BI, UnitOptionPlural("meters of Mercury")) MmHg = Milli(MHg) CmHg = Centi(MHg) Newton = NewUnit("newton per square meter", "N/m²", Pressure, BI) Psi = NewUnit("pound-force per square inch", "psi", Pressure, BI) Torr = NewUnit("torr", "Torr", Pressure, BI) )
var ( Temp = UnitOptionQuantity("temperature") Celsius = NewUnit("celsius", "C", Temp, UnitOptionPlural("none"), SI) Fahrenheit = NewUnit("fahrenheit", "F", Temp, UnitOptionPlural("none"), US) Kelvin = NewUnit("kelvin", "K", Temp, UnitOptionPlural("none"), SI) )
var ( Time = UnitOptionQuantity("time") Second = NewUnit("second", "s", Time) ExaSecond = Exa(Second) PetaSecond = Peta(Second) TeraSecond = Tera(Second) GigaSecond = Giga(Second) MegaSecond = Mega(Second) KiloSecond = Kilo(Second) HectoSecond = Hecto(Second) DecaSecond = Deca(Second) DeciSecond = Deci(Second) CentiSecond = Centi(Second) MilliSecond = Milli(Second) MicroSecond = Micro(Second) NanoSecond = Nano(Second) PicoSecond = Pico(Second) FemtoSecond = Femto(Second) AttoSecond = Atto(Second) Minute = NewUnit("minute", "min", Time) Hour = NewUnit("hour", "hr", Time) Day = NewUnit("day", "d", Time) Month = NewUnit("month", "", Time) Year = NewUnit("year", "yr", Time) Decade = NewUnit("decade", "", Time) Century = NewUnit("century", "", Time) Millennium = NewUnit("millennium", "", Time) // more esoteric time units PlanckTime = NewUnit("planck time", "𝑡ₚ", Time) Fortnight = NewUnit("fortnight", "", Time) Score = NewUnit("score", "", Time) )
var ( Torque = UnitOptionQuantity("torque") // metric NewtonMeter = NewUnit("newton meter", "Nm", Torque, SI, UnitOptionAliases("newton metre", "newtonmeter", "newtonmetre")) NewtonCentimeter = NewUnit("newton centimeter", "Ncm", Torque, SI, UnitOptionAliases("newton centimetre", "newtoncentimeter", "newtoncentimetre")) // imperial PoundForceFoot = NewUnit("pound-force foot", "lbsf.ft", Torque, BI, UnitOptionAliases("lbsf-ft", "ft.lb", "ft-lb", "ft-lbf", "ft.lbf")) PoundForceInch = NewUnit("pound-force inch", "lbsf.in", Torque, BI, UnitOptionAliases("lbsf-in", "in.lb", "in-lb", "in-lbf", "in.lbf")) )
var ( // Shorthand for pre-defined unit systems BI = UnitOptionSystem("imperial") SI = UnitOptionSystem("metric") US = UnitOptionSystem("us") )
var ( Volume = UnitOptionQuantity("volume") // metric Liter = NewUnit("liter", "l", Volume, SI, UnitOptionAliases("litre")) ExaLiter = Exa(Liter) PetaLiter = Peta(Liter) TeraLiter = Tera(Liter) GigaLiter = Giga(Liter) MegaLiter = Mega(Liter) KiloLiter = Kilo(Liter) HectoLiter = Hecto(Liter) DecaLiter = Deca(Liter) DeciLiter = Deci(Liter) CentiLiter = Centi(Liter) MilliLiter = Milli(Liter) MicroLiter = Micro(Liter) NanoLiter = Nano(Liter) PicoLiter = Pico(Liter) FemtoLiter = Femto(Liter) AttoLiter = Atto(Liter) // imperial Quart = NewUnit("quart", "qt", Volume, BI) Pint = NewUnit("pint", "pt", Volume, BI) Gallon = NewUnit("gallon", "gal", Volume, BI) FluidOunce = NewUnit("fluid ounce", "fl oz", Volume, BI, UnitOptionAliases("floz")) // US FluidQuart = NewUnit("fluid quart", "", Volume, US) FluidPint = NewUnit("fluid pint", "", Volume, US) FluidGallon = NewUnit("fluid gallon", "", Volume, US) CustomaryFluidOunce = NewUnit("customary fluid ounce", "", Volume, US) )
var DefaultFmtOptions = FmtOptions{true, false, 6}
Functions ¶
func NewConversion ¶
NewConversion registers a new conversion formula from one Unit to another
func NewConversionFromFn ¶
func NewConversionFromFn(from, to Unit, f ConversionFn, formula string)
NewConversion registers a new conversion formula from one Unit to another
func NewRatioConversion ¶
Register a conversion formula and the inverse, given a ratio of from Unit in to Unit
Types ¶
type Conversion ¶
type Conversion struct { Fn ConversionFn Formula string // contains filtered or unexported fields }
func ResolveConversion ¶
func ResolveConversion(from, to Unit) (cpath []Conversion, err error)
ResolveConversion resolves a path of one or more Conversions between two units
func (Conversion) From ¶
func (c Conversion) From() string
func (Conversion) String ¶
func (c Conversion) String() string
String representation of conversion formula
type ConversionFn ¶
type FmtOptions ¶
type Unit ¶
type Unit struct { Name string Symbol string Quantity string // contains filtered or unexported fields }
func Atto ¶
func Atto(b Unit, o ...UnitOption) Unit
func Centi ¶
func Centi(b Unit, o ...UnitOption) Unit
func Deca ¶
func Deca(b Unit, o ...UnitOption) Unit
func Deci ¶
func Deci(b Unit, o ...UnitOption) Unit
func Exa ¶
func Exa(b Unit, o ...UnitOption) Unit
Magnitude prefix methods create and return a new Unit, while automatically registering conversions to and from the provided base Unit
func Femto ¶
func Femto(b Unit, o ...UnitOption) Unit
func Giga ¶
func Giga(b Unit, o ...UnitOption) Unit
func Hecto ¶
func Hecto(b Unit, o ...UnitOption) Unit
func Kilo ¶
func Kilo(b Unit, o ...UnitOption) Unit
func Mega ¶
func Mega(b Unit, o ...UnitOption) Unit
func Micro ¶
func Micro(b Unit, o ...UnitOption) Unit
func Milli ¶
func Milli(b Unit, o ...UnitOption) Unit
func Nano ¶
func Nano(b Unit, o ...UnitOption) Unit
func NewUnit ¶
func NewUnit(name, symbol string, opts ...UnitOption) Unit
NewUnit registers a new Unit within the package, returning the newly created Unit
func Peta ¶
func Peta(b Unit, o ...UnitOption) Unit
func Pico ¶
func Pico(b Unit, o ...UnitOption) Unit
func Tera ¶
func Tera(b Unit, o ...UnitOption) Unit
type UnitOption ¶
Option that may be passed to NewUnit
func UnitOptionAliases ¶
func UnitOptionAliases(a ...string) UnitOption
Additional names, spellings, or symbols that this unit may be referred to as
func UnitOptionPlural ¶
func UnitOptionPlural(s string) UnitOption
Either "none", "auto", or a custom plural unit name "none" - labels will use the unmodified unit name in a plural context "auto" - labels for this unit will be created with a plural suffix when appropriate (default)
func UnitOptionQuantity ¶
func UnitOptionQuantity(s string) UnitOption
Set a quantity label for which this Unit belongs
func UnitOptionSystem ¶
func UnitOptionSystem(s string) UnitOption
Set a system of units for which this Unit belongs
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
func ConvertFloat ¶
ConvertFloat converts a provided float from one Unit to another
func MustConvertFloat ¶
MustConvertFloat converts a provided float from one Unit to another, panicking on error
func (Value) Fmt ¶
func (v Value) Fmt(opts FmtOptions) string
func (Value) MustConvert ¶
MustConvert converts this Value to another Unit, panicking on error