go channel select如何屏蔽已关闭通道
go channel select如何屏蔽已关闭通道(如果不屏蔽的话,select 一直会处于选择状态)
func main() { channel := make(chan int ,20) selectChannel(channel) getChannel(channel) time.Sleep(time.Hour) } func selectChannel(channel chan int) { go func() { for{ select{ case _,ok := <- channel: if !ok{ fmt.Println("channel is close") channel = make(chan int) //设置为非缓冲通道已做阻塞 time.Sleep(time.Second) } } } }() } func getChannel(channel chan int) { go func() { channel <- 2 close(channel) }() }
posted on 2020-04-02 18:29 ExplorerMan 阅读(649) 评论(0) 编辑 收藏 举报