ZetCode

Go switch

last modified July 23, 2026

In this article we show how to work with switch statements in Go.

Go switch statement

Go's switch provides multi-way branching by comparing an expression or type against a list of cases. Unlike C, Java, or PHP, each case in Go is terminated by an implicit break, so execution does not fall through to the next case unless fallthrough is explicitly used.

Cases are evaluated from top to bottom, stopping at the first match. The switch works on values of any type, not just integers. When used without an expression, switch behaves like a cleaner if/else chain.

There are two forms: expression switches and type switches. Multiple expressions can be listed in a single case with commas. The optional default clause matches when no other case applies.

Go switch example

The following is a simple example of a switch statement in Go.

weekday.go
package main

import (
    "fmt"
    "time"
)

func main() {

    switch time.Now().Weekday() {

    case time.Monday:
        fmt.Println("Today is Monday.")
        
    case time.Tuesday:
        fmt.Println("Today is Tuesday.")
        
    case time.Wednesday:
        fmt.Println("Today is Wednesday.")
        
    case time.Thursday:
        fmt.Println("Today is Thursday.")
        
    case time.Friday:
        fmt.Println("Today is Friday.")
        
    case time.Saturday:
        fmt.Println("Today is Saturday.")
        
    case time.Sunday:
        fmt.Println("Today is Sunday.")
    }
}

In the code example, we find out the current weekday and print the corresponding message.

switch time.Now().Weekday() {

The switch statement takes an expression, which evaluates to the current weekday.

case time.Monday:
    fmt.Println("Today is Monday.")

If the weekday evaluates to time.Monday, we print the "Today is Monday" message.

Go switch multiple expressions

It is possible to place multiple expressions in one case.

weekday2.go
package main

import (
    "time"
    "fmt"
)

func main() {

    switch time.Now().Weekday() {
        
    case time.Monday, time.Tuesday, time.Wednesday, time.Thursday, time.Friday:
        fmt.Println("weekday")
    case time.Saturday, time.Sunday:
        fmt.Println("weekend")
    }
}

The example prints either weekday or weekend, depending on the evaluation of multiple expressions in two case statements.

Go switch default

The default statement can be used for all the values that do not fit the specified cases.

sizes.go
package main

import (
    "fmt"
)

func main() {

    size := "XXXL"

    switch size {

    case "XXS":
        fmt.Println("extra extra small")

    case "XS":
        fmt.Println("extra small")

    case "S":
        fmt.Println("small")

    case "M":
        fmt.Println("medium")

    case "L":
        fmt.Println("large")

    case "XL":
        fmt.Println("extra large")

    case "XXL":
        fmt.Println("extra extra large")

    default:
        fmt.Println("unknown")
    }
}

The example checks the sizes of clothes. If a value that is not recognized is used, it prints "unknown" to the terminal.

Go switch optional statement

An optional initializer statement may precede a switch expression. The initializer and the expression are separated by semicolon.

number.go
package main

import (
    "fmt"
)

func main() {

    switch num := 6; num % 2 == 0 {

    case true:
        fmt.Println("even value")

    case false:
        fmt.Println("odd value")
    }
}

In the code example, we have both the switch initializer and the expression. The switch statement determines if the value is even or odd.

switch num := 6; num % 2 == 0 {

The num := 6 is the switch initializer and the num % 2 is the switch expression.

Go switch break statement

Go uses an implicit break statement for each case. This is different from languages like C or Java, where the break is necessary. We can also specify break explicitly to exit the enclosing loop when the switch is nested inside one.

labeled_break.go
package main

import "fmt"

func main() {

    rows := [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
    target := 5

outer:
    for i, row := range rows {
        for _, val := range row {
            switch val {
            case target:
                fmt.Printf("found %d at row %d\n", target, i)
                break outer
            }
        }
    }
}

In a 2D slice of integers, we look for a target value. When found, we exit both the inner switch and the outer for loop.

outer:
    for i, row := range rows {

A label called outer is placed before the for loop.

case target:
    fmt.Printf("found %d at row %d\n", target, i)
    break outer

Inside the switch case, break outer jumps out of the labeled for loop, skipping all remaining iterations.

$ go run labeled_break.go 
found 5 at row 1

Go switch without expression

When used without an expression, the switch statement is effectively equal to switch true. This form can be used instead of multiline if/else statements to shorten code.

grade.go
package main

import "fmt"

func main() {

    score := 87

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

The example converts a numeric score into a letter grade using an expressionless switch. The first matching case is executed, and the rest are skipped — behaving like a chain of if/else statements.

$ go run grade.go
B

Go switch fallthrough

We can use the fallthrough keyword to go to the next case.

fallthrough.go
package main

import (
    "fmt"
)

// A -> B -> C -> D -> E

func main() {

    nextstop := "B"

    fmt.Println("Stops ahead of us:")

    switch nextstop {

    case "A":
        fmt.Println("A")
        fallthrough

    case "B":
        fmt.Println("B")
        fallthrough

    case "C":
        fmt.Println("C")
        fallthrough

    case "D":
        fmt.Println("D")
        fallthrough

    case "E":
        fmt.Println("E")
    }
}

Imagine that we go from A stop to E stop. We determine how many stops are ahead of us, based on the next visible stop.

$ go run fallthrough.go 
Stops ahead of us:
B
C
D
E

Go type switch

With a type switch we can switch on the type of an interface value. Multiple types can be combined in a single case.

type_switch.go
package main

import "fmt"

func main() {

    var data interface{}

    data = 112523652346.23463246345

    switch v := data.(type) {

    case string:
        fmt.Printf("string: %s\n", v)

    case int, int32, int64:
        fmt.Printf("integer type: %T value: %v\n", v, v)

    case float32, float64:
        fmt.Printf("float type: %T value: %v\n", v, v)

    case bool:
        fmt.Printf("boolean: %v\n", v)

    default:
        fmt.Printf("unknown type: %T\n", v)
    }
}

The example prints both the type and the value. Notice that integer types (int, int32, int64) and float types (float32, float64) are grouped into single cases.

$ go run type_switch.go
float type: float64 value: 1.1251265234623464e+11

Go switch in a function

A switch statement can be used inside a function to provide clean branching with early return statements.

category.go
package main

import "fmt"

func category(age int) string {

    switch {
    case age < 13:
        return "child"
    case age < 20:
        return "teen"
    case age < 65:
        return "adult"
    default:
        return "senior"
    }
}

func main() {

    fmt.Println(category(10))
    fmt.Println(category(16))
    fmt.Println(category(45))
    fmt.Println(category(70))
}

The category function classifies a person by age group using an expressionless switch with early return statements.

$ go run category.go
child
teen
adult
senior

Source

The Go Programming Language Specification

In this article we have covered the switch statement in Golang.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Go tutorials.