length: github.com/penguingovernor/length Index | Files

package length

import "github.com/penguingovernor/length"

Package length provides functionality for measuring and displaying lengths.


Package Files

doc.go length.go


const (
    Nanometer  Distance = 1
    Micrometer          = 1e3 * Nanometer
    Millimeter          = 1e3 * Micrometer
    Centimeter          = 10 * Millimeter
    Meter               = 1e3 * Millimeter
    Kilometer           = 1e3 * Meter
    Inch                = 2.54 * Centimeter
    Feet                = 304.8 * Millimeter
    Yard                = 3 * Feet
    Mile                = 5280 * Feet
    Lightyear           = 9.461e12 * Kilometer

Common distances.

To count the number of units in a Distance, divide:

meter := length.Meter
fmt.Print(float64(meter/length.Millimeter)) // prints 1000

To convert an integer number of units to a Distance, multiply:

meters := 10
fmt.Print(length.Distance(meters)*length.Meter) // prints 10m

func ToggleUnits Uses

func ToggleUnits()

ToggleUnits toggles the units (metric <=> imperial) that are printed whenever the String function is called (as is the case in family of printing functions in the fmt package). By default the metric system is used.

func UseImperial Uses

func UseImperial()

UseImperial toggles the units to use the imperial system. See ToggleUnits for more information.

func UseMetric Uses

func UseMetric()

UseMetric toggles the units to use the metric system. See ToggleUnits for more information.

type Distance Uses

type Distance float64

A Distance represents a physical distance as a float64 nanometer count. The representation limits the largest representable duration to approximately 1.900163142869793e283 light years ≈ diameter of the observable universe (≈ 93 billion ly )

func ParseDistance Uses

func ParseDistance(s string) (Distance, error)

ParseDistance parses a distance string. A distance string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300m" or "-1.5ly" Valid distance units are "nm", "um" (or "µm"), "mm", "m", "km", "in", "ft", "yd", "mi", "ly".

func (Distance) String Uses

func (d Distance) String() string

String returns a string representing the distance in the form "10m" or "10yd". The unit that is used is based on the state of the ToggleUnit function. As a special case, distances less than one meter (or yard) use a smaller unit to ensure that the leading digit is non-zero. The zero duration formats as 0m or 0yd.

Package length imports 2 packages (graph). Updated 2019-03-07. Refresh now. Tools for package owners.