func main() {
a := 2
switch a {
case 1:
fmt.Println("×")
case 2:
fmt.Println("〇")
fallthrough
case 3:
fmt.Println("×")
default:
fmt.Println("どれにも合致しませんでした。")
}
}
case 2の処理が実行された後、fallthroughすることでcase3にも入ります。
よってこのコードの実行結果は〇 ×
case 3にはfallthroughが書かれていないため、暗黙的にbreak文が実行されswitchを抜けます。
func main() {
a := 2
switch a {
case 1:
fmt.Println("×")
case 2:
fmt.Println("〇")
fallthrough
case 3:
fmt.Println("×")
fallthrough
default:
fmt.Println("どれにも合致しませんでした。")
}
}