Go switch Statement

Su Suriya Ravichandran Updated 16 Sep 2026
3 min read

The switch statement selects one of several code blocks to run, based on the value of an expression. It's a cleaner, more readable alternative to a long chain of if...else if statements when you're comparing one value against several possibilities.

Go's switch has a notable difference from C, C++, Java, or JavaScript: it only runs the first matching case and then stops automatically — there is no fall-through by default, and no break statement is needed.

Basic Syntax

switch expression {
case value1:
    // code block
case value2:
    // code block
case value3:
    // code block
default:
    // code block (optional)
}

How switch Works

  1. The expression is evaluated exactly once.
  2. Its result is compared against each case value, in order.
  3. When a match is found, that block runs, and the switch statement ends immediately — no other cases are checked.
  4. If no case matches, the optional default block runs.

Example: Displaying a Day Name

package main

import "fmt"

func main() {
    dayNumber := 5

    switch dayNumber {
    case 1:
        fmt.Println("Monday")
    case 2:
        fmt.Println("Tuesday")
    case 3:
        fmt.Println("Wednesday")
    case 4:
        fmt.Println("Thursday")
    case 5:
        fmt.Println("Friday")
    case 6:
        fmt.Println("Saturday")
    case 7:
        fmt.Println("Sunday")
    }
}

Expected output:

Friday

Example: Using the default Case

default acts as a fallback when no other case matches — including it is optional, but strongly recommended so unexpected values don't pass through silently.

package main

import "fmt"

func main() {
    score := 105

    switch score {
    case 0, 1, 2, 3, 4:
        fmt.Println("Very Low Score")
    case 5, 6, 7, 8:
        fmt.Println("Average Score")
    case 9, 10:
        fmt.Println("Excellent Score")
    default:
        fmt.Println("Invalid score")
    }
}

Expected output:

Invalid score

Key Features of Go's switch

  • No break needed — each case terminates automatically once its block finishes.
  • A single case can list multiple comma-separated values, all mapping to the same block.
  • default is optional but improves robustness by handling unanticipated values explicitly.
  • Reads more cleanly than an equivalent chain of if...else if statements once you have more than two or three branches.

If you do want fall-through: Go supports it explicitly, with the fallthrough keyword placed at the end of a case block. This is rarely used, and it's worth knowing it exists mainly so you recognize it if you see it — most Go code never needs it, precisely because each case not falling through by default is usually the behavior you want.

Multi-Value Cases

A single case can match multiple values by separating them with commas, letting you group related values without duplicating code.

switch expression {
case value1, value2:
    // runs if expression equals value1 OR value2
case value3, value4:
    // runs if expression equals value3 OR value4
default:
    // runs if nothing matches
}

Example: Categorizing Days of the Week

package main

import "fmt"

func main() {
    day := 6

    switch day {
    case 1, 3, 5:
        fmt.Println("Weekday: Odd schedule")
    case 2, 4:
        fmt.Println("Weekday: Even schedule")
    case 6, 7:
        fmt.Println("Weekend: Time to relax")
    default:
        fmt.Println("Invalid day number")
    }
}

Expected output:

Weekend: Time to relax

Example: Grouping User Roles

package main

import "fmt"

func main() {
    role := "editor"

    switch role {
    case "admin", "superuser":
        fmt.Println("Full system access granted")
    case "editor", "author":
        fmt.Println("Content editing permissions granted")
    case "viewer", "guest":
        fmt.Println("Read-only access")
    default:
        fmt.Println("Unknown role")
    }
}

Expected output:

Content editing permissions granted

Switch Without an Expression

Go also allows a switch with no expression at all, in which case each case is itself a boolean condition — this form works like a cleaner if...else if chain:

package main

import "fmt"

func main() {
    hour := 14

    switch {
    case hour < 12:
        fmt.Println("Good morning!")
    case hour < 18:
        fmt.Println("Good afternoon!")
    default:
        fmt.Println("Good evening!")
    }
}

This form is common in real Go code whenever the branches depend on different conditions rather than the same variable being compared to fixed values.

Common Mistakes

  • Expecting fall-through by default, as in C or Java — Go's switch stops after the first match unless you explicitly write fallthrough.
  • Forgetting the default case and being surprised when an unexpected value produces no output at all, rather than an error — a switch with no matching case and no default simply does nothing.

0 Comments

Reviewed before they appear

No comments yet.

Go Lang
Ask about this post
AI Ask about this post

Ask questions about Go switch Statement and get answers drawn from it.

Signed-in readers only.