Skip to main content

Introduction

Switch statements provide a clean way to express conditionals across many branches. Go’s switch is more powerful than in many other languages, supporting multiple expressions per case, automatic break behavior, and even type switches.

The Code

// _Switch statements_ express conditionals across many
// branches.

package main

import (
	"fmt"
	"time"
)

func main() {

	// Here's a basic `switch`.
	i := 2
	fmt.Print("Write ", i, " as ")
	switch i {
	case 1:
		fmt.Println("one")
	case 2:
		fmt.Println("two")
	case 3:
		fmt.Println("three")
	}

	// You can use commas to separate multiple expressions
	// in the same `case` statement. We use the optional
	// `default` case in this example as well.
	switch time.Now().Weekday() {
	case time.Saturday, time.Sunday:
		fmt.Println("It's the weekend")
	default:
		fmt.Println("It's a weekday")
	}

	// `switch` without an expression is an alternate way
	// to express if/else logic. Here we also show how the
	// `case` expressions can be non-constants.
	t := time.Now()
	switch {
	case t.Hour() < 12:
		fmt.Println("It's before noon")
	default:
		fmt.Println("It's after noon")
	}

	// A type `switch` compares types instead of values.  You
	// can use this to discover the type of an interface
	// value.  In this example, the variable `t` will have the
	// type corresponding to its clause.
	whatAmI := func(i interface{}) {
		switch t := i.(type) {
		case bool:
			fmt.Println("I'm a bool")
		case int:
			fmt.Println("I'm an int")
		default:
			fmt.Printf("Don't know type %T\n", t)
		}
	}
	whatAmI(true)
	whatAmI(1)
	whatAmI("hey")
}

Switch Variants

Basic Switch

i := 2
switch i {
case 1:
	fmt.Println("one")
case 2:
	fmt.Println("two")
case 3:
	fmt.Println("three")
}
The standard switch compares a value against multiple cases. Unlike C or Java, Go automatically breaks after each case - no break statement needed!
Go’s switch cases break automatically. You don’t need (and shouldn’t use) explicit break statements unless you want to exit early from within a case.

Multiple Expressions in Case

switch time.Now().Weekday() {
case time.Saturday, time.Sunday:
	fmt.Println("It's the weekend")
default:
	fmt.Println("It's a weekday")
}
Use commas to test multiple values in a single case. The default case runs when no other cases match.

Expression-less Switch (If/Else Alternative)

t := time.Now()
switch {
case t.Hour() < 12:
	fmt.Println("It's before noon")
default:
	fmt.Println("It's after noon")
}
A switch without an expression is equivalent to switch true. This provides a cleaner way to write long if-else chains.
Use expression-less switch when you have multiple unrelated conditions to check. It’s more readable than multiple if/else if statements.

Type Switch

whatAmI := func(i interface{}) {
	switch t := i.(type) {
	case bool:
		fmt.Println("I'm a bool")
	case int:
		fmt.Println("I'm an int")
	default:
		fmt.Printf("Don't know type %T\n", t)
	}
}
whatAmI(true)   // I'm a bool
whatAmI(1)      // I'm an int
whatAmI("hey")  // Don't know type string
Type switches compare types instead of values. This is useful when working with interfaces to determine the underlying type.
In a type switch, the variable t has a different type in each case block, matching the type specified in that case.

Switch Features

Automatic Break

Unlike C/Java, Go switches break automatically - no fall-through by default.

Non-Constant Cases

Cases can be expressions, not just constants:
switch {
case x > 10:
    // ...
case y < 5:
    // ...
}

Default Case

The default case is optional and can appear anywhere (though by convention it’s usually last):
switch x {
default:
    fmt.Println("default")
case 1:
    fmt.Println("one")
}

Common Patterns

Value Matching

switch status {
case "active":
    // handle active
case "pending":
    // handle pending
case "inactive":
    // handle inactive
}

Range Checking

switch {
case age < 18:
    category = "minor"
case age < 65:
    category = "adult"
default:
    category = "senior"
}

Type Detection

switch v := x.(type) {
case string:
    fmt.Printf("string: %s\n", v)
case int:
    fmt.Printf("int: %d\n", v)
}

Switch vs If/Else

Use Switch WhenUse If/Else When
Multiple values for one variableDifferent variables in conditions
Checking type of interfaceComplex boolean logic
Many branches (3+)Simple binary decision

Key Takeaways

  • Switch cases break automatically - no explicit break needed
  • Multiple values can be tested in one case using commas
  • Expression-less switch (switch { }) is great for if/else chains
  • Type switches determine the type of interface values
  • Cases can be expressions, not just constants
  • The default case is optional and handles unmatched cases
  • Go’s switch is more powerful and flexible than in C/Java

Build docs developers (and LLMs) love