我在从两个通道接收的 go 例程中有一个 select 语句。
for {
fmt.Printf("Waiting for select statement ...\n")
select {
case req := <-requestChan:
fmt.Printf("I got a request: %v\n", req)
case <-doneChan:
fmt.Printf("serveDatabase: Got closing signal. Stop serving.\n")
return
}
}
如果调用函数发送到第一个通道两次,然后发送到第二个通道,一切正常:
requestChan <- Db_request{ request: "Login", beehive: "yaylaswiese" }
requestChan <- Db_request{ request: "Signup", beehive: "aziz nezir" }
fmt.Printf("Sending true to the doneChannel\n")
doneChan <- true
控制台输出(正确)是:
> Waiting for select statement ...
> I got a request: {Login yaylaswiese}
> Waiting for select statement ...
> Sending true to the doneChannel
> I got a request: {Signup aziz nezir}
> Waiting for select statement ...
> serveDatabase: Got closing signal. Stop serving.
但是,如果我评论第二个请求,例如
requestChan <- Db_request{ request: "Login", beehive: "yaylaswiese" }
// requestChan <- Db_request{ request: "Signup", beehive: "aziz nezir" }
fmt.Printf("Sending true to the doneChannel\n")
doneChan <- true
然后输出是
> Waiting for select statement ...
> I got a request: {Login yaylaswiese}
> Waiting for select statement ...
> Sending true to the doneChannel
所以doneChan 永远不会收到。我也尝试在发送 doneChan 后进入无限循环,但结果相同。
那可能是什么?
慕无忌1623718
相关分类