Golang switch 语句

简介

switch 语句提供了一种简洁的方式来执行多路分支选择

基本使用

  1. 基本语法如下:
go 复制代码
switch expression {
case value1:
    // 当 expression 的值等于 value1 时执行
case value2:
    // 当 expression 的值等于 value2 
  1. switch 的每个分支自动提供了隐式的 break,意味着只有第一个匹配的 case 会被执行。不需要显式地在每个 case 后面加 break

  2. 如果你想要一个 case 执行后继续检查下一个 case,可以使用 fallthrough 语句:

go 复制代码
switch x {
case 1:
    fmt.Println("x is 1")
    fallthrough
case 2:
    fmt.Println("x is 2")
    fallthrough
case 3:
    fmt.Println("x is 3")
default:
    fmt.Println("x is not 1, 2, or 3")
}

在上面的代码中,如果 x 是 1,它会打印出 "x is 1" 和 "x is 2",因为 fallthrough 语句导致程序继续执行下一个 case

  1. switch 也支持不带表达式的用法,其中每个 case 可以包含一个条件表达式:
go 复制代码
y := 20

switch {
case y > 10:
    fmt.Println("y is greater than 10")
case y == 10:
    fmt.Println("y is exactly 10")
default:
    fmt.Println("y is less than 10")
}

在这种情况下,switch 语句类似于一系列的 if-else 语句,但其语法更加清晰

  1. switch 也可以包含初始化语句,类似于 if 语句:
go 复制代码
switch z := computeValue(); {
case z > 10:
    fmt.Println("z is greater than 10")
case z == 10:
    fmt.Println("z is exactly 10")
default:
    fmt.Println("z is less than 10")
}

常见用法

  • 类型判断: switch 可以用来进行类型判断,在这种情况下,它会匹配一个接口变量的动态类型
go 复制代码
var i interface{} = /* 一个值 */

switch t := i.(type) {
case string:
    fmt.Println("i is a string:", t)
case int:
    fmt.Println("i is an int:", t)
default:
    fmt.Printf("Unknown type %T\n", t)
}

在这个例子中,i.(type) 用来发现接口变量 i 的动态类型

注意:i.(type) 用于 switch 语句中进行类型断言的类型判断。它只能在 switch 的类型判断分支中使用,不可以单独使用在其他地方

  • 多值匹配: 你可以在一个 case 语句中测试多个值。这可以简化代码,避免编写多个具有相同结果的 case 语句
go 复制代码
switch x {
case 1, 2, 3:
    fmt.Println("x is 1, 2 or 3")
default:
    fmt.Println("x is not 1, 2, or 3")
}
  • 条件组合: case 语句中可以包含多个条件,这些条件可以是逻辑表达式
go 复制代码
switch {
case x > 0 && x < 10:
    fmt.Println("x is between 1 and 9")
case x == 10 || x == 20:
    fmt.Println("x is either 10 or 20")
}
  • 无条件的 switch: 无条件的 switch 相当于一个更优雅的 if-else 链
go 复制代码
switch {
case score >= 90:
    fmt.Println("Grade: A")
case score >= 80:
    fmt.Println("Grade: B")
case score >= 70:
    fmt.Println("Grade: C")
default:
    fmt.Println("Grade: F")
}
  • 退出循环: 在循环内部,switch 可以与 break 语句一起使用来退出循环
go 复制代码
for {
    switch {
    case someCondition():
        fmt.Println("Condition met")
        break // 默认只会跳出 switch
    default:
        fmt.Println("Default case")
    }
    break // 退出 for 循环
}

请注意,在这种情况下,break 语句只会退出 switch,而不是循环。要退出循环,需要在外部再次使用 break 语句

  • 跳过当前迭代: 在循环中使用 switch,可以配合 continue 语句来跳过当前迭代
go 复制代码
for x := 0; x < 5; x++ {
    switch {
    case x%2 == 0:
        // 跳过偶数
        continue
    }
    fmt.Println("Odd:", x)
}
相关推荐
码士集团小青14 小时前
从对标 Java 到对标 Go:Native AOT 的“无痛化“之路,走到哪一站了?
golang
右耳朵猫AI15 小时前
Go周刊2026W36 | Go 1.27.1 发布、TinyGo 0.42、HTTP/2 原生迁入 net/http、quic-go 0.62
redis·http·golang
大树911 天前
把 Java 项目从手动 SCP 升级到 Gitee Go 自动部署:一份踩坑实录
java·golang·gitee
lmy_loveF1 天前
go 切换go version 版本
开发语言·后端·golang
智购科技自动贩卖机1 天前
自动售货机嵌入式系统安全加固实践:从Go语言国密算法到硬件防拆的工程化落地
大数据·人工智能·后端·安全·golang·系统安全
妙码生花2 天前
使用git更新ai-go-admin框架
前端·人工智能·git·golang·typescript·php
王的宝库2 天前
GO常用标准库包
开发语言·后端·golang
平头哥AI2 天前
Day 13 | 一个函数回两个值:Go 的 error 是从哪冒出来的
开发语言·后端·golang
我不会起名字3222 天前
一天一道算法题(29):单调栈
java·数据结构·python·算法·leetcode·golang·单调栈