forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example2.go
47 lines (37 loc) · 1.09 KB
/
example2.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
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// Sample program to show how to declare methods against
// a named type.
package main
import "fmt"
// duration is a named type that represents a duration
// of time in Nanosecond.
type duration int64
const (
nanosecond duration = 1
microsecond = 1000 * nanosecond
millisecond = 1000 * microsecond
second = 1000 * millisecond
minute = 60 * second
hour = 60 * minute
)
// setHours sets the specified number of hours.
func (d *duration) setHours(h float64) {
*d = duration(h) * hour
}
// hours returns the duration as a floating point number of hours.
func (d duration) hours() float64 {
hour := d / hour
nsec := d % hour
return float64(hour) + float64(nsec)*(1e-9/60/60)
}
func main() {
// Declare a variable of type duration set to
// its zero value.
var dur duration
// Change the value of dur to equal
// five hours.
dur.setHours(5)
// Display the new value of dur.
fmt.Println("Hours:", dur.hours())
}