Switch statements in a nutshell

yourbasic.org/golang

A switch is a cleaner way to write a sequence of if statements.

Basics

switch time.Now().Weekday() {
case time.Saturday:
    fmt.Println("Today is Saturday.")
case time.Sunday:
    fmt.Println("Today is Sunday.")
default:
    fmt.Println("Today is a weekday.")
}

Unlike C and Java, the case expressions do not need to be constants.

No condition

hour := time.Now().Hour()
switch { // Same as: switch true
case hour < 12:
    fmt.Println("Good morning!")
case hour < 17:
    fmt.Println("Good afternoon!")
default:
    fmt.Println("Good evening!")
}

Case lists

func whiteSpace(c rune) bool {
    switch c {
    case ' ', '\t', '\n', '\f', '\r':
        return true
    }
    return false
}

Fallthrough

switch 2 {
case 1:
    fmt.Println("1")
    fallthrough
case 2:
    fmt.Println("2")
    fallthrough
case 3:
    fmt.Println("3")
}
2
3

Exit a switch

A break statement terminates execution of the innermost for, switch, or select statement.

If you need to break out of a surrounding loop, not the switch, you can put a label on the loop and break to that label. This example shows both uses.

Loop:
    for _, ch := range "a b\nc" {
        switch ch {
        case ' ': // skip space
            break
        case '\n': // break at newline
            break Loop
        default:
            fmt.Printf("%c\n", ch)
        }
    }
a
b

Execution order

// Foo prints and returns n.
func Foo(n int) int {
    fmt.Println(n)
    return n
}

func main() {
    switch Foo(2) {
    case Foo(1), Foo(2), Foo(3):
        fmt.Println("First case")
        fallthrough
    case Foo(4):
        fmt.Println("Second case")
    }
}
2
1
2
First case
Second case

More nutshells

Core Go concepts: interfaces, slices, maps, for loops, switch statements, packages.

Share this page: