-
Notifications
You must be signed in to change notification settings - Fork 0
/
timers.go
38 lines (31 loc) · 1.01 KB
/
timers.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
package main
import (
"fmt"
"time"
)
func main() {
// Timers represent a single event in the future. You tell the timer
// how long you want to wait, and it provides a channel that will be
// notified at that time. This timer will wait 2 seconds
timer1 := time.NewTimer(2 * time.Second)
value := <-timer1.C // timer starts
fmt.Println("Timer 1 fired: ", value) // print after timer is over
// The <-timer1.C blocks on the timer’s channel C until it sends a
// value indicating that the timer fired.
// If you just wanted to wait, you could have used time.Sleep. One
// reason a timer may be useful is that you can cancel the timer
// before it fires.
timer2 := time.NewTimer(time.Second)
go func() {
<-timer2.C
fmt.Println("Timer 2 fired")
}()
// stopping timer2 before if fires
stop2 := timer2.Stop()
if stop2 {
fmt.Println("Timer 2 stopped")
}
// Give the timer2 enough time to fire, if it ever was
// going to, to show it is in fact stopped.
time.Sleep(2 * time.Second)
}