对以下行为感到好奇
func test() error {
ctx, cancel := context.WithCancel(context.Background())
cancel()
doneChan := make(chan bool)
go func() {
// emulate a long running function
time.Sleep(time.Minute)
// never exits?
doneChan <- true
}()
select {
case <- ctx.Done():
return ctx.Err()
case <- doneChan:
return nil
}
}
鉴于上述功能,如果select
语句选择了上下文取消,那么goroutine是否会尝试永久阻止doneChan
被阻止?在这种情况下,解决方案是否总是只有一个缓冲通道?
答案 0 :(得分:0)
要结束这个问题......简单的答案是肯定的。