From 31605150d3e10b08dad2086005c64664f5648a51 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Fri, 27 May 2022 22:39:03 -0230 Subject: prepare to switch ui library --- temperature/temperature.go | 47 ++++++++++++---------------------------------- 1 file changed, 12 insertions(+), 35 deletions(-) (limited to 'temperature/temperature.go') diff --git a/temperature/temperature.go b/temperature/temperature.go index 2a135df..6588ee6 100644 --- a/temperature/temperature.go +++ b/temperature/temperature.go @@ -5,56 +5,33 @@ import ( "fmt" ) -type unit int +type Unit int const ( - Celcius unit = iota + Celcius Unit = iota Kelvin Fahrenheit ) -// UnitStrings returns a slice of strings, each representing a -// unit. -// This is necessary because giu.Combo only works with strings. -func UnitStrings() []string { - return []string{"°C", "°K", "°F"} -} - -const ( - DefaultUnit unit = Celcius - // DefaultUnitIndex is used to index UnitStrings(). - DefaultUnitIndex int32 = 0 // celcius -) - -func UnitFromString(s string) (unit, error) { - // Each case corresponds to a value in UnitStrings(). - switch s { - case "°C": - return Celcius, nil - case "°K": - return Kelvin, nil - case "°F": - return Fahrenheit, nil - default: - return *new(unit), errors.New(fmt.Sprintf("invalid unit: '%s'", s)) - } +type Temperature struct { + val float32 + unit Unit } -type Temperature struct { - Val float32 - Unit unit +func New(i float32, u Unit) Temperature { + return Temperature{i, u} } -func (t Temperature) AsUnit(u unit) (float32, error) { +func (t Temperature) As(u Unit) (float32, error) { // Convert to celcius var c float32 - switch t.Unit { + switch t.unit { case Celcius: - c = t.Val + c = t.val case Kelvin: - c = t.Val - 272.15 + c = t.val - 272.15 case Fahrenheit: - c = (t.Val - 32.0) * (5.0 / 9.0) + c = (t.val - 32.0) * (5.0 / 9.0) } // Convert to desired unit -- cgit v1.2.3