package main
import "fmt"
func routine(doneCh <-chan struct{}) {
for {
select {
case <-doneCh:
fmt.Println("Routine done") // This never prints
return
}
}
}
func main() {
doneCh := make(chan struct{}, 1)
go routine(doneCh)
close(doneCh)
}
但是,如果我添加time.Sleep(1 * time.Second)
,则输出会打印出来。在程序退出之前关闭例程从doneCh
接收的惯用方法是什么?