Expression switches conclusion

May 15, 2024

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)


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


Omitting for clause elements

For statements with for clause … Any element of the ForClause may be empty but the semicolons are required unless there is only a condition. If the condition is absent, it is equivalent to the boolean value true. for cond { S() } is the same as for ; cond ; { S() } for { S() } is the same as for true { S() } So this goes to show that the single condition form of the for loop is really just a special case of the “for clause” for loop, with the init and post statements omitted.


For statements with for clause

Today we’ll look at the second, of three, types of for statements: Those with a for clause. For statements with for clause A “for” statement with a ForClause is also controlled by its condition, but additionally it may specify an init and a post statement, such as an assignment, an increment or decrement statement. The init statement may be a short variable declaration, but the post statement must not. ForClause = [ InitStmt ] ";" [ Condition ] ";" [ PostStmt ] .


Type switches conclusion

Two final items with regard to type switches, before we move on to the next topic: Type switches … The type switch guard may be preceded by a simple statement, which executes before the guard is evaluated. This is pretty straight forward. It works just like the simple statements in if statements and normal switch statements. A simple statement can be used both with or without a temporary variable. In other words, both of these are valid:

Get daily content like this in your inbox!

Subscribe