Go 戈朗陷落
我是Golang的新手,我发现switch case语句不需要break语句来停止对案例的评估 所以,我想知道如何在go中实现这个故障行为?对此有一个明确的说明 请参见此示例:Go 戈朗陷落,go,switch-statement,fall-through,Go,Switch Statement,Fall Through,我是Golang的新手,我发现switch case语句不需要break语句来停止对案例的评估 所以,我想知道如何在go中实现这个故障行为?对此有一个明确的说明 请参见此示例: fmt.Println("First round: without fallthrough") switch 1 { case 0: fmt.Println(0) case 1: fmt.Println(1) case 2: fmt.Println(2) case 3: fmt.Print
fmt.Println("First round: without fallthrough")
switch 1 {
case 0:
fmt.Println(0)
case 1:
fmt.Println(1)
case 2:
fmt.Println(2)
case 3:
fmt.Println(3)
}
fmt.Println("Second round: with fallthrough")
switch 1 {
case 0:
fmt.Println(0)
fallthrough
case 1:
fmt.Println(1)
fallthrough
case 2:
fmt.Println(2)
fallthrough
case 3:
fmt.Println(3)
}
请在以下服务器上尝试输出:
请注意,我没有在最后一个案例中使用fallthrough语句,因为这将是一个编译时错误:无法在switch中使用fallthrough final case。感谢您的帮助。。。
First round: without fallthrough
1
Second round: with fallthrough
1
2
3