Expression switches

May 9, 2024

Expression switches

In an expression switch, the switch expression is evaluated and the case expressions, which need not be constants, are evaluated left-to-right and top-to-bottom; the first one that equals the switch expression triggers execution of the statements of the associated case; the other cases are skipped. If no case matches and there is a “default” case, its statements are executed. There can be at most one default case and it may appear anywhere in the “switch” statement. A missing switch expression is equivalent to the boolean value true.

ExprSwitchStmt = "switch" [ SimpleStmt ";" ] [ Expression ] "{" { ExprCaseClause } "}" .
ExprCaseClause = ExprSwitchCase ":" StatementList .
ExprSwitchCase = "case" ExpressionList | "default" .

There’s a lot packed into that paragraph. But most of it is pretty intuitive, so doesn’t need a lot of special attention.

The main takeaways from this are:

  • cases are evaluated top-to-bottom. This means that if you cases that aren’t completely exclusive of each other, the one listed first takes priority. For example:

    switch {
    	case x == 3:
    		/* x is exactly 3 */
    	case x > 0:
    		/* x is greater than 0, but not 3 */
    	default:
    		/* x is less than or equal to 0 */
    }
    
  • the location of the default case doesn’t matter. This means the following code is equivalent to the previous example:

    switch {
    	default:
    		/* x is less than or equal to 0 */
    	case x == 3:
    		/* x is exactly 3 */
    	case x > 0:
    		/* x is greater than 0, but not 3 */
    }
    
    
    wever, I think it's good practice to put the `default` case at the end, just because it's easier to comprehend that way.
    

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


Expression switches conclusion

We have just two more points (and some examples) before finishing the topic of expression switches, so let’s get to it. Expression switches … The switch expression may be preceded by a simple statement, which executes before the expression is evaluated. You may recall the discussion of “simple statements” from before. They can exist in an if expression as well. switch x := foo(); x { In this example, x := foo() is the simple statement.


Fallthrough statements

Expression switches … In a case or default clause, the last non-empty statement may be a (possibly labeled) “fallthrough” statement to indicate that control should flow from the end of this clause to the first statement of the next clause. Otherwise control flows to the end of the “switch” statement. A “fallthrough” statement may appear as the last statement of all but the last clause of an expression switch.


Case expressions

I’m back live streaming again! Join me in just over an hour! Bring your questions, too! Expression switches … If a case expression is untyped, it is first implicitly converted to the type of the switch expression. For each (possibly converted) case expression x and the value t of the switch expression, x == t must be a valid comparison. In other words, the switch expression is treated as if it were used to declare and initialize a temporary variable t without explicit type; it is that value of t against which each case expression x is tested for equality.