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. Any variables declared in that statement are scoped to the rest of the switch statement. This means the following two snippets are roughly equivalent, with regard to variable scope:
switch x := foo(); x {
case foo:
/* do something */
break
case bar:
/* do something else */
break
default:
/* do one last thing */
}
and
{
x := foo();
switch x {
case foo:
/* do something */
break
case bar:
/* do something else */
break
default:
/* do one last thing */
}
}
The spec the continues with its own examples, which we won’t dwell on, since we’ve already covered a number of our own examples in detail:
switch tag { default: s3() case 0, 1, 2, 3: s1() case 4, 5, 6, 7: s2() } switch x := f(); { // missing switch expression means "true" case x < 0: return -x default: return x } switch { case x < y: f1() case x < z: f2() case x == 4: f3() }
And then finally, one of those implementation restrictions that make for great trivia night fodder at your local Go meetup:
Implementation restriction: A compiler may disallow multiple case expressions evaluating to the same constant. For instance, the current compilers disallow duplicate integer, floating point, or string constants in case expressions.
What this means is that the following code may or may not compile, depending on the implementation—but it does fail to compile in the “current compilers” (or at least the ones known to the spec authors at the time of writing).
switch x {
case 1:
case 1:
case 2:
}
Quotes from The Go Programming Language Specification Language version go1.22 (Feb 6, 2024)