Select statements

July 15, 2024

Join me again today as I’ll be coding and answering questions on my livestream. Join me


Select statements

A “select” statement chooses which of a set of possible send or receive operations will proceed. It looks similar to a “switch” statement but with the cases all referring to communication operations.

SelectStmt = "select" "{" { CommClause } "}" .
CommClause = CommCase ":" StatementList .
CommCase   = "case" ( SendStmt | RecvStmt ) | "default" .
RecvStmt   = [ ExpressionList "=" | IdentifierList ":=" ] RecvExpr .
RecvExpr   = Expression .

So a switch statement may contain send cases, receive cases, or up to a single default. But we start with some details on receive cases, as they have the most variability.

A case with a RecvStmt may assign the result of a RecvExpr to one or two variables, which may be declared using a short variable declaration. The RecvExpr must be a (possibly parenthesized) receive operation.

As when receiving data from a channel outside of a switch statement, you may assign the received value to zero, one, or two variables.

value, ok := <- recvCh

is equivalent to:

case value, ok := <- recvCh

Also the same, you may omit the optional second boolean variable:

case value := <- recvCh

Or all of the varaibles entirely, in which case the value received is simply discarded:

case <- recvCh

And finally, as with receive operations, the receive expression may be parenthesized, which is sometimes necessary to avoid ambiguity.

case value, ok := (<-ch1)
case value := (<-ch2)
case (<-ch3)

Quotes from The Go Programming Language Specification Language version go1.22 (Feb 6, 2024)


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


Select statements, conclusion

Select statements … 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. This is to say that, given a select case such as the following, where getAChannel() returns a receiving channel: case value, ok := <- getAChannel() the right-hand side of the assignment operator (<- receiveChannel) is evaluated earlier (back in step 1), and the left-hand side is only evaluated if the case is actually selected.


Execution of a select statement

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.


Default select case

Select statements … There can be at most one default case and it may appear anywhere in the list of cases. As with a switch statement, a select statement may include either zero or one default case. The behavior of a select statement is changed in important ways when a default case is included. The details are coming soon, but I think this is a good chance to jump ahead a bit and elaborate.

Get daily content like this in your inbox!

Subscribe