Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Keywords and Statements

The fallthrough statement

Reference for Go fallthrough: run the body of the next case in an expression switch without testing it, where it is allowed, and why it is rarely needed.

Go switch cases do not fall through by default: after a matching case runs, the switch ends. fallthrough is the explicit way to continue into the body of the next case. The next case's condition is not evaluated; its statements simply run.

fallthrough must be the last statement of a case clause. It cannot appear in the last case of a switch, and it cannot be used in a type switch or a select.

Go
switch n {
case 2:
    fmt.Println("two")
    fallthrough
case 3:
    fmt.Println("three or fell in")
}

Try it

Go Fall into the next case

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go The next condition is not tested

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
FallthroughStmt = "fallthrough" .

It has no operands. It is allowed only as the final non-empty statement in a case clause of an expression switch, and that case must not be the last one.

Usage notes

  • Only expression switches. Using fallthrough in a type switch is a compile error ("cannot fallthrough in type switch") because the variable bound in each clause has a different type.
  • Not in the last clause. The compiler rejects it with "cannot fallthrough final case in switch".
  • Condition is skipped. The next case's expression is never evaluated, so the code in the second example runs for x > 100 even though that is false. Treat it as "run the following block next".
  • Only one step. fallthrough goes into the very next clause, not to the clause after that unless that clause also ends with fallthrough. "Next" means next in source order, and that can be the default clause.
  • Prefer lists. If several values share a body, write case 1, 2, 3: instead of chained fallthrough. Use fallthrough only when one case really means "do this extra, then also do the next thing".
  • Readability. Because it is unusual in Go, add a comment above each use.
  • Other languages. C, Java and JavaScript fall through by default; Go reversed that to avoid accidental bugs.