Switch statements

May 7, 2024

Yesterday we looked at if statements, which allows conditional execution of two branches. And we saw how by chaining else if we can extend that to an arbitrary number of branches. But there’s often a cleaner way to handle multiple branches:

Switch statements

“Switch” statements provide multi-way execution. An expression or type is compared to the “cases” inside the “switch” to determine which branch to execute.

SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .

There are two forms: expression switches and type switches. In an expression switch, the cases contain expressions that are compared against the value of the switch expression. In a type switch, the cases contain types that are compared against the type of a specially annotated switch expression. The switch expression is evaluated exactly once in a switch statement.

Naturally, we’ll be looking at both types over the coming few days. But we’ll start with expression switches as the baseline.

For now, let’s just gently introduce the concept with an example, by re-writing yesterday’s else if chain into a switch, so we all know what we’re talking about.

Yesterday we saw:

if thing == 1 {
	/* ... */
} else if thing == 2 {
	/* ... */
} else if thing == 3 {
	/* ... */
}

As a switch statement, the same looks like this:

switch thing {
	case 1:
		/* ... */
	case 2:
		/* ... */
	case 3:
		/* ... */
}

Or alternately:

switch {
	case thing == 1:
		/* ... */
	case thing == 2:
		/* ... */
	case thing == 3:
		/* ... */
}

Why would anyone prefer the second form? Well, in this example, you probably wouldn’t. But if you ever need to compare something other than thing in one of the cases, then this second form can make sense:

switch {
	case thing == 1:
		/* ... */
	case thing == 2:
		/* ... */
	case thing == 3:
		/* ... */
	case otherThing == "blue":
		/* ... */
	case thing == 4 && otherThing = "red":
		/* ... */
}

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

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.


Terminating switch statements

Terminating statements … A “switch” statement in which: there are no “break” statements referring to the “switch” statement, there is a default case, and the statement lists in each case, including the default, end in a terminating statement, or a possibly labeled “fallthrough” statement. This should make good sense, but let’s look at an example, and a counter-example, to illustrate. func greet(args ...string) error { switch len(args) { case 0: return errors.


Ambiguous composite literals

I had planned to live stream this afternoon, but ran into some hardware problems that left me scrambling to get my PC booting again. It is now booting, and barring any other unforseen complications, I’ll be doing the planned boot.dev’s “Learn Web Servers” course next week at the regular time. I hope you can join me Here’s one of the bits of the Go spec you probably never knew existed (I didn’t), and will never need to care about.