Golang通道选择语句

huangapple go评论70阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2013年11月9日 01:35:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/19865194.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定