Back to questions
JuniorTheoryCommon
What does the program print?
package main
import "fmt"
func main() {
switch x := 5; {
case x > 3:
fmt.Print("a")
case x > 1:
fmt.Print("b")
}
}a
- Expecting
ab: Go runs only the first case whose condition is true and then leaves theswitch; it never falls into the next case on its own. - Expecting a compile error for a missing
break: Go needs nobreakat the end of a case, because leaving theswitchis the default.
- What does the program print if you add
fallthroughafterfmt.Print("a")? - How would you list several values in one
caseof aswitchwith a tag?