By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A long if / else if chain often asks the same question over and over: is this value equal to X? A switch asks it once and lists the answers. It is easier to read, and it tells the reader immediately that one value is being routed to one block.
Give switch a value, then list cases. Each case names a possible value and a block to run. When a case matches, Go runs its block and leaves the whole switch. Cases after the match are not evaluated.
package main
import "fmt"
func main() {
code := 404
switch code {
case 200:
fmt.Println("ok")
case 404:
fmt.Println("not found")
default:
fmt.Println("something else")
}
}The default case runs when no other case matches. It is optional, but a good switch for user data usually has one, so an unexpected value at least gets a sensible message instead of silent nothing.
Cases do not have to test a single value. Separate several values with commas, and the block runs when the value equals any of them. This is where switch saves real space compared with a chain of || conditions, and the diagram shows how one value reaches exactly one block.
package main
import "fmt"
func main() {
month := "jan"
switch month {
case "dec", "jan", "feb":
fmt.Println("winter")
case "mar", "apr", "may":
fmt.Println("spring")
default:
fmt.Println("another season")
}
}Leave the value off and switch becomes a list of conditions, checked in order. switch true { is usually written as plain switch {, and the first true case runs. That makes switch a tidy replacement for a long if / else if chain, and the order of the cases matters exactly as it did there.
package main
import "fmt"
func main() {
total := 1250
switch {
case total >= 5000:
fmt.Println("large")
case total >= 1000:
fmt.Println("normal")
default:
fmt.Println("small")
}
}There is no automatic fallthrough in Go. A matched case stops at its closing brace, which is different from C and JavaScript. If you want execution to drop into the next case's block without testing it, write fallthrough as the last statement in a case. It runs the next block, and then stops there unless that case also falls through.
package main
import "fmt"
func main() {
n := 2
switch n {
case 1:
fmt.Println("one")
fallthrough
case 2:
fmt.Println("two")
fallthrough
case 3:
fmt.Println("three")
}
}Read the output: matching 2 printed two, and fallthrough forced three even though n is not 3. In real programs fallthrough is rare, and beginners almost never need it. The mistake it protects against is the opposite one: forgetting a break because other languages required it. In Go, each case ends on its own.
Write a switch that maps a Penny category to a message: "food" prints enjoy, "transport" prints get moving, and anything else prints recorded. Add "coffee" to the food case with a comma. Then rewrite the same decision as a chain of if / else if statements, and decide which version you would rather read in a month. Finally, add a program that uses plain switch with conditions to label a total as large, normal or small.
Key takeaways
switch compares one value against its cases in order and runs the first match.
Separate several values in one case with commas, and use default for the no-match path.
Bare switch (the same as switch true) tests conditions in order, like a cleaner if chain.
Go does not fall through cases automatically; fallthrough is the rare explicit exception.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.