aboutsummaryrefslogtreecommitdiffstats
path: root/temperature
diff options
context:
space:
mode:
Diffstat (limited to 'temperature')
-rw-r--r--temperature/temperature.go47
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