当前位置:网站首页>【愚公系列】2022年7月 Go教学课程 018-分支结构之switch
【愚公系列】2022年7月 Go教学课程 018-分支结构之switch
2022-07-27 02:30:00 【愚公搬代码】
前言
Switch在一些计算机语言中是保留字,其作用大多情况下是进行判断选择。以Go语言来说,switch(开关语句)常和case default一起使用。
一、switch语句
1.switch基本使用
Go 语言中 switch 中的每一个 case 都是独立代码块,无需通过 break 语句跳出代码块,以避免执行到下一个 case 代码块。
相关案例:
package main
import "fmt"
func main() {
num := 1
switch num {
case 1:
fmt.Println("小愚公")
case 2:
fmt.Println("大愚公")
default:
fmt.Println("不是愚公")
}
}

上面代码中,case 对整型 num 进行判断,若条件符合,则打印相应的字符串。另外,还使用了 default 分支,default 分支表示若条件均不符合,则执行改代码块。
2.一分支多值
package main
import "fmt"
func main() {
num := 1
switch num {
case 1, 2:
fmt.Println("都是愚公")
}
}

3.分支表达式
package main
import "fmt"
func main() {
num := 1
switch {
case num > 0 && num < 2:
fmt.Println("猜对了是愚公")
}
}

4.fallthrough关键字
Go 语言中 swtich 语句中的 case 均为独立代码块,执行完成后,不会继续执行下一个 case,fallthrough就可以实现继续执行。
package main
import "fmt"
func main() {
num := 1
switch {
case num > 0:
fmt.Println("愚公一号")
fallthrough
case num < 2:
fmt.Println("愚公二号")
}
}

二、相关案例
1.成绩打分
package main
import "fmt"
func main() {
//考试成绩大于等于90输出A,大于等于80输出B,大于等于70输出C,大于等于60输出D,不及格输出E
var score float64
fmt.Println("请输入考试成绩:")
fmt.Scan(&score)
switch {
case score >= 90:
fmt.Println("A")
case score >= 80:
fmt.Println("B")
case score >= 70:
fmt.Println("C")
case score >= 60:
fmt.Println("D")
default:
fmt.Println("E")
}
}

总结
- 某个case后面跟着的代码执行完毕后,不会再执行后面的case,而是跳出整个switch结构,相当于每个case后面都跟着break(终止),但是如果我们想执行完成某个case后,强制执行后面的case,可以使用fallthrough。
- switch结构和if结构是可以结合来使用的,可以根据具体的问题具体分析,灵活来应用。
边栏推荐
- Plato farm brings a new experience to community users through the LAAS protocol elephant swap
- Plato Farm有望通过Elephant Swap,进一步向外拓展生态
- 明汯投资裘慧明:长期优异超额的背后考验的是团队的投研能力和策略的完整性
- LeetCode 第二十八天
- flink cdc 到MySQL8没问题,到MySQL5读有问题,怎么办?
- Have you encountered the situation that CDC reads incomplete MySQL fields? How to deal with it?
- 关于使用hyperbeach出现/bin/sh: 1: packr2: not found的解决方案
- Characteristics and determination scheme of Worthington pectinase
- C语言力扣第43题之字符串相乘。优化竖式
- 数字孪生实际应用:智慧城市项目建设解决方案
猜你喜欢

函数指针与回调函数

Interview question: the difference between three instantiated objects in string class

次轮Okaleido Tiger即将登录Binance NFT,引发社区热议

Redis(九) - Redis之分布式锁

Introduction to database - a brief introduction to MySQL

Application, addition and deletion of B-tree

DTS is equipped with a new self-developed kernel, which breaks through the key technology of the three center architecture of the two places Tencent cloud database

Connman introduction

How to interact with the server when the client sends an SQL message

If the detailed explanation is generated according to the frame code
随机推荐
【无标题】JDBC连接数据库读超时
【安卓小叙】Kotlin多线程编程(一)
明汯投资裘慧明:长期优异超额的背后考验的是团队的投研能力和策略的完整性
Network security / penetration testing tool awvs14.9 download / tutorial / installation tutorial
Introduction to database - Introduction to database
Mysql database related operations
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
[untitled]
Meta Quest内容生态总监谈App Lab设计初衷
jmeter接口测试(登录、注册)
The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode
分享当下人生——一个高中毕业生在中央电视台的六星期实习经历
Typescript TS basic knowledge interface, generics
Characteristics and determination scheme of Worthington pectinase
Practical application of digital twins: smart city project construction solution
flask_ Reqparse parser inheritance in restful
Number of 0 at the end of factorial
03.获取网页源代码
DTS is equipped with a new self-developed kernel, which breaks through the key technology of the three center architecture of the two places Tencent cloud database
百融榕树数据分析拆解方法