Go switch and case keywords
last modified November 19, 2026 Go 1.27
In this article we look at the switch and case
keywords in Go. This is a short keyword reference. The complete tutorial,
including the fallthrough statement, type switches, and the
optional init statement, is in the Go switch
article.
The switch keyword starts a multi-way branch. Each
case keyword introduces a clause with one or more values or
conditions, followed by the statements that run when the clause matches. The
optional default clause runs when no case matches.
The switch and case keywords
A switch compares its expression against the case values from top
to bottom and runs the first matching clause. After the clause finishes,
control leaves the switch; no break is needed.
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")
}
fmt.Println("done")
}
The value of num matches the third case. Only that clause runs,
then the program continues after the closing brace of the switch.
$ go run first_switch.go three done
The default case
The default clause handles all values that no case lists. A
switch may have at most one default, and it may
appear anywhere among the cases.
package main
import "fmt"
func describe(code int) string {
switch code {
default:
return "unknown status"
case 200:
return "OK"
case 404:
return "Not Found"
case 500:
return "Internal Server Error"
}
}
func main() {
fmt.Println(describe(200))
fmt.Println(describe(404))
fmt.Println(describe(301))
}
The default clause is written first, yet it is still chosen only
after all cases fail to match. Because every path through the
switch returns a value, the function needs no
return statement after it.
$ go run default_case.go OK Not Found unknown status
Several values in one case
A single case can list several values separated by commas. The
clause runs when the expression equals any of them.
package main
import "fmt"
func season(month string) string {
switch month {
case "December", "January", "February":
return "winter"
case "March", "April", "May":
return "spring"
case "June", "July", "August":
return "summer"
case "September", "October", "November":
return "autumn"
default:
return "no such month"
}
}
func main() {
fmt.Println(season("April"))
fmt.Println(season("October"))
fmt.Println(season("Smarch"))
}
Each case groups three months into a season. Listing values this way replaces the stacked, falling-through cases common in C.
$ go run seasons.go spring autumn no such month
Duplicate case values
Case values that are constants must be unique within a switch.
The compiler rejects a constant that appears twice.
package main
import "fmt"
func main() {
day := 6
switch day {
case 1, 2, 3, 4, 5:
fmt.Println("weekday")
case 6, 7, 6:
fmt.Println("weekend")
}
}
The value 6 is listed twice in the second case. The program does not compile, and the error points to both occurrences.
$ go run duplicate_case.go # command-line-arguments ./duplicate_case.go:12:16: duplicate case 6 (constant of type int) in expression switch ./duplicate_case.go:12:10: previous case
Case without a body
A case clause may have no statements. When it matches, nothing happens and
control leaves the switch. It does not fall through to the next
clause.
package main
import "fmt"
func main() {
lines := []string{"apple", "", "banana", "# fruit list", "cherry"}
for _, line := range lines {
switch {
case line == "":
case line[0] == '#':
default:
fmt.Println(line)
}
}
}
The program prints the lines that are neither empty nor comments. The first
two cases have no body, so they simply skip their lines. In C, the first case
would fall into the second; in Go, each empty case stands on its own. The
empty-string case also comes first for a reason: it keeps
line[0] from being evaluated on an empty string, which would
panic.
$ go run empty_case.go apple banana cherry
Order of case evaluation
A switch without an expression works like switch true.
Case expressions are evaluated from top to bottom, and evaluation stops at the
first case that is true.
package main
import "fmt"
func check(name string, result bool) bool {
fmt.Println("checking", name)
return result
}
func main() {
switch {
case check("first", false):
fmt.Println("first matched")
case check("second", true):
fmt.Println("second matched")
case check("third", true):
fmt.Println("third matched")
}
}
The check function prints its name before returning a result.
The second case matches, so the third condition is never evaluated, even
though it would also be true.
$ go run case_order.go checking first checking second second matched
Source
The Go Programming Language Specification: Switch statements
In this article we have covered the switch and case
keywords in Go. For fallthrough, type switches, and the init statement, see
the Go switch tutorial. Related keywords are
covered in Go default keyword,
Go fallthrough keyword,
Go break keyword, and
Go if/else keywords.
Author
List all Golang tutorials.