diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2022-05-27 22:39:03 -0230 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2022-05-27 22:39:03 -0230 |
| commit | 31605150d3e10b08dad2086005c64664f5648a51 (patch) | |
| tree | 7419f90e7b30d3de52595f25852e9ef87094718c /temperature | |
| parent | 585fbf852c1e76470df42ebe99ede62440ce19d9 (diff) | |
| download | volute-31605150d3e10b08dad2086005c64664f5648a51.zip | |
prepare to switch ui library
Diffstat (limited to 'temperature')
| -rw-r--r-- | temperature/temperature.go | 47 |
1 files changed, 12 insertions, 35 deletions
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 |