← Back to Go Course | Chapter 3: Control Flow | Lesson 3 of 7

The switch Statement

A switch is a tidy way to check one value against a bunch of possible answers, instead of writing a long chain of if/else statements.

Basic switch

A switch compares an expression against a series of case values and runs the matching branch's code, then automatically stops -- there is no implicit fallthrough to the next case, which is a common point of confusion for people coming from C or Java.

Example: Basic switch

markup
package main

import "fmt"

func main() {
	day := "Tue"
	switch day {
	case "Mon":
		fmt.Println("Start of week")
	case "Tue", "Wed", "Thu":
		fmt.Println("Midweek")
	default:
		fmt.Println("Other day")
	}
}

switch with No Expression

Omitting the expression after switch turns each case into a boolean condition, evaluated in order, functioning like a tidier version of a chain of else-if statements.

Note: This form reads more cleanly than a long if/else-if chain when there are several conditions.

Example: switch with No Expression

markup
package main

import "fmt"

func main() {
	score := 82
	switch {
	case score >= 90:
		fmt.Println("A")
	case score >= 75:
		fmt.Println("B")
	default:
		fmt.Println("C")
	}
}

Using fallthrough

If you do want a case to continue into the next one, Go requires you to say so explicitly with the fallthrough keyword, making the (unusual) behavior visible in the code rather than an easy-to-miss default.

Note: fallthrough is rarely needed -- reach for it only when you deliberately want the next case's code to also run.

Example: Using fallthrough

markup
package main

import "fmt"

func main() {
	n := 1
	switch n {
	case 1:
		fmt.Println("one")
		fallthrough
	case 2:
		fmt.Println("two (via fallthrough)")
	case 3:
		fmt.Println("three")
	}
}

switch with an Init Statement

Just like if, a switch can run a short statement before the value it switches on, scoping a variable to the whole switch block -- handy for computing a value once and branching on it.

Example: switch with an Init Statement

markup
package main

import "fmt"

func classify(n int) string {
	switch remainder := n % 3; remainder {
	case 0:
		return "divisible by 3"
	default:
		return "not divisible by 3"
	}
}

func main() {
	fmt.Println(classify(9))
	fmt.Println(classify(10))
}
Common Mistakes
  1. Adding an explicit break at the end of every case out of habit from C/Java -- Go cases don't fall through by default, so it's unnecessary.
  2. Forgetting that Go's switch can be used with no expression at all, as a cleaner alternative to a long if/else if chain.
  3. Not knowing about fallthrough and being surprised that omitting it stops the next case from running automatically.
Chapter Summary
  • Go's switch automatically breaks after each case -- no fallthrough unless you ask for it with the fallthrough keyword.
  • A switch with no expression works like a cleaner chain of if/else if statements.
  • A single case can list multiple comma-separated values.
  • switch can include an init statement, just like if.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.