Go语言switch分值匹配和类型断言两类:值switch用于已知类型变量比较,case后跟编译期常量;类型switch(switch v := x.(type))专用于接口类型判断,case后跟具体类型,v自动转换为对应类型。
Go语言的switch语句既支持值匹配,也支持类型断言(type switch),但二者语法和使用场景不同,不能混用在同一分支中。关键在于:**值switch用于已知类型的变量比较;类型switch专用于接口变量,判断其底层具体类型。**
适用于整数、字符串、布尔、常量等可比较类型。每个case后跟一个或多个值,用逗号分隔,支持fallthrough(需显式写出)。
示例:
status := 404
switch status {
case 200:
fmt.Println("OK")
case 400, 404, 500:
fmt.Println("Client or server error")
default:
fmt.Println("Unknown status")
}
语法为switch v := x.(type),其中x必须是接口类型(如interface{})。每个case后跟具体类型(不是值),v在对应分支中自动转换为该类型。
case int, string:这种多类型并列(语法错误)case nil:判断接口是否为nil示例:
var i interface{} = "hello"
switch v := i.(type) {
case int:
fmt.Printf("int: %d\n", v)
case string:
fmt.Printf("string: %s\n", v) // 匹配成功,v是string类型
case bool:
fmt.Printf("bool: %t\n", v)
default:
fmt.Printf("unknown type: %T\n", v)
}
Go不支持类似其他语言中case a > 10 && b 的布尔表达式分支。若需逻辑组合,应先用if预筛选,再进switch;或在case内用if进一步判断。
示例(预判断+switch):
if a > 0 && b > 0 {
switch {
case a > 10 && b < 5:
fmt.Println("high a, low b")
case a <= 5 && b > 10:
fmt.Println("low a, high b")
default:
fmt.Println("other positive combo")
}
}
容易混淆值switch和type switch的语法结构,导致编译错误或逻辑偏差。
switch x { case 1: ... } 是值匹配;switch x.(type) { case int: ... } 是类型匹配 —— 圆括号和.type缺一不可
h会报错:switch i.(type)中i若为int,编译失败switch {})是合法语法,等价于select {},会永久阻塞