Go switch 默认不自动fallthrough以避免误执行多分支,必须显式写fallthrough且须为case末行;支持case 1, 2, 3:语法匹配多值;可省略表达式作条件判断。

Go switch 为什么不能直接 fallthrough 到下一个 case?
Go 的 switch 默认不自动穿透(fallthrough),和 C/Java 不同。这是有意设计——避免意外执行多个分支。想穿透必须显式写 fallthrough,且它只能出现在 case 最后一行(不能带语句)。
- 错误写法:
case 1: fmt.Println("one"); fallthrough; fmt.Println("after")→ 编译报错:syntax error: unexpected fallthrough, expecting semicolon or newline - 正确写法:fallthrough 必须是该
case的最后一行,且下个case不能是空分支(否则 panic) - 常见误用场景:想“匹配 1 或 2 都执行同一段逻辑”,结果忘了写
fallthrough,只进了第一个case
如何用 switch 匹配多个值(类似 Python 的 in)?
Go 不支持 case 1, 2, 3: 这种逗号分隔写法(那是老版本 Go 的遗留语法,已废弃)。但可以用逗号分隔多个 case 标签,效果等价:
switch x {
case 1, 2, 3:
fmt.Println("x is small")
case 10, 20:
fmt.Println("x is round")
default:
fmt.Println("x is other")
}
- 注意:逗号必须在同一行,且不能跨行;
case 1,换行再写2:是语法错误 - 底层是编译器展开为多个并列
case,性能无损耗 - 如果值来自 slice 或 map,不能直接用在
case中——需改用if+range或map[key]bool查表
switch 后不跟表达式:用作 if-else 替代方案
省略 switch 后的表达式,就变成“条件判断 switch”,每个 case 是布尔表达式,从上到下求值,遇到第一个为 true 的就执行并退出:
switch {
case x < 0:
fmt.Println("negative")
case x == 0:
fmt.Println("zero")
case x > 0 && x < 10:
fmt.Println("small positive")
default:
fmt.Println("large positive")
}
- 适合多条件、非等值判断(比如范围检查、组合条件)
- case 表达式支持任意 bool 类型语句,包括函数调用:
case os.IsNotExist(err): - 注意短路行为:前面的
case为 true,后面的不会执行,所以不要依赖副作用顺序
type switch 怎么安全转换接口类型?
当变量是 interface{} 或其他接口类型时,用 switch v := x.(type) 做类型断言,比一连串 if v, ok := x.(T); ok { ... } 更清晰:
立即学习“go语言免费学习笔记(深入)”;
func describe(i interface{}) {
switch v := i.(type) {
case int:
fmt.Printf("int %d\n", v)
case string:
fmt.Printf("string %q\n", v)
case bool:
fmt.Printf("bool %t\n", v)
default:
fmt.Printf("unknown type %T\n", v)
}
}
-
v在每个case中自动具有对应具体类型,无需二次断言 -
type关键字必须小写,写成Type或"type"都会编译失败 - 不能对非接口类型使用:
switch x.(type)中x必须是接口类型,否则报错invalid type assertion: x.(type) (non-interface type int on left)
switch 容易漏掉 default 分支,而实际运行中可能遇到未覆盖的类型——这时程序不会 panic,但逻辑会静默跳过,调试起来反而更难定位。










