-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeafter.go
54 lines (51 loc) · 1.09 KB
/
timeafter.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
// package main
// import (
// "fmt"
// "time"
// )
// func main() {
// // 声明一个退出用的通道
// exit := make(chan int)
// // 打印开始
// fmt.Println("start")
// // 过1秒后, 调用匿名函数
// time.AfterFunc(3*time.Second, func() {
// // 1秒后, 打印结果
// fmt.Println("one second after")
// // 通知main()的goroutine已经结束
// exit <- 0
// })
// // 等待结束
// fmt.Println(<-exit)
// }
package main
import (
"fmt"
"time"
)
func main() {
// 创建一个打点器, 每500毫秒触发一次
ticker := time.NewTicker(time.Millisecond * 500)
// 创建一个计时器, 2秒后触发
stopper := time.NewTimer(time.Second * 5)
fmt.Println(stopper.C)
// 声明计数变量
var i int
// 不断地检查通道情况
for {
// 多路复用通道
select {
case <-stopper.C: // 计时器到时了
fmt.Println("stop")
// 跳出循环
goto StopHere
case <-ticker.C: // 打点器触发了
// 记录触发了多少次
i++
fmt.Println("tick", time.Now(), i)
}
}
// 退出的标签, 使用goto跳转
StopHere:
fmt.Println("done")
}