当前位置:网站首页>Go语言自学系列 | golang switch语句
Go语言自学系列 | golang switch语句
2022-07-03 15:35:00 【51CTO】
视频来源:B站《golang入门到项目实战 [2021最新Go语言教程,没有废话,纯干货!持续更新中...]》
一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持!
附上汇总贴: Go语言自学系列 | 汇总_COCOgsta的博客-CSDN博客
go语言中的switch语句,可以非常容易的判断多个值的情况。
go语言中switch语句的语法
go语言中switch语句实例
判断成绩
运行结果
多条件匹配
go语言switch语句,可以同时匹配多个条件,中间用逗号分隔,有其中一个匹配成功即可。
运行结果
case可以是条件表达式
运行结果
fallthrough可以执行满足条件的下一个case
package main
import (
"fmt"
)
func f3() {
a := 100
switch a {
case 100:
fmt.Println("100")
fallthrough
case 200:
fmt.Println("200")
case 300:
fmt.Println("300")
default:
fmt.Println("other")
}
}
func main() {
f3()
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
运行结果
go语言中switch语句的注意事项
- 支持多条件匹配
- 不同的 case 之间不使用 break 分隔,默认只会执行一个 case。
- 如果想要执行多个 case,需要使用 fallthrough 关键字,也可用 break 终止。
- 分支还可以使用表达式,例如:a>10
边栏推荐
- VS2017通过IP调试驱动(双机调试)
- Detailed pointer advanced 1
- Kubernetes - yaml file interpretation
- Jvm-09 byte code introduction
- Second kill system 3 - list of items and item details
- [probably the most complete in Chinese] pushgateway entry notes
- Construction of operation and maintenance system
- Popular understanding of random forest
- C language brush questions ~leetcode and simple questions of niuke.com
- Creation and destruction of function stack frames
猜你喜欢

软件逆向破解入门系列(1)—xdbg32/64的常见配置及功能窗口

Visual upper system design and development (Halcon WinForm) -5 camera

Detailed pointer advanced 2

Kubernetes带你从头到尾捋一遍

Jvm-08-garbage collector

Popular understanding of ovo and ovr

Jvm-04-runtime data area heap, method area

找映射关系

Unity功能——Unity离线文档下载及使用

How are integer and floating-point types stored in memory
随机推荐
Halcon与Winform学习第二节
Popular understanding of gradient descent
SQL server installation location cannot be changed
Qt常用语句备忘
互斥对象与临界区的区别
从 flask 服务端代码自动生成客户端代码 -- flask-native-stubs 库介绍
Using Tengine to solve the session problem of load balancing
Jvm-08-garbage collector
Puppet automatic operation and maintenance troubleshooting cases
通过进程PID获取可执行文件路径(QueryFullProcessImageName)
Custom annotation
String functions that you need to know
Jvm-06-execution engine
高并发下之redis锁优化实战
【云原生训练营】模块八 Kubernetes 生命周期管理和服务发现
The difference between RAR and zip files
使用AUR下载并安装常用程序
Halcon与Winform学习第一节
Unity功能——Unity离线文档下载及使用
Visual upper system design and development (Halcon WinForm) -1 Process node design