Default select case

July 16, 2024

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.

Without a default, case, a select statement blocks until either a send or receive operation succeeds:

select {
	case <- recvA:
	case <- recvB:
	case sendA <- x:
}
fmt.Println("done!")

for as long as neither recvA nor recvB send data, and sendA is incapable of being sent data (by virtue of being nil, or not having available capacity) done! won’t print.

But if we add a default case:

select {
	case <- recvA:
	case <- recvB:
	case sendA <- x:
	default:
		fmt.Println("default")
}
fmt.Println("done!")

Now the select statement is going to finish very quickly. If none of the channels are able to receive/send, then the default case triggers immediately, and we’ll see default printed followed by done!, immediately.

On the other hand, if, at the time the select statement is executed, one or more of the channels is available for sending or receiving, then of the available channels, one is selected, and its case executed.

More on how that’s all handled in the coming couple of days.

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.


Select statements

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" .

Get daily content like this in your inbox!

Subscribe