A switch statement in Go lets you choose between many options based on the value of an expression. It is like asking multiple questions and picking the answer that matches.
Click to reveal answer
beginner
How do you write a basic switch statement in Go?
Use the keyword switch followed by a value. Then write case for each option and default for when no case matches. Example:
switch x {
case 1:
// do something
case 2:
// do something else
default:
// fallback
}
Click to reveal answer
beginner
What happens if no case matches and there is no default in a Go switch?
The switch does nothing and the program continues after the switch block. No code inside the switch runs.
Click to reveal answer
intermediate
Can a Go switch statement have multiple values in one case?
Yes! You can list multiple values separated by commas in one case. For example:
switch day {
case "Saturday", "Sunday":
fmt.Println("Weekend")
default:
fmt.Println("Weekday")
}
Click to reveal answer
intermediate
How does Go switch differ from if-else chains?
Go switch is cleaner and easier to read when checking one value against many options. It also stops checking after the first match without needing breaks, unlike some other languages.
Click to reveal answer
What keyword starts a switch statement in Go?
Aswitch
Bcase
Cselect
Dif
✗ Incorrect
The switch statement always starts with the keyword switch.
What happens if no case matches and there is no default case?
AThe switch block is skipped
BAn error is shown
CThe first case runs anyway
DThe program crashes
✗ Incorrect
If no case matches and no default is given, the switch block does nothing and the program continues.
How do you write multiple values in one case?
AWrite multiple case statements
BUse the OR operator (||)
CUse semicolons between values
DSeparate values with commas
✗ Incorrect
In Go, you list multiple values separated by commas in a single case.
Does Go switch require break statements to stop after a case?
AOnly if you want to skip cases
BYes, always
CNo, it stops automatically
DOnly in default case
✗ Incorrect
Go switch stops after the first matching case automatically, so no break is needed.
Which of these is a valid switch statement header in Go?
Aswitch x then {
Bswitch x {
Cswitch (x) {
Dswitch on x {
✗ Incorrect
The correct syntax is switch x { without parentheses or extra words.
Explain how a switch statement works in Go and how it differs from if-else.
Think about how Go picks one matching case and stops.
You got /5 concepts.
Write a simple Go switch statement that prints "Weekend" for Saturday and Sunday, and "Weekday" for other days.
Use commas to list Saturday and Sunday in one case.
You got /4 concepts.
Practice
(1/5)
1. What does a switch statement do in Go?
easy
A. It selects one case to run based on a value.
B. It repeats code multiple times like a loop.
C. It defines a new function.
D. It declares a variable.
Solution
Step 1: Understand the purpose of switch
A switch statement chooses one path among many based on a value.
It selects one case to run based on a value. -> Option A
Quick Check:
Switch = Select case [OK]
Hint: Switch picks one matching case to run [OK]
Common Mistakes:
Confusing switch with loops
Thinking switch defines functions
Believing switch declares variables
2. Which of the following is the correct syntax for a basic switch statement in Go?
easy
A. switch (x) { case 1: fmt.Println("One") default: fmt.Println("Other") }
B. switch x { case 1: fmt.Println("One") default: fmt.Println("Other") }
C. switch x: case 1: fmt.Println("One") default: fmt.Println("Other")
D. switch x { case 1 => fmt.Println("One") default => fmt.Println("Other") }
Solution
Step 1: Recall Go switch syntax
Go uses switch variable { case value: ... default: ... } without parentheses.
Step 2: Check each option
switch x { case 1: fmt.Println("One") default: fmt.Println("Other") } matches correct syntax. switch (x) { case 1: fmt.Println("One") default: fmt.Println("Other") } wrongly uses parentheses. switch x: case 1: fmt.Println("One") default: fmt.Println("Other") uses colon after switch. switch x { case 1 => fmt.Println("One") default => fmt.Println("Other") } uses => which is invalid.
Final Answer:
switch x { case 1: fmt.Println("One") default: fmt.Println("Other") } -> Option B
Quick Check:
Correct syntax = switch x { case 1: fmt.Println("One") default: fmt.Println("Other") } [OK]
Hint: No parentheses around switch variable in Go [OK]
Common Mistakes:
Adding parentheses around switch variable
Using colon after switch keyword
Using => instead of colon in cases
3. What will be the output of this Go code?
package main
import "fmt"
func main() {
x := 2
switch x {
case 1:
fmt.Println("One")
case 2:
fmt.Println("Two")
case 3:
fmt.Println("Three")
default:
fmt.Println("Other")
}
}
medium
A. Two
B. Three
C. One
D. Other
Solution
Step 1: Identify the switch variable and value
The variable x is 2.
Step 2: Match the case with value 2
The case 2 matches and prints "Two".
Final Answer:
Two -> Option A
Quick Check:
Switch x=2 prints "Two" [OK]
Hint: Match switch value to case label [OK]
Common Mistakes:
Choosing default instead of matching case
Confusing case numbers
Ignoring switch variable value
4. Find the error in this Go switch code:
package main
import "fmt"
func main() {
x := 3
switch x {
case 1
fmt.Println("One")
case 2:
fmt.Println("Two")
default:
fmt.Println("Other")
}
}
medium
A. fmt.Println syntax error
B. Missing default case
C. Switch variable must be string
D. Missing colon after case 1
Solution
Step 1: Check case syntax
Each case must end with a colon (:). Here, case 1 misses the colon.
Step 2: Verify other parts
Default case exists, switch variable can be int, fmt.Println is correct.
Final Answer:
Missing colon after case 1 -> Option D
Quick Check:
Case syntax requires colon [OK]
Hint: Each case must end with a colon (:) [OK]
Common Mistakes:
Omitting colon after case
Thinking default is mandatory
Assuming switch variable must be string
5. How can you write a Go switch statement that prints "Weekend" for Saturday and Sunday, and "Weekday" for other days (1=Monday, 7=Sunday)?
hard
A. switch day {
case day == 6 || day == 7:
fmt.Println("Weekend")
default:
fmt.Println("Weekday")
}
B. switch day {
case 6:
fmt.Println("Weekend")
case 7:
fmt.Println("Weekday")
default:
fmt.Println("Weekday")
}
C. switch day {
case 6, 7:
fmt.Println("Weekend")
default:
fmt.Println("Weekday")
}
D. switch day {
case 6:
fmt.Println("Weekend")
case 7:
fmt.Println("Sunday")
default:
fmt.Println("Weekday")
}
Solution
Step 1: Group weekend days in one case
Using case 6, 7: groups Saturday and Sunday together.
Step 2: Use default for other days
Default prints "Weekday" for days 1 to 5.
Final Answer:
switch day {
case 6, 7:
fmt.Println("Weekend")
default:
fmt.Println("Weekday")
} -> Option C
Quick Check:
Multiple values in one case = switch day {
case 6, 7:
fmt.Println("Weekend")
default:
fmt.Println("Weekday")
} [OK]
Hint: Use commas to group cases in Go switch [OK]
Common Mistakes:
Separating weekend days into different cases with wrong output
Using logical OR inside switch expression incorrectly
Missing default case for weekdays
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.