当前位置:网站首页>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
边栏推荐
- 函数栈帧的创建和销毁
- Puppet automatic operation and maintenance troubleshooting cases
- Can‘t connect to MySQL server on ‘localhost‘
- VC下Unicode和ANSI互转,CStringW和std::string互转
- 详解指针进阶2
- Markdown file titles are all reduced by one level
- Using multipleoutputs to output multiple files in MapReduce
- Win10 enterprise 2016 long term service activation tutorial
- Visual upper system design and development (Halcon WinForm) -5 camera
- C语言刷题~Leetcode与牛客网简单题
猜你喜欢

Idea does not specify an output path for the module

String functions that you need to know

北京共有产权房出租新规实施的租赁案例

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

App移动端测试【3】ADB命令

GCC cannot find the library file after specifying the link library path

redis缓存穿透,缓存击穿,缓存雪崩解决方案

Dataframe returns the whole row according to the value

Jvm-05-object, direct memory, string constant pool

C语言刷题~Leetcode与牛客网简单题
随机推荐
自定义注解
Find mapping relationship
Using multipleoutputs to output multiple files in MapReduce
Srs4.0+obs studio+vlc3 (environment construction and basic use demonstration)
Popular understanding of gradient descent
Leasing cases of the implementation of the new regulations on the rental of jointly owned houses in Beijing
[combinatorial mathematics] binomial theorem and combinatorial identity (binomial theorem | three combinatorial identities | recursive formula 1 | recursive formula 2 | recursive formula 3 Pascal / Ya
Reentrantlock usage and source code analysis
Location of software installation information and system services in the registry
Characteristics of MySQL InnoDB storage engine -- Analysis of row lock
Custom annotation
Popular understanding of linear regression (I)
Chapter 04_ Logical architecture
Stress test WebService with JMeter
redis缓存穿透,缓存击穿,缓存雪崩解决方案
Redis cache penetration, cache breakdown, cache avalanche solution
Halcon and WinForm study section 1
Calibre LVL
视觉上位系统设计开发(halcon-winform)-1.流程节点设计
Enable multi-threaded download of chrome and edge browsers