-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdistance.go
69 lines (54 loc) · 1.21 KB
/
distance.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
61
62
63
64
65
66
67
68
69
package distance
import (
"fmt"
"strings"
)
type Distance float64
const (
Metre Distance = 1
Centimetre = Metre / 100
Millimetre = Centimetre / 10
Kilometre = 1000 * Metre
Mile = 1.609344 * Kilometre
Yard = Mile / 1760
Foot = Yard / 3
Inch = Foot / 12
)
func (d Distance) String() string {
return fmt.Sprintf("%vm", float64(d))
}
func (d Distance) Metres() float64 {
return float64(d)
}
func (d Distance) Centimetres() float64 {
return float64(d / Centimetre)
}
func (d Distance) Millimetres() float64 {
return float64(d / Millimetre)
}
func (d Distance) Kilometres() float64 {
return float64(d / Kilometre)
}
func (d Distance) Miles() float64 {
return float64(d / Mile)
}
func (d Distance) Inches() float64 {
return float64(d / Inch)
}
func (d Distance) Yards() float64 {
return float64(d / Yard)
}
func (d Distance) Feet() float64 {
return float64(d / Foot)
}
func (d *Distance) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), `"`)
if s == "null" {
return
}
*d, err = ParseDistance(s)
return
}
func (d *Distance) MarshalJSON() ([]byte, error) {
return []byte(d.String()), nil
}