on the go way (six)the usage of select which prevent form blocking

when used the select word ,sometimes it may be blocked ,we can use the timer to prevent form being blocking

which used as follows:

 1 package main
 2 
 3 import (
 4     "fmt"
 5     "time"
 6 )
 7 
 8 func main() {
 9     c := make(chan int)
10     o := make(chan bool)
11     go func() {
12         for {
13             select {
14             case v := <-c:
15                 fmt.Println("result is ", v)
16             case a := <-time.After(5 * time.Second):
17                 fmt.Println("time out", a)
18                 o <- true
19                 break
20             }
21         }
22     }()
23 
24     <-o
25 }

 

posted on 2016-03-23 12:01  AAAAAApple  阅读(262)  评论(0编辑  收藏  举报

导航