Thanks to visit codestin.com
Credit goes to pkg.go.dev

unit

package
v0.0.0-...-63d2a97 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2025 License: Apache-2.0 Imports: 2 Imported by: 1

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

Examples

Constants

View Source
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.

View Source
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.

View Source
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) Abs

func (a Area) Abs() Area

Abs returns the area as an absolute value.

func (Area) Acres

func (a Area) Acres() float64

Acres returns the area in acres.

func (Area) GoString

func (a Area) GoString() string

GoString returns a Go syntax expression of the area. For example:

"0 * SquareMeter" "1234.56 * SquareCentimeter" "118484 * SquareKilometer" "5.10066e+15 * SquareMeter"

func (Area) Hectares

func (a Area) Hectares() float64

Hectares returns the area in hectares.

func (Area) SquareCentimeters

func (a Area) SquareCentimeters() float64

SquareCentimeters returns the area in square centimeters.

func (Area) SquareFeet

func (a Area) SquareFeet() float64

SquareFeet returns the area in square feet.

func (Area) SquareInches

func (a Area) SquareInches() float64

SquareInches returns the area in square inches.

func (Area) SquareKilometers

func (a Area) SquareKilometers() float64

SquareKilometers returns the area in square kilometers.

func (Area) SquareMeters

func (a Area) SquareMeters() float64

SquareMeters returns the area in square meters.

func (Area) SquareMiles

func (a Area) SquareMiles() float64

SquareMiles returns the area in square miles.

func (Area) SquareMillimeters

func (a Area) SquareMillimeters() float64

SquareMillimeters returns the area in square millimeters.

func (Area) String

func (a Area) String() 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) Abs

func (l Length) Abs() Length

Abs returns the length as an absolute value.

func (Length) Centimeters

func (l Length) Centimeters() float64

Centimeters returns the length in centimeters.

func (Length) Feet

func (l Length) Feet() float64

Feet returns the length in feet.

func (Length) GoString

func (l Length) GoString() string

func (Length) Inches

func (l Length) Inches() float64

Inches returns the length in inches.

func (Length) Kilometers

func (l Length) Kilometers() float64

Kilometers returns the length in kilometers.

func (Length) Meters

func (l Length) Meters() float64

Meters returns the length in meters.

func (Length) Micrometers

func (l Length) Micrometers() float64

Micrometers returns the length in micrometers.

func (Length) Miles

func (l Length) Miles() float64

Miles returns the length in miles.

func (Length) Millimeters

func (l Length) Millimeters() float64

Millimeters returns the length in millimeters.

func (Length) NauticalMiles

func (l Length) NauticalMiles() float64

NauticalMiles returns the length in nautical miles.

func (Length) PerTime

func (l Length) PerTime(d time.Duration) Speed

PerTime returns a speed from a length travelled in a given amount of time.

func (Length) String

func (l Length) String() 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

func (s Speed) FeetPerSecond() float64

FeetPerSecond returns the speed in feet per second.

func (Speed) GoString

func (s Speed) GoString() string

GoString returns a Go syntax expression of the speed. For example:

"0 * MeterPerSecond"
"0.001 * MeterPerSecond"
"2.99792458e+08 * MeterPerSecond"

func (Speed) KilometersPerHour

func (s Speed) KilometersPerHour() float64

KilometersPerHour returns the speed in kilometers per hour.

func (Speed) Knots

func (s Speed) Knots() float64

Knots returns the speed in nautical miles per hour.

func (Speed) MetersPerSecond

func (s Speed) MetersPerSecond() float64

MetersPerSecond returns the speed in meters per second.

func (Speed) MilesPerHour

func (s Speed) MilesPerHour() float64

MilesPerHour returns the speed in miles per hour.

func (Speed) String

func (s Speed) String() string

String returns a string representation of the speed in meters per second using compact number syntax. For example:

"0 m/s"
"0.001 m/s"
"2.99792458e+08 m/s"

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL