Knaph

Switch

switch

Go's switch doesn't fall through by default — each case breaks automatically, which is the behavior most people actually want and forget to write in C-family languages:

switch dayNumber {
case 1, 7:
	fmt.Println("weekend")
case 2, 3, 4, 5, 6:
	fmt.Println("weekday")
default:
	fmt.Println("invalid")
}

A switch with no expression at all reads like a cleaner if/else-if chain:

switch {
case age >= 18:
	fmt.Println("adult")
case age >= 13:
	fmt.Println("teen")
default:
	fmt.Println("child")
}

Sign in to track your progress through this course.

Ask an AI

Open a ready-made prompt in ChatGPT or Claude — just press Enter.

SummarizeChatGPTClaude
Ask me questionsChatGPTClaude
Let's learn togetherChatGPTClaude