Fallthrough statements

May 14, 2024

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.

This is one aspect of Go’s switch statements that seems to throw off a lot of people. I suppose because some languages default to a “fallthrough” behavior, while Go does not.

In particular, I see the following frequently in code written by less experienced Gophers:

switch {
	case foo:
		/* do something */
		break
	case bar:
		/* do something else */
		break
	default:
		/* do one last thing */
}

Based on what we’ve just read, this is rather silly, and redundant. 🫠 “break” is in effect the default behavior.

If you want to enable what the author of the above code probably assumed was happening, you’d need to explicitly add fallthrough instead.

So let’s look at an example of that.

for i := range 5 {
  switch {
  case i%2 == 0:
    fmt.Println(i, "is even")
    fallthrough
  case i%5 == 0:
    fmt.Println(i, "is divisible by 5")
  }
}

This code prints the following:

0 is even
0 is divisible by 5
2 is even
2 is divisible by 5
4 is even
4 is divisible by 5

Surprised?

Notice that fallthrough indicates “that control should flow from the end of this clause to the first statement of the next clause”. It does not indicate that the other conditions should be evaluated. If that’s what you want, you’ll need a different construct, such as a list of independent if statements:

for i := range 5 {
  if i%2 == 0 {
    fmt.Println(i, "is even")
  }
  if i%5 == 0 {
    fmt.Println(i, "is divisible by 5")
  }
}

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


Fallthrough statements

Fallthrough statements A “fallthrough” statement transfers control to the first statement of the next case clause in an expression “switch” statement. It may be used only as the final non-empty statement in such a clause. FallthroughStmt = "fallthrough" . Well that’s pretty straight forward. for x := range 10 { switch { case x%2 == 0: fmt.Print("X") fallthrough case x%3 == 0: fmt.Print("x") fallthrough case x%5 == 0: fmt.Print("O") } fmt.


Continue statements

Let’s continue… haha See what I did there? Bleh, okay. Continue statements A “continue” statement begins the next iteration of the innermost enclosing “for” loop by advancing control to the end of the loop block. The “for” loop must be within the same function. ContinueStmt = "continue" [ Label ] . There’s not a lot necessary to say here. continue works very much like break. The scoping rules are essentially the same.


Break statements with labels

Break statements … If there is a label, it must be that of an enclosing “for”, “switch”, or “select” statement, and that is the one whose execution terminates. In other words, you can’t break to a label that labels a more deeply nested for, switch or select statement, or to one that’s completely unrelated. But that’s only intuitive, right? OuterLoop: for i = 0; i < n; i++ { for j = 0; j < m; j++ { switch a[i][j] { case nil: state = Error break OuterLoop case item: state = Found break OuterLoop } } } Speaking of labels, did you know that you can have blank labels?

Get daily content like this in your inbox!

Subscribe