blob: acec0406ef884661d0d6624a9406aeab5309c534 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
package volume
import (
"errors"
"fmt"
)
type unit float32
const (
CubicCentimetre unit = 1
Litre unit = 1_000
CubicMetre unit = 1_000_000
CubicInch unit = 16.38706
)
// 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{"cc", "L", "m³", "in³"}
}
const (
DefaultUnit unit = CubicCentimetre
// DefaulUnitIndex is used to index UnitStrings().
DefaultUnitIndex int32 = 0 // cc
)
func UnitFromString(s string) (unit, error) {
// Each case corresponds to a value in UnitStrings().
switch s {
case "cc":
return CubicCentimetre, nil
case "L":
return Litre, nil
case "m³":
return CubicMetre, nil
case "in³":
return CubicInch, nil
default:
return *new(unit), errors.New(fmt.Sprintf("invalid volume unit: '%s'", s))
}
}
type Volume struct {
val float32
}
func New(i float32, u unit) Volume {
return Volume{i * float32(u)}
}
func (v Volume) AsUnit(u unit) float32 {
return v.val / float32(u)
}
|