gonum: gonum.org/v1/gonum/unit Index | Examples | Files | Directories

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 a 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:

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

Output:

1 hp = 745.6998715822701 kg m^2 s^-3
W is equivalent to hp? true

Index

Examples

Package 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

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
)

func DimensionsMatch Uses

func DimensionsMatch(a, b Uniter) bool

DimensionsMatch checks if the dimensions of two Uniters are the same.

func SymbolExists Uses

func SymbolExists(symbol string) bool

SymbolExists returns whether the given symbol is already in use.

type AbsorbedRadioactiveDose Uses

type AbsorbedRadioactiveDose float64

AbsorbedRadioactiveDose is a measure of absorbed dose of ionizing radiation in grays.

const Gray AbsorbedRadioactiveDose = 1

func (AbsorbedRadioactiveDose) AbsorbedRadioactiveDose Uses

func (a AbsorbedRadioactiveDose) AbsorbedRadioactiveDose() AbsorbedRadioactiveDose

AbsorbedRadioactiveDose allows AbsorbedRadioactiveDose to implement a AbsorbedRadioactiveDoseer interface

func (AbsorbedRadioactiveDose) Format Uses

func (a AbsorbedRadioactiveDose) Format(fs fmt.State, c rune)

func (*AbsorbedRadioactiveDose) From Uses

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 Uses

func (a AbsorbedRadioactiveDose) Unit() *Unit

Unit converts the AbsorbedRadioactiveDose to a *Unit

type Acceleration Uses

type Acceleration float64

Acceleration represents an acceleration in metres per second squared.

func (Acceleration) Acceleration Uses

func (a Acceleration) Acceleration() Acceleration

Acceleration allows Acceleration to implement a Accelerationer interface

func (Acceleration) Format Uses

func (a Acceleration) Format(fs fmt.State, c rune)

func (*Acceleration) From Uses

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 Uses

func (a Acceleration) Unit() *Unit

Unit converts the Acceleration to a *Unit

type Angle Uses

type Angle float64

Angle represents an angle in radians.

const Rad Angle = 1

func (Angle) Angle Uses

func (a Angle) Angle() Angle

Angle allows Angle to implement a Angleer interface

func (Angle) Format Uses

func (a Angle) Format(fs fmt.State, c rune)

func (*Angle) From Uses

func (a *Angle) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension

func (Angle) Unit Uses

func (a Angle) Unit() *Unit

Unit converts the Angle to a *Unit

type Area Uses

type Area float64

Area represents an area in square metres.

func (Area) Area Uses

func (a Area) Area() Area

Area allows Area to implement a Areaer interface

func (Area) Format Uses

func (a Area) Format(fs fmt.State, c rune)

func (*Area) From Uses

func (a *Area) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension

func (Area) Unit Uses

func (a Area) Unit() *Unit

Unit converts the Area to a *Unit

type Capacitance Uses

type Capacitance float64

Capacitance represents an electrical capacitance in Farads.

const Farad Capacitance = 1

func (Capacitance) Capacitance Uses

func (cp Capacitance) Capacitance() Capacitance

Capacitance allows Capacitance to implement a Capacitancer interface

func (Capacitance) Format Uses

func (cp Capacitance) Format(fs fmt.State, c rune)

func (*Capacitance) From Uses

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 Uses

func (cp Capacitance) Unit() *Unit

Unit converts the Capacitance to a *Unit

type Charge Uses

type Charge float64

Charge represents an electric charge in Coulombs.

const Coulomb Charge = 1

func (Charge) Charge Uses

func (ch Charge) Charge() Charge

Charge allows Charge to implement a Charger interface

func (Charge) Format Uses

func (ch Charge) Format(fs fmt.State, c rune)

func (*Charge) From Uses

func (ch *Charge) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension

func (Charge) Unit Uses

func (ch Charge) Unit() *Unit

Unit converts the Charge to a *Unit

type Conductance Uses

type Conductance float64

Conductance represents an electrical conductance in Siemens.

const Siemens Conductance = 1

func (Conductance) Conductance Uses

func (co Conductance) Conductance() Conductance

Conductance allows Conductance to implement a Conductancer interface

func (Conductance) Format Uses

func (co Conductance) Format(fs fmt.State, c rune)

func (*Conductance) From Uses

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 Uses

func (co Conductance) Unit() *Unit

Unit converts the Conductance to a *Unit

type Current Uses

type Current float64

Current represents a current in Amperes.

const Ampere Current = 1

func (Current) Current Uses

func (i Current) Current() Current

Current allows Current to implement a Currenter interface

func (Current) Format Uses

func (i Current) Format(fs fmt.State, c rune)

func (*Current) From Uses

func (i *Current) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension

func (Current) Unit Uses

func (i Current) Unit() *Unit

Unit converts the Current to a *Unit

type Dimension Uses

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 Uses

func NewDimension(symbol string) Dimension

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:

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

Output:

0.1 tree m^-2

func (Dimension) String Uses

func (d Dimension) String() string

String returns the string for the dimension.

type Dimensions Uses

type Dimensions map[Dimension]int

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 Uses

func (d Dimensions) String() string

type Dimless Uses

type Dimless float64

Dimless represents a dimensionless constant

const (
    One Dimless = 1.0
)

func (Dimless) Dimless Uses

func (d Dimless) Dimless() Dimless

Dimless allows Dimless to implement a Dimlesser interface

func (Dimless) Format Uses

func (d Dimless) Format(fs fmt.State, c rune)

func (*Dimless) From Uses

func (d *Dimless) From(u *Unit) error

From converts the unit to a dimless. Returns an error if there is a mismatch in dimension

func (Dimless) Unit Uses

func (d Dimless) Unit() *Unit

Unit converts the Dimless to a unit

type Energy Uses

type Energy float64

Energy represents a quantity of energy in Joules.

const Joule Energy = 1

func (Energy) Energy Uses

func (e Energy) Energy() Energy

Energy allows Energy to implement a Energyer interface

func (Energy) Format Uses

func (e Energy) Format(fs fmt.State, c rune)

func (*Energy) From Uses

func (e *Energy) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension

func (Energy) Unit Uses

func (e Energy) Unit() *Unit

Unit converts the Energy to a *Unit

type EquivalentRadioactiveDose Uses

type EquivalentRadioactiveDose float64

EquivalentRadioactiveDose is a measure of equivalent dose of ionizing radiation in sieverts.

const Sievert EquivalentRadioactiveDose = 1

func (EquivalentRadioactiveDose) EquivalentRadioactiveDose Uses

func (a EquivalentRadioactiveDose) EquivalentRadioactiveDose() EquivalentRadioactiveDose

EquivalentRadioactiveDose allows EquivalentRadioactiveDose to implement a EquivalentRadioactiveDoseer interface

func (EquivalentRadioactiveDose) Format Uses

func (a EquivalentRadioactiveDose) Format(fs fmt.State, c rune)

func (*EquivalentRadioactiveDose) From Uses

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 Uses

func (a EquivalentRadioactiveDose) Unit() *Unit

Unit converts the EquivalentRadioactiveDose to a *Unit

type Force Uses

type Force float64

Force represents a force in Newtons.

const Newton Force = 1

func (Force) Force Uses

func (f Force) Force() Force

Force allows Force to implement a Forcer interface

func (Force) Format Uses

func (f Force) Format(fs fmt.State, c rune)

func (*Force) From Uses

func (f *Force) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension

func (Force) Unit Uses

func (f Force) Unit() *Unit

Unit converts the Force to a *Unit

type Frequency Uses

type Frequency float64

Frequency represents a frequency in Hertz.

const Hertz Frequency = 1

func (Frequency) Format Uses

func (f Frequency) Format(fs fmt.State, c rune)

func (Frequency) Frequency Uses

func (f Frequency) Frequency() Frequency

Frequency allows Frequency to implement a Frequencyer interface

func (*Frequency) From Uses

func (f *Frequency) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension

func (Frequency) Unit Uses

func (f Frequency) Unit() *Unit

Unit converts the Frequency to a *Unit

type Inductance Uses

type Inductance float64

Inductance represents an electrical inductance in Henry.

const Henry Inductance = 1

func (Inductance) Format Uses

func (i Inductance) Format(fs fmt.State, c rune)

func (*Inductance) From Uses

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 Uses

func (i Inductance) Inductance() Inductance

Inductance allows Inductance to implement a Inductancer interface

func (Inductance) Unit Uses

func (i Inductance) Unit() *Unit

Unit converts the Inductance to a *Unit

type Length Uses

type Length float64

Length represents a length in metres.

const Metre Length = 1

func (Length) Format Uses

func (l Length) Format(fs fmt.State, c rune)

func (*Length) From Uses

func (l *Length) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension

func (Length) Length Uses

func (l Length) Length() Length

Length allows Length to implement a Lengther interface

func (Length) Unit Uses

func (l Length) Unit() *Unit

Unit converts the Length to a *Unit

type LuminousIntensity Uses

type LuminousIntensity float64

Candela represents a luminous intensity in candela.

const Candela LuminousIntensity = 1

func (LuminousIntensity) Format Uses

func (j LuminousIntensity) Format(fs fmt.State, c rune)

func (*LuminousIntensity) From Uses

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 Uses

func (j LuminousIntensity) LuminousIntensity() LuminousIntensity

LuminousIntensity allows LuminousIntensity to implement a LuminousIntensityer interface

func (LuminousIntensity) Unit Uses

func (j LuminousIntensity) Unit() *Unit

Unit converts the LuminousIntensity to a *Unit

type MagneticFlux Uses

type MagneticFlux float64

MagneticFlux represents a magnetic flux in Weber.

const Weber MagneticFlux = 1

func (MagneticFlux) Format Uses

func (m MagneticFlux) Format(fs fmt.State, c rune)

func (*MagneticFlux) From Uses

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 Uses

func (m MagneticFlux) MagneticFlux() MagneticFlux

MagneticFlux allows MagneticFlux to implement a MagneticFluxer interface

func (MagneticFlux) Unit Uses

func (m MagneticFlux) Unit() *Unit

Unit converts the MagneticFlux to a *Unit

type MagneticFluxDensity Uses

type MagneticFluxDensity float64

MagneticFluxDensity represents a magnetic flux density in Tesla.

const Tesla MagneticFluxDensity = 1

func (MagneticFluxDensity) Format Uses

func (m MagneticFluxDensity) Format(fs fmt.State, c rune)

func (*MagneticFluxDensity) From Uses

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 Uses

func (m MagneticFluxDensity) MagneticFluxDensity() MagneticFluxDensity

MagneticFluxDensity allows MagneticFluxDensity to implement a MagneticFluxDensityer interface

func (MagneticFluxDensity) Unit Uses

func (m MagneticFluxDensity) Unit() *Unit

Unit converts the MagneticFluxDensity to a *Unit

type Mass Uses

type Mass float64

Mass represents a mass in kilograms.

const (
    Gram Mass = 1e-3

    Kilogram = Kilo * Gram
)

func (Mass) Format Uses

func (m Mass) Format(fs fmt.State, c rune)

func (*Mass) From Uses

func (m *Mass) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension

func (Mass) Mass Uses

func (m Mass) Mass() Mass

Mass allows Mass to implement a Masser interface

func (Mass) Unit Uses

func (m Mass) Unit() *Unit

Unit converts the Mass to a *Unit

type Mole Uses

type Mole float64

Mole represents an amount in moles.

const Mol Mole = 1

func (Mole) Format Uses

func (n Mole) Format(fs fmt.State, c rune)

func (*Mole) From Uses

func (n *Mole) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension

func (Mole) Mole Uses

func (n Mole) Mole() Mole

Mole allows Mole to implement a Moleer interface

func (Mole) Unit Uses

func (n Mole) Unit() *Unit

Unit converts the Mole to a *Unit

type Power Uses

type Power float64

Power represents a power in Watts.

const Watt Power = 1

func (Power) Format Uses

func (pw Power) Format(fs fmt.State, c rune)

func (*Power) From Uses

func (pw *Power) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension

func (Power) Power Uses

func (pw Power) Power() Power

Power allows Power to implement a Powerer interface

func (Power) Unit Uses

func (pw Power) Unit() *Unit

Unit converts the Power to a *Unit

type Pressure Uses

type Pressure float64

Pressure represents a pressure in Pascals.

const Pascal Pressure = 1

func (Pressure) Format Uses

func (pr Pressure) Format(fs fmt.State, c rune)

func (*Pressure) From Uses

func (pr *Pressure) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension

func (Pressure) Pressure Uses

func (pr Pressure) Pressure() Pressure

Pressure allows Pressure to implement a Pressurer interface

func (Pressure) Unit Uses

func (pr Pressure) Unit() *Unit

Unit converts the Pressure to a *Unit

type Radioactivity Uses

type Radioactivity float64

Radioactivity represents a rate of radioactive decay in becquerels.

const Becquerel Radioactivity = 1

func (Radioactivity) Format Uses

func (r Radioactivity) Format(fs fmt.State, c rune)

func (*Radioactivity) From Uses

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 Uses

func (r Radioactivity) Radioactivity() Radioactivity

Radioactivity allows Radioactivity to implement a Radioactivityer interface

func (Radioactivity) Unit Uses

func (r Radioactivity) Unit() *Unit

Unit converts the Radioactivity to a *Unit

type Resistance Uses

type Resistance float64

Resistance represents an electrical resistance, impedance or reactance in Ohms.

const Ohm Resistance = 1

func (Resistance) Format Uses

func (r Resistance) Format(fs fmt.State, c rune)

func (*Resistance) From Uses

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 Uses

func (r Resistance) Resistance() Resistance

Resistance allows Resistance to implement a Resistancer interface

func (Resistance) Unit Uses

func (r Resistance) Unit() *Unit

Unit converts the Resistance to a *Unit

type Temperature Uses

type Temperature float64

Temperature represents a temperature in Kelvin.

const Kelvin Temperature = 1

func (Temperature) Format Uses

func (t Temperature) Format(fs fmt.State, c rune)

func (*Temperature) From Uses

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 Uses

func (t Temperature) Temperature() Temperature

Temperature allows Temperature to implement a Temperaturer interface

func (Temperature) Unit Uses

func (t Temperature) Unit() *Unit

Unit converts the Temperature to a *Unit

type Time Uses

type Time float64

Time represents a duration in seconds.

func (Time) Format Uses

func (t Time) Format(fs fmt.State, c rune)

func (*Time) From Uses

func (t *Time) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension

func (Time) Time Uses

func (t Time) Time() Time

Time allows Time to implement a Timer interface

func (Time) Unit Uses

func (t Time) Unit() *Unit

Unit converts the Time to a *Unit

type Torque Uses

type Torque float64

Torque represents a torque in Newton metres.

const Newtonmetre Torque = 1

func (Torque) Format Uses

func (t Torque) Format(fs fmt.State, c rune)

func (*Torque) From Uses

func (t *Torque) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension

func (Torque) Torque Uses

func (t Torque) Torque() Torque

Torque allows Torque to implement a Torquer interface

func (Torque) Unit Uses

func (t Torque) Unit() *Unit

Unit converts the Torque to a *Unit

type Unit Uses

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 Uses

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

Code:

// Create an angular acceleration of 3 rad/s^2
accel := unit.New(3.0, unit.Dimensions{unit.AngleDim: 1, unit.TimeDim: -2})
fmt.Println(accel)

Output:

3 rad s^-2

func (*Unit) Add Uses

func (u *Unit) Add(uniter Uniter) *Unit

Add adds the function argument to the receiver. Panics if the units of the receiver and the argument don't match.

func (*Unit) Dimensions Uses

func (u *Unit) Dimensions() Dimensions

Dimensions returns a copy of the dimensions of the unit.

func (*Unit) Div Uses

func (u *Unit) Div(uniter Uniter) *Unit

Div divides the receiver by the argument changing the dimensions of the receiver as appropriate.

func (*Unit) Format Uses

func (u *Unit) Format(fs fmt.State, c rune)

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 Uses

func (u *Unit) Mul(uniter Uniter) *Unit

Mul multiply the receiver by the input changing the dimensions of the receiver as appropriate. The input is not changed.

func (*Unit) SetValue Uses

func (u *Unit) SetValue(v float64)

SetValue sets the value of the unit.

func (*Unit) Unit Uses

func (u *Unit) Unit() *Unit

Unit implements the Uniter interface

func (*Unit) Value Uses

func (u *Unit) Value() float64

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 Uses

type Uniter interface {
    Unit() *Unit
}

Uniter is a type that can be converted to a Unit.

type Velocity Uses

type Velocity float64

Velocity represents a velocity in metres per second.

func (Velocity) Format Uses

func (v Velocity) Format(fs fmt.State, c rune)

func (*Velocity) From Uses

func (v *Velocity) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension

func (Velocity) Unit Uses

func (v Velocity) Unit() *Unit

Unit converts the Velocity to a *Unit

func (Velocity) Velocity Uses

func (v Velocity) Velocity() Velocity

Velocity allows Velocity to implement a Velocityer interface

type Voltage Uses

type Voltage float64

Voltage represents a voltage in Volts.

const Volt Voltage = 1

func (Voltage) Format Uses

func (v Voltage) Format(fs fmt.State, c rune)

func (*Voltage) From Uses

func (v *Voltage) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension

func (Voltage) Unit Uses

func (v Voltage) Unit() *Unit

Unit converts the Voltage to a *Unit

func (Voltage) Voltage Uses

func (v Voltage) Voltage() Voltage

Voltage allows Voltage to implement a Voltager interface

type Volume Uses

type Volume float64

Volume represents a volume in cubic metres.

const Litre Volume = 1e-3

func (Volume) Format Uses

func (v Volume) Format(fs fmt.State, c rune)

func (*Volume) From Uses

func (v *Volume) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension

func (Volume) Unit Uses

func (v Volume) Unit() *Unit

Unit converts the Volume to a *Unit

func (Volume) Volume Uses

func (v Volume) Volume() Volume

Volume allows Volume to implement a Volumeer interface

Directories

PathSynopsis
constantPackage constant provides fundamental constants satisfying unit.Uniter.

Package unit imports 7 packages (graph) and is imported by 4 packages. Updated 2019-06-06. Refresh now. Tools for package owners.