temperature

package module
v0.0.0-...-f357c4d Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2018 License: MIT Imports: 3 Imported by: 0

README

temperature

Build Status Go Report Card GoDoc

This is Go library for temperature conversion. So far, it allows you to convert Kelvin, Celsius, Fahrenheit, Rankine, Reaumur and Delisle unit. You can also use your own unit as long at the unit is implementing the interfaces ToKelvin and FromKelvin.

Installing

go get github.com/yanndr/temperature

Usage

c := temperature.New(30,Celsius)

result,_ := temperature.Convert(c, temperature.Fahrenheit)


fmt.Println(result)
// output 86 °F

If you want to use a unit that I didn't implement:

type newUnit temperature.Unit

const myUnit = newUnit("Y")

func (newUnit) ToKelvin(v float64) float64 {
	return v + 710
}

func (newUnit) FromKelvin(v float64) temperature.Temperature {
	return temperature.New(v-710, myUnit)
}

func main() {
	c := temperature.New(0,temperature.Celsius)

	result,_ := temperature.Convert(c, myUnit)
	fmt.Println(c)
	fmt.Println(result)
}

Documentation

Overview

Package temperature provides Temperature struct and a convert method to deal with different temperature scale.

Index

Examples

Constants

View Source
const (
	//Celsius temperature unit
	Celsius = CelsiusUnit("°C")
	//Kelvin temperature unit
	Kelvin = KelvinUnit("K")
	//Fahrenheit temperature unit
	Fahrenheit = FahrenheitUnit("°F")
	//Rankine temperature unit
	Rankine = RankineUnit("°Ra")
	//Delisle temperature unit
	Delisle = DelisleUnit("°D")
	//Reaumur temperature unit
	Reaumur = ReaumurUnit("°Re")
)

Variables

View Source
var ErrNilArgument = errors.New("argument can't be nil")

ErrNilArgument is an error when the argument is nil.

Functions

func Equals

func Equals(a Temperature, b Temperature) bool

Equals returns true if two temperature are equals.

Types

type CelsiusUnit

type CelsiusUnit Unit

func (CelsiusUnit) FromKelvin

func (CelsiusUnit) FromKelvin(v float64) Temperature

func (CelsiusUnit) ToKelvin

func (CelsiusUnit) ToKelvin(v float64) float64

type Convertible

type Convertible interface {
	ToKelvin(float64) float64
	FromKelvin(float64) Temperature
}

Convertible provides function to convert from and to Kelvin unit.

type DelisleUnit

type DelisleUnit Unit

func (DelisleUnit) FromKelvin

func (DelisleUnit) FromKelvin(v float64) Temperature

func (DelisleUnit) ToKelvin

func (DelisleUnit) ToKelvin(v float64) float64

type FahrenheitUnit

type FahrenheitUnit Unit

func (FahrenheitUnit) FromKelvin

func (FahrenheitUnit) FromKelvin(v float64) Temperature

func (FahrenheitUnit) ToKelvin

func (FahrenheitUnit) ToKelvin(v float64) float64

type KelvinUnit

type KelvinUnit Unit

func (KelvinUnit) FromKelvin

func (KelvinUnit) FromKelvin(v float64) Temperature

func (KelvinUnit) ToKelvin

func (KelvinUnit) ToKelvin(v float64) float64

type RankineUnit

type RankineUnit Unit

func (RankineUnit) FromKelvin

func (RankineUnit) FromKelvin(v float64) Temperature

func (RankineUnit) ToKelvin

func (RankineUnit) ToKelvin(v float64) float64

type ReaumurUnit

type ReaumurUnit Unit

func (ReaumurUnit) FromKelvin

func (ReaumurUnit) FromKelvin(v float64) Temperature

func (ReaumurUnit) ToKelvin

func (ReaumurUnit) ToKelvin(v float64) float64

type Stringer

type Stringer interface {
	String() string
}

Stringer provides String method.

type Temperature

type Temperature interface {
	Stringer
	Value() float64
	SetValue(float64)
	Unit() Convertible
	SetUnit(Convertible)

	SetTemperature(Temperature)
	SetTemperateChangeHandler(temperatureChangeHandler)
}

Temperature provides all the function needed for a temperature.

func Convert

func Convert(input Temperature, output Convertible) (Temperature, error)

Convert a temperature to different unit.

Example
c := New(30, Celsius)

result, err := Convert(c, Fahrenheit)
if err != nil {
	log.Fatal(err)
}

fmt.Println(result)
Output:

86 °F

func New

func New(v float64, unit Convertible) Temperature

New returns a new Temperature.

func NewWithHandler

func NewWithHandler(v float64, unit Convertible, handler temperatureChangeHandler) Temperature

NewWithHandler returns a new temperature with a handler to handle temperature changes.

type Unit

type Unit string

Unit is a temperature unit type.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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