英文:
How this select statement executes?
问题
incoming := make(chan int)
done := make(chan struct{})
...
close(done)
...
select {
case incoming <- order:
// 逻辑处理
case <-done:
return
}
假设在 select 语句之前关闭了 done 通道:
-
会选择执行
case <-done
。 -
这是因为当
close(done)
执行时,done 通道将处于一种状态,使得 select 语句会将case <-done
视为匹配项,而不是写入 caseincoming <- order
。
英文:
incoming := make(chan int)
done := make(chan struct{})
...
close(done)
...
select {
case incoming <- order:
// logic
case <-done:
return
}
Let's say you close the done channel before the select statement:
1.The case <-done will be selected.
2.Is this because when close(done) executes, the done channel will be in a state, in which the select statement will consider the "case <-done" a match over the writing case "incoming <- order"?
答案1
得分: 4
select
语句会阻塞,直到其cases
中的一个或多个通信操作可以进行。一旦有一个或多个通信操作可以进行,select
会随机选择其中之一。
对于关闭的channel
上的接收操作,可以立即进行。
> 执行“select”语句分为几个步骤:
>
> 1. 对于语句中的所有case,在进入“select”语句时,接收操作的channel操作数和发送语句的channel和右侧表达式将按照源代码顺序进行精确一次的求值。结果是一组要接收或发送的通道,以及要发送的相应值。在该求值过程中的任何副作用都将发生,无论选择哪个(如果有)通信操作来进行。带有短变量声明或赋值的RecvStmt左侧的表达式尚未求值。
> 2. **如果有一个或多个通信可以进行,将通过均匀伪随机选择选择一个可以进行的通信。**否则,如果有一个默认的case,将选择该case。如果没有默认的case,那么“select”语句将阻塞,直到至少有一个通信可以进行。
> 3. 除非所选的case是默认的case,否则将执行相应的通信操作。
> 4. 如果所选的case是带有短变量声明或赋值的RecvStmt,将对左侧表达式进行求值,并将接收到的值(或值)赋给它们。
> 5. 执行所选case的语句列表。
> 对于关闭的通道上的接收操作,始终可以立即进行,在接收到之前发送的任何值之后,产生元素类型的零值。
英文:
The select
statement blocks until one or more of the communication operations in its cases
can proceed. As soon as one or more communication operations can proceed select
will randomly choose one of them.
A receive operation on a closed channel
can proceed immediately.
> 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.
> A receive operation on a closed channel can always proceed
> immediately, yielding the element type's zero value after any
> previously sent values have been received.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论