Back to questions
JuniorTheoryCommon
What does the program print?
package main
import "fmt"
func main() {
switch x := 2; x {
case 2:
fallthrough
case 3:
fmt.Print(x)
}
}2
- Expecting nothing:
fallthroughenters the next case without checking its value, socase 3runs althoughxis 2. - Expecting
3or a compile error:fallthroughonly moves to the next case and leavesxas it is, and a case may hold justfallthrough.
- What does
fallthroughdo to the next case's own condition check? - How does a type
switchdiffer from this value-matching switch?