I’m looking for a new client or two. Could your team use some expert Go help? Reach out, and let’s talk!
Select statements
…
Execution of a “select” statement proceeds in several steps:
- 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.
There are two important things to call out in this paragraph, I think.
First, and most important, unlike with a switch
statement, all cases are evaluated when a select
statement executes. With a switch
statement, the cases are evaluated in order, until a match is found, then evaluation stops.
A common mistake I’ve seen is to assume that a case
at the top of a switch
statement takes priority over one lower. It does not!
Second, while order of a switches cases does not affect which one is selected, it does affect the order of evaluation. It’s rare that this should matter, but it’s good to be aware, in case your expressions may have side effects or otherwise interact with each other.
Here’s an incredibly contrived example:
chans := []<- chan int{/* add some channels */}
func getChannel() <- chan int {
ch, chans := chans[0], chans[1:]
return ch
}
select {
case <- getChannel():
case <- getChannel():
case <- getChannel():
}
Quotes from The Go Programming Language Specification Language version go1.22 (Feb 6, 2024)