14.11 Fallthrough 语句

fallthrough 语句将控制权转移到表达式 switch 语句中下一个(或默认)case 子句的第一个语句。fallthrough 语句在类型 switch 语句中不允许使用。

在以下示例中,当 x 大于 100 时,它会输出三行,这是所有真实语句。当 x 大于 10 但小于或等于 100 时,它会输出两行。当 x 在 1 和 10 之间时,它会输出一行,我是确定的。

switch {
case x > 100:
  fmt.Println("I'm bigger than 100")
  fallthrough
case x > 10:
  fmt.Println("I'm bigger than 10")
  fallthrough
case x > 0:
  fmt.Println("I'm positive")
default:
  fmt.Println("I'm NOT positive")
}

最后更新于