package unit
import "gonum.org/v1/gonum/unit"
Package unit provides a set of types and constants that facilitate the use of the International System of Units (SI).
The unit package provides two main functionalities: compile-time type-safe base SI units and common derived units; and a system for dynamically extensible user-defined units.
Static SI units
This package provides a number of types representing either an SI base unit or a common combination of base units, named for the physical quantity it represents (Length, Mass, Pressure, etc.). Each type is defined from float64. The value of the float64 represents the quantity of that unit as expressed in SI base units (kilogram, metre, Pascal, etc.). For example,
height := 1.6 * unit.Metre acc := unit.Acceleration(9.8)
creates a variable named 'height' with a value of 1.6 metres, and a variable named 'acc' with a value of 9.8 metres per second squared. These types can be used to add compile-time safety to code. For example,
func unitVolume(t unit.Temperature, p unit.Pressure) unit.Volume { ... } func main(){ t := 300 * unit.Kelvin p := 500 * unit.Kilo * unit.Pascal v := unitVolume(p, t) // compile-time error }
gives a compile-time error (temperature type does not match pressure type) while the corresponding code using float64 runs without error.
func float64Volume(temperature, pressure float64) float64 { ... } func main(){ t := 300.0 // Kelvin p := 500000.0 // Pascals v := float64Volume(p, t) // no error }
Many types have constants defined representing named SI units (Metre, Kilogram, etc. ) or SI derived units (Pascal, Hz, etc.). The unit package additionally provides untyped constants for SI prefixes, so the following are all equivalent.
l := 0.001 * unit.Metre k := 1 * unit.Milli * unit.Metre j := unit.Length(0.001)
Additional SI-derived static units can also be defined by adding types that satisfy the Uniter interface described below.
Dynamic user-extensible unit system
The unit package also provides the Unit type, a representation of a general dimensional value. Unit can be used to help prevent errors of dimensionality when multiplying or dividing dimensional numbers defined at run time. New variables of type Unit can be created with the New function and the Dimensions map. For example, the code
rate := unit.New(1 * unit.Milli, Dimensions{MoleDim: 1, TimeDim: -1})
creates a variable "rate" which has a value of 1e-3 mol/s. Methods of unit can be used to modify this value, for example:
rate.Mul(1 * unit.Centi * unit.Metre).Div(1 * unit.Milli * unit.Volt)
To convert the unit back into a typed float64 value, the From methods of the dimensional types should be used. From will return an error if the dimensions do not match.
var energy unit.Energy err := energy.From(acc)
Domain-specific problems may need custom dimensions, and for this purpose NewDimension should be used to help avoid accidental overlap between packages. For example, results from a blood test may be measured in "White blood cells per slide". In this case, NewDimension should be used to create a 'WhiteBloodCell' dimension. NewDimension takes in a string which will be used for printing that dimension, and will return a unique dimension number.
wbc := unit.NewDimension("WhiteBloodCell")
NewDimension should not be used, however, to create the unit of 'Slide', because in this case slide is just a measurement of liquid volume. Instead, a constant could be defined.
const Slide unit.Volume = 0.1 * unit.Micro * unit.Litre
Note that unit cannot catch all errors related to dimensionality. Different physical ideas are sometimes expressed with the same dimensions and unit is incapable of catching these mismatches. For example, energy and torque are both expressed as force times distance (Newton-metres in SI), but it is wrong to say that a torque of 10 N·m is the same as 10 J, even though the dimensions agree. Despite this, using the defined types to represent units can help to catch errors at compile-time. For example, using unit.Torque allows you to define a statically typed function like so
func LeverLength(apply unit.Force, want unit.Torque) unit.Length { return unit.Length(float64(want)/float64(apply)) }
This will prevent an energy value being provided to LeverLength in place
of a torque value.
Code:play
Output:Example (Horsepower)¶
package main
import (
"fmt"
"gonum.org/v1/gonum/unit"
)
func main() {
// One mechanical horsepower ≡ 33,000 ft-lbf/min.
foot := unit.Length(0.3048)
pound := unit.Mass(0.45359237)
gravity := unit.Acceleration(9.80665)
poundforce := pound.Unit().Mul(gravity)
hp := ((33000 * foot).Unit().Mul(poundforce)).Div(unit.Minute)
fmt.Println("1 hp =", hp)
watt := unit.Power(1)
fmt.Println("W is equivalent to hp?", unit.DimensionsMatch(hp, watt))
}
1 hp = 745.6998715822701 kg m^2 s^-3
W is equivalent to hp? true
Index ¶
- Constants
- func DimensionsMatch(a, b Uniter) bool
- func SymbolExists(symbol string) bool
- type AbsorbedRadioactiveDose
- func (a AbsorbedRadioactiveDose) AbsorbedRadioactiveDose() AbsorbedRadioactiveDose
- func (a AbsorbedRadioactiveDose) Format(fs fmt.State, c rune)
- func (a *AbsorbedRadioactiveDose) From(u Uniter) error
- func (a AbsorbedRadioactiveDose) Unit() *Unit
- type Acceleration
- func (a Acceleration) Acceleration() Acceleration
- func (a Acceleration) Format(fs fmt.State, c rune)
- func (a *Acceleration) From(u Uniter) error
- func (a Acceleration) Unit() *Unit
- type Angle
- func (a Angle) Angle() Angle
- func (a Angle) Format(fs fmt.State, c rune)
- func (a *Angle) From(u Uniter) error
- func (a Angle) Unit() *Unit
- type Area
- func (a Area) Area() Area
- func (a Area) Format(fs fmt.State, c rune)
- func (a *Area) From(u Uniter) error
- func (a Area) Unit() *Unit
- type Capacitance
- func (cp Capacitance) Capacitance() Capacitance
- func (cp Capacitance) Format(fs fmt.State, c rune)
- func (cp *Capacitance) From(u Uniter) error
- func (cp Capacitance) Unit() *Unit
- type Charge
- func (ch Charge) Charge() Charge
- func (ch Charge) Format(fs fmt.State, c rune)
- func (ch *Charge) From(u Uniter) error
- func (ch Charge) Unit() *Unit
- type Conductance
- func (co Conductance) Conductance() Conductance
- func (co Conductance) Format(fs fmt.State, c rune)
- func (co *Conductance) From(u Uniter) error
- func (co Conductance) Unit() *Unit
- type Current
- func (i Current) Current() Current
- func (i Current) Format(fs fmt.State, c rune)
- func (i *Current) From(u Uniter) error
- func (i Current) Unit() *Unit
- type Dimension
- type Dimensions
- type Dimless
- func (d Dimless) Dimless() Dimless
- func (d Dimless) Format(fs fmt.State, c rune)
- func (d *Dimless) From(u Uniter) error
- func (d Dimless) Unit() *Unit
- type Energy
- func (e Energy) Energy() Energy
- func (e Energy) Format(fs fmt.State, c rune)
- func (e *Energy) From(u Uniter) error
- func (e Energy) Unit() *Unit
- type EquivalentRadioactiveDose
- func (a EquivalentRadioactiveDose) EquivalentRadioactiveDose() EquivalentRadioactiveDose
- func (a EquivalentRadioactiveDose) Format(fs fmt.State, c rune)
- func (a *EquivalentRadioactiveDose) From(u Uniter) error
- func (a EquivalentRadioactiveDose) Unit() *Unit
- type Force
- func (f Force) Force() Force
- func (f Force) Format(fs fmt.State, c rune)
- func (f *Force) From(u Uniter) error
- func (f Force) Unit() *Unit
- type Frequency
- func (f Frequency) Format(fs fmt.State, c rune)
- func (f Frequency) Frequency() Frequency
- func (f *Frequency) From(u Uniter) error
- func (f Frequency) Unit() *Unit
- type Inductance
- func (i Inductance) Format(fs fmt.State, c rune)
- func (i *Inductance) From(u Uniter) error
- func (i Inductance) Inductance() Inductance
- func (i Inductance) Unit() *Unit
- type Length
- func (l Length) Format(fs fmt.State, c rune)
- func (l *Length) From(u Uniter) error
- func (l Length) Length() Length
- func (l Length) Unit() *Unit
- type LuminousIntensity
- func (j LuminousIntensity) Format(fs fmt.State, c rune)
- func (j *LuminousIntensity) From(u Uniter) error
- func (j LuminousIntensity) LuminousIntensity() LuminousIntensity
- func (j LuminousIntensity) Unit() *Unit
- type MagneticFlux
- func (m MagneticFlux) Format(fs fmt.State, c rune)
- func (m *MagneticFlux) From(u Uniter) error
- func (m MagneticFlux) MagneticFlux() MagneticFlux
- func (m MagneticFlux) Unit() *Unit
- type MagneticFluxDensity
- func (m MagneticFluxDensity) Format(fs fmt.State, c rune)
- func (m *MagneticFluxDensity) From(u Uniter) error
- func (m MagneticFluxDensity) MagneticFluxDensity() MagneticFluxDensity
- func (m MagneticFluxDensity) Unit() *Unit
- type Mass
- func (m Mass) Format(fs fmt.State, c rune)
- func (m *Mass) From(u Uniter) error
- func (m Mass) Mass() Mass
- func (m Mass) Unit() *Unit
- type Mole
- func (n Mole) Format(fs fmt.State, c rune)
- func (n *Mole) From(u Uniter) error
- func (n Mole) Mole() Mole
- func (n Mole) Unit() *Unit
- type Power
- func (pw Power) Format(fs fmt.State, c rune)
- func (pw *Power) From(u Uniter) error
- func (pw Power) Power() Power
- func (pw Power) Unit() *Unit
- type Pressure
- func (pr Pressure) Format(fs fmt.State, c rune)
- func (pr *Pressure) From(u Uniter) error
- func (pr Pressure) Pressure() Pressure
- func (pr Pressure) Unit() *Unit
- type Radioactivity
- func (r Radioactivity) Format(fs fmt.State, c rune)
- func (r *Radioactivity) From(u Uniter) error
- func (r Radioactivity) Radioactivity() Radioactivity
- func (r Radioactivity) Unit() *Unit
- type Resistance
- func (r Resistance) Format(fs fmt.State, c rune)
- func (r *Resistance) From(u Uniter) error
- func (r Resistance) Resistance() Resistance
- func (r Resistance) Unit() *Unit
- type Temperature
- func (t Temperature) Format(fs fmt.State, c rune)
- func (t *Temperature) From(u Uniter) error
- func (t Temperature) Temperature() Temperature
- func (t Temperature) Unit() *Unit
- type Time
- func (t Time) Format(fs fmt.State, c rune)
- func (t *Time) From(u Uniter) error
- func (t Time) Time() Time
- func (t Time) Unit() *Unit
- type Torque
- func (t Torque) Format(fs fmt.State, c rune)
- func (t *Torque) From(u Uniter) error
- func (t Torque) Torque() Torque
- func (t Torque) Unit() *Unit
- type Unit
- func New(value float64, d Dimensions) *Unit
- func (u *Unit) Add(uniter Uniter) *Unit
- func (u *Unit) Copy() *Unit
- func (u *Unit) Dimensions() Dimensions
- func (u *Unit) Div(uniter Uniter) *Unit
- func (u *Unit) Format(fs fmt.State, c rune)
- func (u *Unit) Mul(uniter Uniter) *Unit
- func (u *Unit) SetValue(v float64)
- func (u *Unit) Unit() *Unit
- func (u *Unit) Value() float64
- type Uniter
- type Velocity
- func (v Velocity) Format(fs fmt.State, c rune)
- func (v *Velocity) From(u Uniter) error
- func (v Velocity) Unit() *Unit
- func (v Velocity) Velocity() Velocity
- type Voltage
- func (v Voltage) Format(fs fmt.State, c rune)
- func (v *Voltage) From(u Uniter) error
- func (v Voltage) Unit() *Unit
- func (v Voltage) Voltage() Voltage
- type Volume
Examples ¶
Constants ¶
const ( Yotta = 1e24 Zetta = 1e21 Exa = 1e18 Peta = 1e15 Tera = 1e12 Giga = 1e9 Mega = 1e6 Kilo = 1e3 Hecto = 1e2 Deca = 1e1 Deci = 1e-1 Centi = 1e-2 Milli = 1e-3 Micro = 1e-6 Nano = 1e-9 Pico = 1e-12 Femto = 1e-15 Atto = 1e-18 Zepto = 1e-21 Yocto = 1e-24 )
const ( Second Time = 1 Minute = 60 * Second Hour = 60 * Minute )
Functions ¶
func DimensionsMatch ¶
DimensionsMatch checks if the dimensions of two Uniters are the same.
func SymbolExists ¶
SymbolExists returns whether the given symbol is already in use.
Types ¶
type AbsorbedRadioactiveDose ¶
type AbsorbedRadioactiveDose float64
AbsorbedRadioactiveDose is a measure of absorbed dose of ionizing radiation in grays.
const Gray AbsorbedRadioactiveDose = 1
func (AbsorbedRadioactiveDose) AbsorbedRadioactiveDose ¶
func (a AbsorbedRadioactiveDose) AbsorbedRadioactiveDose() AbsorbedRadioactiveDose
AbsorbedRadioactiveDose allows AbsorbedRadioactiveDose to implement a AbsorbedRadioactiveDoseer interface.
func (AbsorbedRadioactiveDose) Format ¶
func (a AbsorbedRadioactiveDose) Format(fs fmt.State, c rune)
func (*AbsorbedRadioactiveDose) From ¶
func (a *AbsorbedRadioactiveDose) From(u Uniter) error
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (AbsorbedRadioactiveDose) Unit ¶
func (a AbsorbedRadioactiveDose) Unit() *Unit
Unit converts the AbsorbedRadioactiveDose to a *Unit.
type Acceleration ¶
type Acceleration float64
Acceleration represents an acceleration in metres per second squared.
func (Acceleration) Acceleration ¶
func (a Acceleration) Acceleration() Acceleration
Acceleration allows Acceleration to implement a Accelerationer interface.
func (Acceleration) Format ¶
func (a Acceleration) Format(fs fmt.State, c rune)
func (*Acceleration) From ¶
func (a *Acceleration) From(u Uniter) error
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Acceleration) Unit ¶
func (a Acceleration) Unit() *Unit
Unit converts the Acceleration to a *Unit.
type Angle ¶
type Angle float64
Angle represents an angle in radians.
const Rad Angle = 1
func (Angle) Angle ¶
Angle allows Angle to implement a Angleer interface.
func (Angle) Format ¶
func (*Angle) From ¶
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Angle) Unit ¶
Unit converts the Angle to a *Unit.
type Area ¶
type Area float64
Area represents an area in square metres.
func (Area) Area ¶
Area allows Area to implement a Areaer interface.
func (Area) Format ¶
func (*Area) From ¶
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Area) Unit ¶
Unit converts the Area to a *Unit.
type Capacitance ¶
type Capacitance float64
Capacitance represents an electrical capacitance in Farads.
const Farad Capacitance = 1
func (Capacitance) Capacitance ¶
func (cp Capacitance) Capacitance() Capacitance
Capacitance allows Capacitance to implement a Capacitancer interface.
func (Capacitance) Format ¶
func (cp Capacitance) Format(fs fmt.State, c rune)
func (*Capacitance) From ¶
func (cp *Capacitance) From(u Uniter) error
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Capacitance) Unit ¶
func (cp Capacitance) Unit() *Unit
Unit converts the Capacitance to a *Unit.
type Charge ¶
type Charge float64
Charge represents an electric charge in Coulombs.
const Coulomb Charge = 1
func (Charge) Charge ¶
Charge allows Charge to implement a Charger interface.
func (Charge) Format ¶
func (*Charge) From ¶
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Charge) Unit ¶
Unit converts the Charge to a *Unit.
type Conductance ¶
type Conductance float64
Conductance represents an electrical conductance in Siemens.
const Siemens Conductance = 1
func (Conductance) Conductance ¶
func (co Conductance) Conductance() Conductance
Conductance allows Conductance to implement a Conductancer interface.
func (Conductance) Format ¶
func (co Conductance) Format(fs fmt.State, c rune)
func (*Conductance) From ¶
func (co *Conductance) From(u Uniter) error
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Conductance) Unit ¶
func (co Conductance) Unit() *Unit
Unit converts the Conductance to a *Unit.
type Current ¶
type Current float64
Current represents a current in Amperes.
const Ampere Current = 1
func (Current) Current ¶
Current allows Current to implement a Currenter interface.
func (Current) Format ¶
func (*Current) From ¶
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Current) Unit ¶
Unit converts the Current to a *Unit.
type Dimension ¶
type Dimension int
Dimension is a type representing an SI base dimension or a distinct orthogonal dimension. Non-SI dimensions can be created using the NewDimension function, typically within an init function.
const ( CurrentDim Dimension LengthDim LuminousIntensityDim MassDim MoleDim TemperatureDim TimeDim // Other common SI Dimensions AngleDim // e.g. radians )
func NewDimension ¶
NewDimension creates a new orthogonal dimension with the given symbol, and returns the value of that dimension. The input symbol must not overlap with any of the any of the SI base units or other symbols of common use in SI ("kg", "J", etc.), and must not overlap with any other dimensions created by calls to NewDimension. The SymbolExists function can check if the symbol exists. NewDimension will panic if the input symbol matches an existing symbol.
NewDimension should only be called for unit types that are actually orthogonal
to the base dimensions defined in this package. See the package-level
documentation for further explanation.
Code:play
Output:Example¶
package main
import (
"fmt"
"gonum.org/v1/gonum/unit"
)
func main() {
// Create a "trees" dimension
// Typically, this should be used within an init function
treeDim := unit.NewDimension("tree")
countPerArea := unit.New(0.1, unit.Dimensions{treeDim: 1, unit.LengthDim: -2})
fmt.Println(countPerArea)
}
0.1 tree m^-2
func (Dimension) String ¶
String returns the string for the dimension.
type Dimensions ¶
Dimensions represent the dimensionality of the unit in powers of that dimension. If a key is not present, the power of that dimension is zero. Dimensions is used in conjunction with New.
func (Dimensions) String ¶
func (d Dimensions) String() string
type Dimless ¶
type Dimless float64
Dimless represents a dimensionless constant.
func (Dimless) Dimless ¶
Dimless allows Dimless to implement a Dimlesser interface.
func (Dimless) Format ¶
func (*Dimless) From ¶
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Dimless) Unit ¶
Unit converts the Dimless to a *Unit.
type Energy ¶
type Energy float64
Energy represents a quantity of energy in Joules.
const Joule Energy = 1
func (Energy) Energy ¶
Energy allows Energy to implement a Energyer interface.
func (Energy) Format ¶
func (*Energy) From ¶
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Energy) Unit ¶
Unit converts the Energy to a *Unit.
type EquivalentRadioactiveDose ¶
type EquivalentRadioactiveDose float64
EquivalentRadioactiveDose is a measure of equivalent dose of ionizing radiation in sieverts.
const Sievert EquivalentRadioactiveDose = 1
func (EquivalentRadioactiveDose) EquivalentRadioactiveDose ¶
func (a EquivalentRadioactiveDose) EquivalentRadioactiveDose() EquivalentRadioactiveDose
EquivalentRadioactiveDose allows EquivalentRadioactiveDose to implement a EquivalentRadioactiveDoseer interface.
func (EquivalentRadioactiveDose) Format ¶
func (a EquivalentRadioactiveDose) Format(fs fmt.State, c rune)
func (*EquivalentRadioactiveDose) From ¶
func (a *EquivalentRadioactiveDose) From(u Uniter) error
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (EquivalentRadioactiveDose) Unit ¶
func (a EquivalentRadioactiveDose) Unit() *Unit
Unit converts the EquivalentRadioactiveDose to a *Unit.
type Force ¶
type Force float64
Force represents a force in Newtons.
const Newton Force = 1
func (Force) Force ¶
Force allows Force to implement a Forcer interface.
func (Force) Format ¶
func (*Force) From ¶
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Force) Unit ¶
Unit converts the Force to a *Unit.
type Frequency ¶
type Frequency float64
Frequency represents a frequency in Hertz.
const Hertz Frequency = 1
func (Frequency) Format ¶
func (Frequency) Frequency ¶
Frequency allows Frequency to implement a Frequencyer interface.
func (*Frequency) From ¶
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Frequency) Unit ¶
Unit converts the Frequency to a *Unit.
type Inductance ¶
type Inductance float64
Inductance represents an electrical inductance in Henry.
const Henry Inductance = 1
func (Inductance) Format ¶
func (i Inductance) Format(fs fmt.State, c rune)
func (*Inductance) From ¶
func (i *Inductance) From(u Uniter) error
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Inductance) Inductance ¶
func (i Inductance) Inductance() Inductance
Inductance allows Inductance to implement a Inductancer interface.
func (Inductance) Unit ¶
func (i Inductance) Unit() *Unit
Unit converts the Inductance to a *Unit.
type Length ¶
type Length float64
Length represents a length in metres.
const Metre Length = 1
func (Length) Format ¶
func (*Length) From ¶
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Length) Length ¶
Length allows Length to implement a Lengther interface.
func (Length) Unit ¶
Unit converts the Length to a *Unit.
type LuminousIntensity ¶
type LuminousIntensity float64
Candela represents a luminous intensity in candela.
const Candela LuminousIntensity = 1
func (LuminousIntensity) Format ¶
func (j LuminousIntensity) Format(fs fmt.State, c rune)
func (*LuminousIntensity) From ¶
func (j *LuminousIntensity) From(u Uniter) error
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (LuminousIntensity) LuminousIntensity ¶
func (j LuminousIntensity) LuminousIntensity() LuminousIntensity
LuminousIntensity allows LuminousIntensity to implement a LuminousIntensityer interface.
func (LuminousIntensity) Unit ¶
func (j LuminousIntensity) Unit() *Unit
Unit converts the LuminousIntensity to a *Unit.
type MagneticFlux ¶
type MagneticFlux float64
MagneticFlux represents a magnetic flux in Weber.
const Weber MagneticFlux = 1
func (MagneticFlux) Format ¶
func (m MagneticFlux) Format(fs fmt.State, c rune)
func (*MagneticFlux) From ¶
func (m *MagneticFlux) From(u Uniter) error
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (MagneticFlux) MagneticFlux ¶
func (m MagneticFlux) MagneticFlux() MagneticFlux
MagneticFlux allows MagneticFlux to implement a MagneticFluxer interface.
func (MagneticFlux) Unit ¶
func (m MagneticFlux) Unit() *Unit
Unit converts the MagneticFlux to a *Unit.
type MagneticFluxDensity ¶
type MagneticFluxDensity float64
MagneticFluxDensity represents a magnetic flux density in Tesla.
const Tesla MagneticFluxDensity = 1
func (MagneticFluxDensity) Format ¶
func (m MagneticFluxDensity) Format(fs fmt.State, c rune)
func (*MagneticFluxDensity) From ¶
func (m *MagneticFluxDensity) From(u Uniter) error
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (MagneticFluxDensity) MagneticFluxDensity ¶
func (m MagneticFluxDensity) MagneticFluxDensity() MagneticFluxDensity
MagneticFluxDensity allows MagneticFluxDensity to implement a MagneticFluxDensityer interface.
func (MagneticFluxDensity) Unit ¶
func (m MagneticFluxDensity) Unit() *Unit
Unit converts the MagneticFluxDensity to a *Unit.
type Mass ¶
type Mass float64
Mass represents a mass in kilograms.
const ( Gram Mass = 1e-3 Kilogram = Kilo * Gram )
func (Mass) Format ¶
func (*Mass) From ¶
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Mass) Mass ¶
Mass allows Mass to implement a Masser interface.
func (Mass) Unit ¶
Unit converts the Mass to a *Unit.
type Mole ¶
type Mole float64
Mole represents an amount in moles.
const Mol Mole = 1
func (Mole) Format ¶
func (*Mole) From ¶
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Mole) Mole ¶
Mole allows Mole to implement a Moleer interface.
func (Mole) Unit ¶
Unit converts the Mole to a *Unit.
type Power ¶
type Power float64
Power represents a power in Watts.
const Watt Power = 1
func (Power) Format ¶
func (*Power) From ¶
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Power) Power ¶
Power allows Power to implement a Powerer interface.
func (Power) Unit ¶
Unit converts the Power to a *Unit.
type Pressure ¶
type Pressure float64
Pressure represents a pressure in Pascals.
const Pascal Pressure = 1
func (Pressure) Format ¶
func (*Pressure) From ¶
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Pressure) Pressure ¶
Pressure allows Pressure to implement a Pressurer interface.
func (Pressure) Unit ¶
Unit converts the Pressure to a *Unit.
type Radioactivity ¶
type Radioactivity float64
Radioactivity represents a rate of radioactive decay in becquerels.
const Becquerel Radioactivity = 1
func (Radioactivity) Format ¶
func (r Radioactivity) Format(fs fmt.State, c rune)
func (*Radioactivity) From ¶
func (r *Radioactivity) From(u Uniter) error
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Radioactivity) Radioactivity ¶
func (r Radioactivity) Radioactivity() Radioactivity
Radioactivity allows Radioactivity to implement a Radioactivityer interface.
func (Radioactivity) Unit ¶
func (r Radioactivity) Unit() *Unit
Unit converts the Radioactivity to a *Unit.
type Resistance ¶
type Resistance float64
Resistance represents an electrical resistance, impedance or reactance in Ohms.
const Ohm Resistance = 1
func (Resistance) Format ¶
func (r Resistance) Format(fs fmt.State, c rune)
func (*Resistance) From ¶
func (r *Resistance) From(u Uniter) error
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Resistance) Resistance ¶
func (r Resistance) Resistance() Resistance
Resistance allows Resistance to implement a Resistancer interface.
func (Resistance) Unit ¶
func (r Resistance) Unit() *Unit
Unit converts the Resistance to a *Unit.
type Temperature ¶
type Temperature float64
Temperature represents a temperature in Kelvin.
const Kelvin Temperature = 1
func (Temperature) Format ¶
func (t Temperature) Format(fs fmt.State, c rune)
func (*Temperature) From ¶
func (t *Temperature) From(u Uniter) error
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Temperature) Temperature ¶
func (t Temperature) Temperature() Temperature
Temperature allows Temperature to implement a Temperaturer interface.
func (Temperature) Unit ¶
func (t Temperature) Unit() *Unit
Unit converts the Temperature to a *Unit.
type Time ¶
type Time float64
Time represents a duration in seconds.
func (Time) Format ¶
func (*Time) From ¶
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Time) Time ¶
Time allows Time to implement a Timer interface.
func (Time) Unit ¶
Unit converts the Time to a *Unit.
type Torque ¶
type Torque float64
Torque represents a torque in Newton metres.
const Newtonmetre Torque = 1
func (Torque) Format ¶
func (*Torque) From ¶
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Torque) Torque ¶
Torque allows Torque to implement a Torquer interface.
func (Torque) Unit ¶
Unit converts the Torque to a *Unit.
type Unit ¶
type Unit struct {
// contains filtered or unexported fields
}
Unit represents a dimensional value. The dimensions will typically be in SI units, but can also include dimensions created with NewDimension. The Unit type is most useful for ensuring dimensional consistency when manipulating types with different units, for example, by multiplying an acceleration with a mass to get a force. See the package documentation for further explanation.
func New ¶
func New(value float64, d Dimensions) *Unit
New creates a new variable of type Unit which has the value and dimensions specified by the inputs. The built-in dimensions are always in SI units (metres, kilograms, etc.).
func (*Unit) Add ¶
Add adds the function argument to the receiver. Panics if the units of the receiver and the argument don't match.
func (*Unit) Copy ¶
Copy returns a copy of the Unit that can be mutated without the change being reflected in the original value.
func (*Unit) Dimensions ¶
func (u *Unit) Dimensions() Dimensions
Dimensions returns a copy of the dimensions of the unit.
func (*Unit) Div ¶
Div divides the receiver by the argument changing the dimensions of the receiver as appropriate.
func (*Unit) Format ¶
Format makes Unit satisfy the fmt.Formatter interface. The unit is formatted with dimensions appended. If the power of the dimension is not zero or one, symbol^power is appended, if the power is one, just the symbol is appended and if the power is zero, nothing is appended. Dimensions are appended in order by symbol name with positive powers ahead of negative powers.
func (*Unit) Mul ¶
Mul multiply the receiver by the input changing the dimensions of the receiver as appropriate. The input is not changed.
func (*Unit) SetValue ¶
SetValue sets the value of the unit.
func (*Unit) Unit ¶
Unit implements the Uniter interface, returning the receiver. If a copy of the receiver is required, use the Copy method.
func (*Unit) Value ¶
Value return the raw value of the unit as a float64. Use of this method is, in general, not recommended, though it can be useful for printing. Instead, the From method of a specific dimension should be used to guarantee dimension consistency.
type Uniter ¶
type Uniter interface { Unit() *Unit }
Uniter is a type that can be converted to a Unit.
type Velocity ¶
type Velocity float64
Velocity represents a velocity in metres per second.
func (Velocity) Format ¶
func (*Velocity) From ¶
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Velocity) Unit ¶
Unit converts the Velocity to a *Unit.
func (Velocity) Velocity ¶
Velocity allows Velocity to implement a Velocityer interface.
type Voltage ¶
type Voltage float64
Voltage represents a voltage in Volts.
const Volt Voltage = 1
func (Voltage) Format ¶
func (*Voltage) From ¶
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Voltage) Unit ¶
Unit converts the Voltage to a *Unit.
func (Voltage) Voltage ¶
Voltage allows Voltage to implement a Voltager interface.
type Volume ¶
type Volume float64
Volume represents a volume in cubic metres.
const Litre Volume = 1e-3
func (Volume) Format ¶
func (*Volume) From ¶
From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.
func (Volume) Unit ¶
Unit converts the Volume to a *Unit.
func (Volume) Volume ¶
Volume allows Volume to implement a Volumeer interface.
Source Files ¶
absorbedradioactivedose.go acceleration.go angle.go area.go capacitance.go charge.go conductance.go current.go dimless.go doc.go energy.go equivalentradioactivedose.go force.go frequency.go inductance.go length.go luminousintensity.go magneticflux.go magneticfluxdensity.go mass.go mole.go power.go prefixes.go pressure.go radioactivity.go resistance.go temperature.go time.go torque.go unittype.go velocity.go voltage.go volume.go
Directories ¶
Path | Synopsis |
---|---|
unit/constant | Package constant provides fundamental constants satisfying unit.Uniter. |
- Version
- v0.15.1 (latest)
- Published
- Aug 16, 2024
- Platform
- linux/amd64
- Imports
- 8 packages
- Last checked
- 12 hours ago –
Tools for package owners.