我有一个 go 例程,它会定期轮询每个时间间隔并执行操作。它还检查完成 channel ,作为结束信号,以停止轮询。
go func(){
for {
select{
case <-myticker.C:
do_something()
case <-done:
fmt.Println("I am done")
return
}
}
}
我意识到如果我关闭 channel ,它也会运行,就好像值被传递给 done channel 一样,它打印出“我完成了”为什么会这样?
最佳答案
来自规范:
A receive operation on a closed channel can always proceed immediately, yielding the element type's zero value after any previously sent values have been received.
关于go - 为什么在go routine中关闭channel会导致receiving channel收到值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49143280/