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) }() }
原文:https://www.cnblogs.com/ExMan/p/12622148.html