-
Notifications
You must be signed in to change notification settings - Fork 0
/
mass.go
60 lines (46 loc) · 1.3 KB
/
mass.go
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
57
58
59
60
package unit
import (
"encoding"
"fmt"
)
// Mass is the quantity of matter in a physical body.
type Mass float64
var _ encoding.TextUnmarshaler = (*Mass)(nil)
// Kilogram is the SI unit for measuring Mass.
const Kilogram = Kilo * Gram
const kilogramSymbol = "kg"
// Gram is one thousandth of the SI unit for measuring mass, the Kilogram.
const Gram Mass = 1.0
const gramSymbol = "g"
// Tonne is a thousand of the SI unit for measuring mass, the Kilogram.
const Tonne = 1e6 * Gram
const tonneSymbol = "t"
// Kilograms returns m with the unit of kg.
func (m Mass) Kilograms() float64 {
return m.Get(Kilogram)
}
// Get returns m with the unit of as.
func (m Mass) Get(as Mass) float64 {
return float64(m) / float64(as)
}
// String implements fmt.Stringer.
func (m Mass) String() string {
return format(float64(m), gramSymbol)
}
// UnmarshalString sets *m from s.
func (m *Mass) UnmarshalString(s string) error {
parsed, err := parse(s, map[string]float64{
gramSymbol: float64(Gram),
kilogramSymbol: float64(Kilogram),
tonneSymbol: float64(Tonne),
})
if err != nil {
return fmt.Errorf("unmarshal mass: %w", err)
}
*m = Mass(parsed)
return nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (m *Mass) UnmarshalText(text []byte) error {
return m.UnmarshalString(string(text))
}