我正在阅读本书中关于 Go 中 switch 语句的部分。但是这个例子让我感到困惑:
package main
import "fmt"
func main() {
k := 6
switch k {
case 4: fmt.Println("was <= 4"); fallthrough;
case 5: fmt.Println("was <= 5"); fallthrough;
case 6: fmt.Println("was <= 6"); fallthrough;
case 7: fmt.Println("was <= 7"); fallthrough;
case 8: fmt.Println("was <= 8"); fallthrough;
default: fmt.Println("default case")
}
}
输出是:
was <= 6
was <= 7
was <= 8
default case
并且书中指出:
使用 fallthrough 语句来指示必须执行当前块之后的 case 块。
现在我要提问:
为什么 Go 默认比较,在这种情况下 k 较低?
文中提到执行以下情况。美好的。但是为什么他们不只执行与 k 匹配的案例?
烙印99
相关分类