1 package main
2
3 import (
4 "fmt"
5 "time"
6 )
7
8 func main() {
9
10 //创建一个10.5秒的定时器(当到达时间时它会向 timer.C 管道发送一条消息)
11 dis, _ := time.ParseDuration("10s500ms")
12 timer := time.NewTimer(dis)
13
14 //c管道用于阻塞主程, 起一个go程运行 testSelect 方法
15 c := make(chan bool)
16 go testSelect(timer, c)
17
18 //阻塞go主程
19 c <- true
20
21 fmt.Println("10.5秒已过, go程全部结束!")
22 //index.RunServer() //运行服务器
23 }
24
25 func testSelect(timer *time.Timer, c chan bool) {
26 var _c chan bool
27
28 for {
29 select {
30
31 //timer.C 管道如果有消息结束此方法, 并接收 c管道 的消息,让主程停止阻塞
32 case <-timer.C:
33 <-c
34 return
35
36 case <-_c:
37 fmt.Println("不会说话")
38
39 }
40
41 }
42
43 }