-
Notifications
You must be signed in to change notification settings - Fork 1
/
Timers.go
35 lines (31 loc) · 688 Bytes
/
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
// Timers
package main
import (
"fmt"
"time"
)
func timers_() {
/*
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.
*/
time1 := time.NewTimer(time.Second * 2)
/*
The <-timer1.C blocks on the timer is channel C
until it sends a value indicating
that the timer expired.
*/
<-time1.C
fmt.Println("Timer 1 expired.")
time2 := time.NewTimer(time.Second)
go func() {
<-time2.C
fmt.Println("Timer 2 expired.")
}()
stop2 := time2.Stop()
if stop2 {
fmt.Println("Timer 2 stopped.")
}
}