forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example4.go
61 lines (46 loc) · 1.84 KB
/
example4.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
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
/*
// A Duration represents the elapsed time between two instants as
// an int64 nanosecond count. The representation limits the largest
// representable duration to approximately 290 years.
type Duration int64
// Common durations. There is no definition for units of Day or larger
// to avoid confusion across daylight savings time zone transitions.
const (
Nanosecond Duration = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 60 * Minute
)
// Add returns the time t+d.
func (t Time) Add(d Duration) Time
*/
// Sample program to show a idiomatic use of named types from the
// standard library and how they work in concert with other Go concepts.
package main
import (
"fmt"
"time"
)
// fiveSeconds is a typed constant of type int64.
const fiveSeconds int64 = 5 * time.Second // time.Duration(5) * time.Duration(1000000000)
// ./example2.go:37: cannot use time.Duration(5) * time.Second
// (type time.Duration) as type int64 in const initializer
func main() {
// Use the time package to get the current date/time.
now := time.Now()
// Subtract 5 nanoseconds from now time using a literal constant.
lessFiveNanoseconds := now.Add(-5)
// Attempt to use the constant of type int64.
lessFiveSeconds := now.Add(-fiveSeconds)
// ./example4.go:51: illegal constant expression - int64
// ./example4.go:51: cannot use -fiveSeconds (type int64) as
// type time.Duration in argument to now.Add
// Display the values.
fmt.Printf("Now : %v\n", now)
fmt.Printf("Nano : %v\n", lessFiveNanoseconds)
fmt.Printf("Seconds : %v\n", lessFiveSeconds)
}