gpt4 book ai didi

go - 使用异常选择的意外行为

转载 作者:行者123 更新时间:2023-12-03 10:06:44 26 4
gpt4 key购买 nike

我正在编写一些代码,将数据从一个 channel 传递到另一个 channel 。遵循一些直觉和this answer我期望以下代码能够工作(other 是一个足够大的缓冲 channel ,outsource channel ):

for {
select {
case other <- (<-out):
log.Warn("C")
}
}

确实如此!但其他情况根本不会触发,例如以下代码的日志中没有 D:

for {
select {
case other <- (<-out):
log.Warn("C")
default:
log.Warn("D")
}
}

使用更传统的解决方案,日志中到处都是 D:

for {
select {
case msg := <-out:
other <- msg
log.Warn("C")
default:
log.Warn("D")
}
}

显然,我会采用通常的解决方案,但我仍然不知道为什么不寻常的解决方案不能按预期工作。

我怀疑答案就在 The Go Memory Model 中的某个地方但我不太清楚这种情况到底发生了什么。

我整理了一些 Playground ,您可以在其中检查此行为:

  • Unusal (根本没有D)
  • Usual (有很多 D,您可能需要在本地尝试才能看到 D 以外的任何内容)

预先感谢任何能够阐明这一点的人!

最佳答案

当你有这个时:

ch := make(chan int, 10)
// ...

select {
case ch <- <-out:
fmt.Println("C")
default:
fmt.Println("D")
}

第一个case的通信操作是 ch <- something ,其中something<-out 。但是something首先评估,然后才检查案例的哪个通信操作可以继续。

所以<-out只要需要就会阻塞,然后 ch <- something检查是否可以继续。由于您使用了足够大的缓冲区,因此它始终可以在您的示例中继续进行,因此 default从未被选择。

Spec: Select statements:

Execution of a "select" statement proceeds in several steps:

  1. For all the cases in the statement, the channel operands of receive operations and the channel and right-hand-side expressions of send statements are evaluated exactly once, in source order, upon entering the "select" statement. The result is a set of channels to receive from or send to, and the corresponding values to send. Any side effects in that evaluation will occur irrespective of which (if any) communication operation is selected to proceed. Expressions on the left-hand side of a RecvStmt with a short variable declaration or assignment are not yet evaluated.
  2. If one or more of the communications can proceed, a single one that can proceed is chosen via a uniform pseudo-random selection. Otherwise, if there is a default case, that case is chosen. If there is no default case, the "select" statement blocks until at least one of the communications can proceed.
  3. Unless the selected case is the default case, the respective communication operation is executed.
  4. If the selected case is a RecvStmt with a short variable declaration or an assignment, the left-hand side expressions are evaluated and the received value (or values) are assigned.
  5. The statement list of the selected case is executed.

如果降低 ch 的缓冲区,你偶尔会看到D s 在输出中打印(在 Go Playground 上尝试)。

ch := make(chan int, 2)

关于go - 使用异常选择的意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64661318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com