Go Reference ยท Keywords and Statements
The switch statement
Reference for Go switch: expression switches without implicit fall-through, multi-value cases, tagless switches, init statements, fallthrough and break.
switch compares one value against a list of cases and runs the first one that matches. Unlike C and Java, Go cases do not fall through: when a case body finishes, the switch is over, so you rarely write break. A case can list several values, and the case values do not need to be constants.
If you omit the tag (the value after switch), the switch behaves like a chain of if/else if: each case is a boolean condition and the first one that is true wins. This "tagless" form is the tidy way to express ranges.
switch day { case "Sat", "Sun": fmt.Println("weekend") default: fmt.Println("weekday") }
Try it
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.
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
switch init; tag { case v1, v2: ... case v3: ... default: ... } switch { // tagless: cases are conditions case cond1: ... }
| Part | Description |
|---|---|
init |
Optional simple statement; its variables are scoped to the switch. |
tag |
Optional expression, evaluated once. Any comparable type works. |
case list |
Expressions compared with == to the tag, left to right, top to bottom. In a tagless switch each must be a bool. |
default |
Runs if nothing matches. At most one; it may appear anywhere, but the convention is last. |
Errors and edge cases:
- Duplicate constant cases are a compile error (
duplicate case 1 in expression switch) for integers, strings and similar constants. fallthroughmust be the last statement of a case, cannot be used in the final case, and cannot be used in a type switch.- Comparing incomparable values (slices, maps, functions) as the tag panics at run time or fails to compile; compare with
nilin a tagless switch instead. - If the tag is omitted, it is the constant
true, socase x > 5:works.
Usage notes
- No implicit fall-through is the main difference from C-family languages, and it removes a whole class of forgotten-
breakbugs.fallthroughexists, but is rarely needed. breakleaves the switch, not the enclosing loop. Use a label (break loop) or move the switch into a function andreturn.continueinside a switch inside a loop continues the loop.- Case values may be arbitrary expressions (including function calls) and are evaluated lazily, in order, until one matches.
- Switch on types uses a different form: see type switch.
- Prefer switch over long
else ifchains when the same value is compared repeatedly or when you test ranges with a tagless switch. - Exhaustiveness is not checked: adding a new enum constant does not make existing switches fail to compile. Linters such as
exhaustivefill this gap.