Documentation
¶
Overview ¶
Package unit implements types and functions for working with units of measurement.
Usage:
Use constants to define literal quantities:
distanceToSun := 149.6e6 * unit.Kilometer
or cast to create typed quantities from variables:
var altitudeInFeet = 29031 altitude := Length(altitudeInFeet) * unit.Foot
Use the conversion functions to convert back to units, as needed:
altitudeInMeters := altitude.Meters()
Types in this package implement the fmt.GoStringer interface, returning a scaled value times an SI unit. For example,
fmt.Sprintf("%#v", unit.Meter / 4) == "25 * Centimeter"
Index ¶
- Constants
- type Area
- func (a Area) Abs() Area
- func (a Area) Acres() float64
- func (a Area) GoString() string
- func (a Area) Hectares() float64
- func (a Area) SquareCentimeters() float64
- func (a Area) SquareFeet() float64
- func (a Area) SquareInches() float64
- func (a Area) SquareKilometers() float64
- func (a Area) SquareMeters() float64
- func (a Area) SquareMiles() float64
- func (a Area) SquareMillimeters() float64
- func (a Area) String() string
- type Length
- func (l Length) Abs() Length
- func (l Length) Centimeters() float64
- func (l Length) Feet() float64
- func (l Length) GoString() string
- func (l Length) Inches() float64
- func (l Length) Kilometers() float64
- func (l Length) Meters() float64
- func (l Length) Micrometers() float64
- func (l Length) Miles() float64
- func (l Length) Millimeters() float64
- func (l Length) NauticalMiles() float64
- func (l Length) PerTime(d time.Duration) Speed
- func (l Length) String() string
- type Speed
- type Temperature
Examples ¶
Constants ¶
const ( SquareMeter Area = 1 SquareKilometer = 1e6 * SquareMeter Hectare = 1e4 * SquareMeter SquareCentimeter = 1e-4 * SquareMeter SquareMillimeter = 1e-6 * SquareMeter SquareFoot = 0.3048 * 0.3048 * SquareMeter SquareMile = 5280 * 5280 * SquareFoot Acre = 66 * 660 * SquareFoot SquareInch = SquareFoot / (12 * 12) )
Common area units.
const ( Meter Length = 1 Kilometer = 1e3 * Meter Centimeter = 1e-2 * Meter Millimeter = 1e-3 * Meter Micrometer = 1e-6 * Meter Foot = 0.3048 * Meter Mile = 5280 * Foot Inch = Foot / 12 NauticalMile = 1852 * Meter )
Common length units.
const ( MeterPerSecond Speed = 1 KilometerPerHour = Speed(Kilometer/Meter) * MeterPerSecond / hourInSeconds FootPerSecond = Speed(Foot/Meter) * MeterPerSecond MilePerHour = Speed(Mile/Foot) * FootPerSecond / hourInSeconds Knot = Speed(NauticalMile/Kilometer) * KilometerPerHour )
Common durations.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Area ¶
type Area float64
Area represents a two-dimensional measurement in square meters as a float64.
func (Area) GoString ¶
GoString returns a Go syntax expression of the area. For example:
"0 * SquareMeter" "1234.56 * SquareCentimeter" "118484 * SquareKilometer" "5.10066e+15 * SquareMeter"
func (Area) SquareCentimeters ¶
SquareCentimeters returns the area in square centimeters.
func (Area) SquareFeet ¶
SquareFeet returns the area in square feet.
func (Area) SquareInches ¶
SquareInches returns the area in square inches.
func (Area) SquareKilometers ¶
SquareKilometers returns the area in square kilometers.
func (Area) SquareMeters ¶
SquareMeters returns the area in square meters.
func (Area) SquareMiles ¶
SquareMiles returns the area in square miles.
func (Area) SquareMillimeters ¶
SquareMillimeters returns the area in square millimeters.
func (Area) String ¶
String returns a string representation of the area in square meters.
If possible, the area will be formatted with an appropriate SI prefix, e.g. 1.2km^2, 2.3m^2, 3.4cm^2, 4.5mm^2. Otherwise the distance will be formatted as a scientific representation in square meters, e.g. 123.45e+09m^2. Except in the case where the length is 0, leading zeroes will always be omitted, e.g. 0.2km^2 will be returned as "200000m^2" and 0.9mm^2 will be returned as "9e-7m^2".
type Length ¶
type Length float64
Length represents a linear dimension measurement in meters as a float64.
Example ¶
package main import ( "fmt" "github.com/google/go-units/unit" ) func main() { // Define a length using a constant. distance := 500 * unit.Mile // Define a length from a variable. var altitudeInFeet = 29031 altitude := unit.Length(altitudeInFeet) * unit.Foot // Convert to different units. fmt.Printf("I would walk %.0f miles, but also %.1f kilometers.\n", distance.Miles(), distance.Kilometers()) fmt.Printf("Altitude: %.2f m\n", altitude.Meters()) // Perform calculations. totalDistance := 2 * distance fmt.Printf("And after I walk %.0f miles more, that's a total of %.1f nautical miles.\n", distance.Miles(), totalDistance.NauticalMiles()) }
Output: I would walk 500 miles, but also 804.7 kilometers. Altitude: 8848.65 m And after I walk 500 miles more, that's a total of 869.0 nautical miles.
func (Length) Centimeters ¶
Centimeters returns the length in centimeters.
func (Length) Kilometers ¶
Kilometers returns the length in kilometers.
func (Length) Micrometers ¶
Micrometers returns the length in micrometers.
func (Length) Millimeters ¶
Millimeters returns the length in millimeters.
func (Length) NauticalMiles ¶
NauticalMiles returns the length in nautical miles.
func (Length) String ¶
String returns a string representation of the length in meters.
If possible, the length will be returned with an appropriate SI prefix (e.g. 1.2km, 2.3m, 3.4cm, 4.5mm, 5.6µm), otherwise the distance will be returned as a scientific representation in meters (e.g. 149.6e+09m). Except in the case where the length is 0, leading zeros will always be omitted (e.g 0.2km will be returned as "200m", 0.9e9m will be returned as "9e+08m").
type Speed ¶
type Speed float64
Speed represents the magnitude of velocity of an object in meters per second as a float64.
func (Speed) FeetPerSecond ¶
FeetPerSecond returns the speed in feet per second.
func (Speed) GoString ¶
GoString returns a Go syntax expression of the speed. For example:
"0 * MeterPerSecond" "0.001 * MeterPerSecond" "2.99792458e+08 * MeterPerSecond"
func (Speed) KilometersPerHour ¶
KilometersPerHour returns the speed in kilometers per hour.
func (Speed) MetersPerSecond ¶
MetersPerSecond returns the speed in meters per second.
func (Speed) MilesPerHour ¶
MilesPerHour returns the speed in miles per hour.
type Temperature ¶
type Temperature float64
Temperature represents a thermodynamic temperature measurement in Kelvin as a float64.
const ( Kelvin Temperature = 1 Rankine = 5.0 / 9.0 * Kelvin )
Common temperatures.
func TemperatureFromDegreesCelsius ¶
func TemperatureFromDegreesCelsius(c float64) Temperature
TemperatureFromDegreesCelsius returns a Temperature from a measurement in degrees Celsius.
func TemperatureFromDegreesFahrenheit ¶
func TemperatureFromDegreesFahrenheit(f float64) Temperature
TemperatureFromDegreesFahrenheit returns a Temperature from a measurement in degrees Fahrenheit.
func (Temperature) DegreesCelsius ¶
func (t Temperature) DegreesCelsius() float64
DegreesCelsius returns the temperature in degrees Celsius.
func (Temperature) DegreesFahrenheit ¶
func (t Temperature) DegreesFahrenheit() float64
DegreesFahrenheit returns the temperature in degrees Fahrenheit.
func (Temperature) DegreesRankine ¶
func (t Temperature) DegreesRankine() float64
DegreesRankine returns the temperature in degrees Rankine.
func (Temperature) GoString ¶
func (t Temperature) GoString() string
GoString returns a Go syntax expression of the temperature (e.g. "294.15 * Kelvin", "5778 * Kelvin", "1.5e+07 * Kelvin").
func (Temperature) Kelvin ¶
func (t Temperature) Kelvin() float64
Kelvin returns the temperature in Kelvin.
func (Temperature) String ¶
func (t Temperature) String() string
String returns a string representation of the temperature in Kelvin using compact number syntax (e.g. "294.15 K", "5778 K", "1.57e+07 K").