英文:
Golang channels select statement
问题
最近我刚开始尝试使用Go语言进行实验。我想知道,如果在几个通道上等待通信的时候,如果同时在两个或多个通道上收到消息,select语句会发生什么情况?如果所有的消息同时到达,select语句会如何确定要接收哪个通道的消息呢?
谢谢!
英文:
Just experimenting with go recently. I was wondering what will happen if you have a select statement waiting for communication on a few channels and if a message comes AT THE SAME time on two or more channels. How will the select determine which channel to accept if all the messages come at the same time?
Thanks!
答案1
得分: 19
根据规范:
> 如果有多个 case 可以执行,将会进行一次均匀的伪随机选择来决定哪个 case 将会执行。
因此,选择是非确定性的。
英文:
From the spec:
> If multiple cases can proceed, a uniform pseudo-random choice is made to decide which single communication will execute.
So the choice is non-deterministic.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论