ZetCode

Golang case keyword

last modified May 7, 2025

This tutorial explains how to use the case keyword in Go. We'll cover switch statement basics with practical examples of case clauses.

The case keyword defines individual branches in a switch statement. It specifies patterns to match against the switch expression's value.

In Go, case can match constant values, expressions, or use type assertions. Each case is evaluated in order until a match is found.

Basic switch with case

The simplest use of case matches constant values. This example demonstrates a basic switch statement with integer cases.

basic_case.go
package main

import "fmt"

func main() {
    num := 3
    
    switch num {
    case 1:
        fmt.Println("One")
    case 2:
        fmt.Println("Two")
    case 3:
        fmt.Println("Three")
    default:
        fmt.Println("Unknown number")
    }
}

The switch evaluates num and executes the matching case. Case 3 matches, printing "Three". The default case handles unmatched values.

Multiple values in a case

A single case can match multiple values separated by commas. This makes code more concise when several values should trigger the same behavior.

multi_case.go
package main

import "fmt"

func main() {
    day := "Tuesday"
    
    switch day {
    case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday":
        fmt.Println("Weekday")
    case "Saturday", "Sunday":
        fmt.Println("Weekend")
    default:
        fmt.Println("Invalid day")
    }
}

The first case matches any weekday, the second matches weekend days. This approach groups related values together efficiently.

Expression cases

Cases can contain expressions, not just constants. This example shows how to use comparison operations in case clauses.

expression_case.go
package main

import "fmt"

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

Without an expression after switch, cases evaluate boolean conditions. The first true case executes. Here, 85 matches the second case (B grade).

Type switch with case

Type switches use case to match types. This powerful feature helps handle different types in a clean way.

type_case.go
package main

import "fmt"

func checkType(x interface{}) {
    switch x.(type) {
    case int:
        fmt.Println("Integer")
    case float64:
        fmt.Println("Float")
    case string:
        fmt.Println("String")
    case bool:
        fmt.Println("Boolean")
    default:
        fmt.Println("Unknown type")
    }
}

func main() {
    checkType(42)
    checkType(3.14)
    checkType("hello")
    checkType(true)
}

The x.(type) syntax enables type switching. Each case matches a specific type, allowing type-specific handling in a clean structure.

Fallthrough in cases

Go's fallthrough transfers control to the next case. This example shows how it differs from automatic case termination.

fallthrough_case.go
package main

import "fmt"

func main() {
    num := 2
    
    switch num {
    case 1:
        fmt.Println("One")
    case 2:
        fmt.Println("Two")
        fallthrough
    case 3:
        fmt.Println("Three")
    case 4:
        fmt.Println("Four")
    }
}

Case 2 matches and executes, then fallthrough runs case 3 as well. Without fallthrough, only the matching case would execute.

Empty cases

Cases can be empty, which is useful when multiple conditions should execute the same code. This example demonstrates shared case behavior.

empty_case.go
package main

import "fmt"

func main() {
    month := "April"
    
    switch month {
    case "January", "February", "December":
        fmt.Println("Winter")
    case "March", "April", "May":
        fmt.Println("Spring")
    case "June", "July", "August":
        fmt.Println("Summer")
    case "September", "October", "November":
        fmt.Println("Autumn")
    }
}

Each case groups months by season. The matching case (April in Spring) executes its print statement. Empty cases share behavior efficiently.

Case with initialization

Switch statements can include initialization like if statements. This example shows how to combine initialization with cases.

init_case.go
package main

import (
    "fmt"
    "time"
)

func main() {
    switch today := time.Now().Weekday(); today {
    case time.Saturday, time.Sunday:
        fmt.Println("It's the weekend!")
    default:
        fmt.Println("It's a weekday")
    }
}

The switch initializes today with the current weekday. Cases then match against this initialized value. This keeps related code together neatly.

Source

Go language specification

This tutorial covered the case keyword in Go with practical examples of switch statements in various scenarios.

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 Golang tutorials.