A switch compares one value against a list of cases and runs the first match. Unlike C, Java and JavaScript, a Go case does not fall through to the next one, so there is no break to forget.
Cases are checked from top to bottom, and only the first matching case runs. default runs when nothing matches; it is optional and can appear anywhere in the list, though it goes last by convention.
Multiple Values per Case
case "sat", "sun": matches either value. This is how Go expresses what C does with stacked empty cases. Any comparable type works: strings, numbers, runes, booleans, pointers, and named types such as enums:
Two cases with the same constant value are a compile error:
./main.go:10:7: duplicate case 1 (constant of type int) in expression switch
./main.go:8:7: previous case
Switch with No Condition
Leave out the expression and each case becomes a boolean test. This is Go's cleaner form of a long if/else if chain:
switch { is shorthand for switch true {. Because the first true case wins, order matters: bmi < 25 is only reached when bmi < 18.5 was false, so each case can assume the ones above failed.
Switch with an Init Statement
Like if, a switch can start with a short statement. Variables declared there are scoped to the switch:
The init statement can also be combined with a tagless switch: switch n := len(items); { case n == 0: ... }.
fallthrough
When you do want to continue into the next case, write fallthrough as the last statement of a case. It transfers control to the next case's body without evaluating its condition:
Level 2 prints write and then read, accumulating permissions downward. The conditions of the later cases are not checked: after fallthrough from case 2, case 1 runs even though level is 2.
Rules for fallthrough:
- It must be the last statement in the case.
- It cannot appear in the final case:
cannot fallthrough final case in switch. - It is not allowed in a type switch.
In practice fallthrough is rare in Go code. Multiple values per case cover the common need, and explicit function calls are usually clearer than a chain of fallthroughs.
break in a switch
break inside a case exits the switch. Cases end on their own, so you only need it to leave a case early. The trap is a switch inside a loop:
Without the loop label, break ends the switch and the loop carries on to "c". continue, on the other hand, always refers to the enclosing loop, since a switch is not a loop.
Switch on Types
A type switch branches on the dynamic type of an interface value, using the special form v.(type):
In a case with a single type, x has that type (len(x) works in the string case). In a case listing several types, x keeps the interface type. The type assertions page covers type switches and the v.(T) assertion in detail.
Things Go's switch Does Differently
| Behavior | C, Java, JavaScript | Go |
|---|---|---|
| Falling into the next case | default | only with fallthrough |
break at the end of each case | required | not needed |
| Several values per case | stacked empty cases | case a, b, c: |
| Case values | constants (mostly) | any expression, evaluated top to bottom |
| Switch value | integer types (C) | any comparable type |
| No switch expression | not allowed | switch { case cond: } |
Case expressions do not have to be constants. case limit(), max + 1: is legal; they are evaluated in order until one matches, so later expressions may never run.
Exhaustiveness
Go does not check that a switch over an enum covers every value. Add a default that logs, returns an error or panics for values you did not expect, or use the exhaustive linter (part of golangci-lint) to have missing cases reported at build time.
Frequently Asked Questions
Does Go switch fall through to the next case?
No. A Go case ends at the next case automatically; you never write break to stop it. If you want execution to continue into the next case's body, write fallthrough as the last statement of the case. It jumps unconditionally, without checking the next case's condition.
How do I match several values in one case in Go?
List them separated by commas: case "sat", "sun":. The case runs if the switch value equals any of them. This replaces the empty fall-through cases you would stack in C or Java.
What is a switch with no condition in Go?
switch { case x < 0: ...; case x == 0: ...; default: ... }. With no expression after switch, each case is a boolean condition and the first true one runs. It is the idiomatic replacement for a long if/else if chain.
What does break do inside a switch in Go?
It ends the switch, not an enclosing for loop. That is rarely needed, since cases end on their own, but it surprises people who write break inside a switch inside a loop expecting to leave the loop. Use a labeled break or return for that.