-
Notifications
You must be signed in to change notification settings - Fork 0
/
gas.go
52 lines (38 loc) · 1.08 KB
/
gas.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
package main
import "fmt"
type Gallons float64
func (g Gallons) String() string {
return fmt.Sprintf("%0.2f gal", g)
}
type Liters float64 // satisfy the stringer interface
func (l Liters) String() string {
return fmt.Sprintf("%0.2f L", l)
}
type Milliliters float64
func (m Milliliters) String() string {
return fmt.Sprintf("%0.2f mL", m)
}
func (l Liters) ToGallons() Gallons {
return Gallons(l * 0.264)
}
func (g Gallons) ToLiters() Liters {
return Liters(g * 3.785)
}
func (m Milliliters) ToGallons() Gallons {
return Gallons(m * 0.000264)
}
func (g Gallons) ToMilliliters() Milliliters {
return Milliliters(g * 3785.41)
}
func main() {
gas := Liters(2)
fmt.Printf("%0.3f liters equals %0.3f gallons.\n", gas, gas.ToGallons())
water := Milliliters(500)
fmt.Printf("%0.3f milliliters equals %0.3f gallons.\n", water, water.ToGallons())
milk := Gallons(2)
fmt.Printf("%0.3f gallons equals %0.3f milliliters.\n", milk, milk.ToMilliliters())
fmt.Printf("%0.3f gallons equals %0.3f liters.\n", milk, milk.ToLiters())
fmt.Println(gas)
fmt.Println(water)
fmt.Println(milk)
}