Priority of select cases

July 22, 2024

I'll be live streaming again today! Today I'll be setting up GitHub Actions for a Go project. So come with your CI/CD questions! [See you there!](https://www.youtube.com/watch?v=RM6DfN1CKkE)

Select statements

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

Remember when I said last week that, unlike a switch statement, select does not execute from the top to bottom? Here’s where that’s spelled out for us.

If only a single communication can proceed, that case executes.

If two or more can succeed at the time the select is executed, then an arbitrary one is chosen, and executed.

Wait, but what if you want one channel to take priority over another? This is a legitimate desire sometimes. Then you might want to nest one or more select:

select {
case <- highPriorityChannel:
	/* Do high priority things */
default:
	select {
	case <- lowPriorityChannel1:
		/* Do low priority things */
	case <- lowPriorityChannel1:
		/* Do other low priority things */
	default:
		/* None of the channels were ready */
}
  1. Unless the selected case is the default case, the respective communication operation is executed.

Naturally, a default case doesn’t have a communication operation, so it can’t be executed!

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