Go Wiki: Switch
规范: https://go-lang.org.cn/ref/spec#Switch_statements
Go 的 switch
语句非常巧妙。首先,你不需要在每个 case 的末尾使用 break
。
switch c {
case '&':
esc = "&"
case '\'':
esc = "'"
case '<':
esc = "<"
case '>':
esc = ">"
case '"':
esc = """
default:
panic("unrecognized escape character")
}
不只是整数
Switch 可以处理任何类型的值。
switch syscall.OS {
case "windows":
sd = &sysDir{
Getenv("SystemRoot") + `\system32\drivers\etc`,
[]string{
"hosts",
"networks",
"protocol",
"services",
},
}
case "plan9":
sd = &sysDir{
"/lib/ndb",
[]string{
"common",
"local",
},
}
default:
sd = &sysDir{
"/etc",
[]string{
"group",
"hosts",
"passwd",
},
}
}
缺失的表达式
实际上,你甚至不需要 switch 任何东西。一个没有值的 switch 意味着“switch true”,使其成为 if-else 链的一个更简洁的版本,就像 Effective Go 中的这个例子一样。
func unhex(c byte) byte {
switch {
case '0' <= c && c <= '9':
return c - '0'
case 'a' <= c && c <= 'f':
return c - 'a' + 10
case 'A' <= c && c <= 'F':
return c - 'A' + 10
}
return 0
}
Break
Go 的 switch
语句会隐式地 break
,但 break
仍然有用。
command := ReadCommand()
argv := strings.Fields(command)
switch argv[0] {
case "echo":
fmt.Print(argv[1:]...)
case "cat":
if len(argv) <= 1 {
fmt.Println("Usage: cat <filename>")
break
}
PrintFile(argv[1])
default:
fmt.Println("Unknown command; try 'echo' or 'cat'")
}
Fall through
要 fall through 到后续的 case,请使用 fallthrough
关键字。
v := 42
switch v {
case 100:
fmt.Println(100)
fallthrough
case 42:
fmt.Println(42)
fallthrough
case 1:
fmt.Println(1)
fallthrough
default:
fmt.Println("default")
}
// Output:
// 42
// 1
// default
另一个例子
// Unpack 4 bytes into uint32 to repack into base 85 5-byte.
var v uint32
switch len(src) {
default:
v |= uint32(src[3])
fallthrough
case 3:
v |= uint32(src[2]) << 8
fallthrough
case 2:
v |= uint32(src[1]) << 16
fallthrough
case 1:
v |= uint32(src[0]) << 24
}
src/pkg/encoding/ascii85/ascii85.go
‘fallthrough’ 必须是 case 中的最后一项;你不能写成这样:
switch {
case f():
if g() {
fallthrough // Does not work!
}
h()
default:
error()
}
但是,你可以通过使用“带标签的” fallthrough
来解决这个问题。
switch {
case f():
if g() {
goto nextCase // Works now!
}
h()
break
nextCase:
fallthrough
default:
error()
}
注意:fallthrough
在 type switch 中不起作用。
多个 case
如果你想在同一个 case 中使用多个值,请使用逗号分隔的列表。
func letterOp(code int) bool {
switch chars[code].category {
case "Lu", "Ll", "Lt", "Lm", "Lo":
return true
}
return false
}
Type switch
使用 type switch,你可以根据接口值的类型进行 switch(仅限于此)。
func typeName(v interface{}) string {
switch v.(type) {
case int:
return "int"
case string:
return "string"
default:
return "unknown"
}
}
你还可以声明一个变量,它将具有每个 case
的类型。
func do(v interface{}) string {
switch u := v.(type) {
case int:
return strconv.Itoa(u*2) // u has type int
case string:
mid := len(u) / 2 // split - u has type string
return u[mid:] + u[:mid] // join
}
return "unknown"
}
do(21) == "42"
do("bitrab") == "rabbit"
do(3.142) == "unknown"
Noop case
有时,拥有不需要执行任何操作的 case 是很有用的。这看起来可能会令人困惑,因为它可能看起来像是 noop case 和后续 case 具有相同的操作,但实际上并非如此。
func pluralEnding(n int) string {
ending := ""
switch n {
case 1:
default:
ending = "s"
}
return ending
}
fmt.Sprintf("foo%s\n", pluralEnding(1)) == "foo"
fmt.Sprintf("bar%s\n", pluralEnding(2)) == "bars"
此内容是 Go Wiki 的一部分。