这段代码正常工作,我的问题就是原因.我了解到,在阻止之前,您只能将一个值发送到无缓冲的通道.但是在我的代码中,我写了两次,但是来自不同的例行程序,它起作用.如果有人能向我解释原因,我将不胜感激!
func main(){
var ch chan string =make(chan string)
go write(ch)
go write2(ch)
go read(ch)
select{}
}
func write(ch chan string){
for{
ch<-"write1"
}
}
func write2(ch chan string){
for{
ch<-"write2"
}
}
func read(ch chan string){
for{
time.Sleep(time.Second)
select{
case res:= <-ch: fmt.Println(res)
case <-time.After(time.Millisecond*200): fmt.Println("time out")
}
}
}
Run Code Online (Sandbox Code Playgroud)