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)