When there are too many paths

The if/else chain is fine for two or three conditions, but it degrades fast. By the fourth branch the structure becomes harder to scan, and readers lose track of which value is being compared. A switch fixes that: it makes the subject of comparison explicit, groups each possible outcome into its own named case, and keeps the code flat.

Go's switch does more than the one you might know from C or Java. Cases don't fall through by default, each case is its own scope, a single case can match multiple values, and, like for, the statement supports an initialization clause.

Basic switch

The simplest form takes an expression and compares it against a list of values:

Go
day := "Tuesday"switch day {case "Monday":    fmt.Println("Start of the work week")case "Tuesday", "Wednesday", "Thursday":    fmt.Println("Midweek")case "Friday":    fmt.Println("Almost there")default:    fmt.Println("Weekend")}

The switch evaluates day once and checks each case in order from top to bottom. The first matching case runs and control exits the entire switch without a break. If no case matches, default runs. The default case is optional and can appear anywhere in the list, though placing it last is conventional.

Switch with initialization

Like for, the switch statement accepts an optional initialization clause separated from the expression by a semicolon. The variable declared in the init clause is scoped to the entire switch block, including all cases:

Go
switch os := runtime.GOOS; os {case "linux":    fmt.Println("Running on Linux")case "darwin":    fmt.Println("Running on macOS")default:    fmt.Printf("Running on %s\n", os)}

Here os is declared and assigned in the init clause, then used as the switch expression on the same line. Once the switch block ends, os goes out of scope, so it never leaks into the rest of the function.

Each case is its own scope

Unlike many languages, each case in Go implicitly introduces its own code block. This means you can declare variables inside a case without worrying about collisions with other cases:

Go
switch status {case 200:    msg := "OK"    fmt.Println(msg)case 404:    msg := "Not Found" // no conflict with the msg above    fmt.Println(msg)}

Both cases declare msg, and Go treats them as completely independent variables. In languages without per-case scoping, this would require explicit braces or cause a redeclaration error.

Matching multiple conditions in one case

A single case can list multiple values separated by commas. If the switch expression matches any of them, the case runs:

Go
switch day {case "Saturday", "Sunday":    fmt.Println("Weekend")case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday":    fmt.Println("Weekday")}

This is more readable than duplicating the case body or chaining conditions. Whenever two or more values share the same behavior, group them into one case.

Expressionless switch

Go allows omitting the switch expression entirely. When you do, it behaves like switch true: each case is evaluated as a boolean expression, and the first one that is true runs:

Go
score := 78switch {case score >= 90:    fmt.Println("A")case score >= 80:    fmt.Println("B")case score >= 70:    fmt.Println("C")default:    fmt.Println("F")}

This form is a clean replacement for a long if/else if chain, especially when the conditions involve ranges or complex expressions rather than equality checks.

The fallthrough keyword

Go's default of no fall-through is almost always what you want, but occasionally you need a case to continue into the next one on purpose. The fallthrough keyword does that:

Go
n := 5switch {case n > 0:    fmt.Println("positive")    fallthroughcase n > -10:    fmt.Println("greater than -10")}

Output:

positive
greater than -10

When the first case matches and fallthrough is reached, execution jumps into the body of the next case without evaluating that case's condition.

Multi-condition cases vs fallthrough

Both features let a single code path handle multiple scenarios, but they behave differently and read differently:

Multi-condition casefallthrough
Conditions checkedAll values matched explicitlyOnly the first matching case
Next case conditionNot applicableSkipped: body runs unconditionally
ReadabilityClear intent, easy to scanLess obvious, requires mental tracking
Best forValues that share the same behaviorRare cases that really need to cascade

In practice, the vast majority of fallthrough uses can be replaced with a multi-condition case:

Go
// Prefer this:switch cmd {case "quit", "exit", "q":    os.Exit(0)}// Over this:switch cmd {case "quit":    fallthroughcase "exit":    fallthroughcase "q":    os.Exit(0)}

The multi-condition version states intent directly. The fallthrough chain forces the reader to trace through three cases to understand that they all do the same thing. Keep fallthrough for the rare situation where cases with distinct bodies need to cascade.