golang基础-chan的select操作、定时器操作、超时控制、goroutine中使用recover
生活随笔
收集整理的這篇文章主要介紹了
golang基础-chan的select操作、定时器操作、超时控制、goroutine中使用recover
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
chan的只讀和只寫
a.只讀chan的聲明
Var 變量的名字 <-chan int
Var readChan <- chan int
b. 只寫chan的聲明
Var 變量的名字 chan<- int
Var writeChan chan<- int
chan的select操作
格式如下
Select {case u := <- ch1:case e := <- ch2:default: }- 1
- 2
- 3
- 4
- 5
看實例代碼:
package mainimport "fmt" import "time"func main() {var ch chan intch = make(chan int ,10)ch2:= make(chan int ,10)go func(){var i intfor {ch <- itime.Sleep(time.Second)ch2 <- i * itime.Sleep(time.Second)i ++}}()for {select{case v:= <-ch:fmt.Println(v)case v:= <-ch2:fmt.Println(v)default:fmt.Println("get data timeout")time.Sleep(time.Second)}} }- 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
輸出如下:
PS E:\golang\go_pro\src\safly> go run demo.go 0 get data timeout 0 get data timeout 1 get data timeout 1 get data timeout 2 get data timeout 4 get data timeout 3 get data timeout 9 get data timeout 4 get data timeout 16 get data timeout 5 get data timeout exit status 2 PS E:\golang\go_pro\src\safly>- 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
chan的定時器
package mainimport "fmt" import "time"/* type Ticker struct {C <-chan Timer runtimeTimer } */func main() {t := time.NewTicker(time.Second)for v := range t.C {fmt.Println("hello, ", v)}}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
輸出如下:
PS E:\golang\go_pro\src\safly> go run demo.go hello, 2017-11-11 18:50:38.165007 +0800 CST hello, 2017-11-11 18:50:39.1652525 +0800 CST hello, 2017-11-11 18:50:40.165327 +0800 CST hello, 2017-11-11 18:50:41.1650873 +0800 CST exit status 2 PS E:\golang\go_pro\src\safly>- 1
- 2
- 3
- 4
- 5
- 6
- 7
一次定時器
package mainimport "fmt" import "time"/* func After(d Duration) <-chan Time {return NewTimer(d).C } */ func main() {select {case <- time.After(time.Second):fmt.Println("after")} }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
輸出如下:
PS E:\golang\go_pro\src\safly> go run demo.go after PS E:\golang\go_pro\src\safly>- 1
- 2
- 3
超時控制
package mainimport "fmt" import "time"func queryDb(ch chan int) {// time.Sleep(time.Second)ch <- 100 }func main() {ch := make(chan int)go queryDb(ch)t := time.NewTicker(time.Second)select {case v := <-ch:fmt.Println("result", v)case <-t.C:fmt.Println("timeout")}}- 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
輸出如下:
PS E:\golang\go_pro\src\safly> go run demo.go result 100 PS E:\golang\go_pro\src\safly>- 1
- 2
- 3
以上代碼是沒有超時的,我們將上面代碼中 // time.Sleep(time.Second)注釋去掉
就會輸出如下的結果
- 1
- 2
- 3
goroutine中使用recover
應用場景,如果某個goroutine panic了,而且這個goroutine里面沒有
捕獲(recover),那么整個進程就會掛掉。所以,好的習慣是每當go產
生一個goroutine,就需要寫下recover
- 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
輸出如下:
PS E:\golang\go_pro\src\safly> go run demo.go i'm calc panic: assignment to entry in nil map i'm calc i'm calc i'm calc i'm calc i'm calc i'm calc i'm calc exit status 2 PS E:\golang\go_pro\src\safly>總結
以上是生活随笔為你收集整理的golang基础-chan的select操作、定时器操作、超时控制、goroutine中使用recover的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Go语言goroutine+channe
- 下一篇: Go游戏服务器开发的一些思考(十):go