当前位置:网站首页> Go语言学习之Switch语句的使用
Go语言学习之Switch语句的使用
2022-06-30 10:41:00 【1024问】
基本语法
fallthrough使用方法
多条件匹配
判断接口(interface)类型
空接口
获取实际类型
基本语法在讲述if-else时已经提到,如果有多个判断条件,Go语言中提供了Switch-Case的方式。如果switch后面不带条件相当于switch true
// Convert hexadecimal character to an int value 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 0fallthrough使用方法默认情况下,case满足执行后会进行break,后面case即使满足条件也不再循环,如果想继续执行,则需要添加fallthrough,
package mainimport "fmt"func main() { i := 3 switch i { case i > 0: fmt.Println("condition 1 triggered") fallthrough case i > 2: fmt.Println("condition 2 triggered") fallthrough default: fmt.Println("Default triggered") }}此时所有的case都会被执行
多条件匹配condition 1 triggered
condition 2 triggered
Default triggered
如果同一个条件满足,也可以这样罗列到同一条件,相当于或条件
switch i { case 0, 1: f() default: g()}判断接口(interface)类型空接口后面我们会讲到接口,通过switch可以对type进行判断,获取接口的真实类型。
package mainimport "fmt"func main() { var value interface{} switch q:= value.(type) { case bool: fmt.Println("value is of boolean type") case float64: fmt.Println("value is of float64 type") case int: fmt.Println("value is of int type") default: fmt.Printf("value is of type: %T", q) }}在上面的例子中,我们定义了一个空接口
var value interface{}同时使用switch来判断类型
switch q:= value.(type) {由于空接口没有内容,所以类型为nil,触发了default
value is of type: <nil>获取实际类型我们对上面的例子进行改造,同时让空接口拥有实际的值,再来看看执行的效果
package mainimport "fmt"func valueType(i interface{}) { switch q:= i.(type) { case bool: fmt.Println("value is of boolean type") case float64: fmt.Println("value is of float64 type") case int: fmt.Println("value is of int type") default: fmt.Printf("value is of type: %T\n", q) }}func main() { person := make(map[string]interface{}, 0) person["name"] = "Alice" person["age"] = 21 person["height"] = 167.64 fmt.Printf("%+v\n", person) for _, value := range person { valueType(value) }}这里有几个还没有讲到的知识点:
函数的定义方法
定义了一个map,但是值的类型为空接口,意思就是可以是任何类型的值,这在接口章节还会详细讲解,所以大家看到这里不要纠结,继续往下看
赋值时,特意给value不同的类型, string/int/float类型
最后通过循环将变量传给valueType函数,看看程序输出什么结果
map[age:21 height:167.64 name:Alice]
value is of type: string
value is of int type
value is of float64 type
到此这篇关于Go语言学习之Switch语句的使用的文章就介绍到这了,更多相关Go语言 Switch语句内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!
边栏推荐
- How can the sports app keep the end-to-side background alive to make the sports record more complete?
- 经典面试题:负责的模块,针对这些功能点你是怎么设计测试用例的?【杭州多测师】【杭州多测师_王sir】...
- Sarsa笔记
- Rejuvenated Dell and apple hit each other, and the two old PC enterprises declined rapidly
- SQL必需掌握的100个重要知识点:组合查询
- 中移OneOS开发板学习入门
- 电商两位大佬花边新闻刷屏,代表电商回归正常,将有利于实体经济
- 语音信号处理-基础(五):傅立叶变换
- 小程序中读取腾讯文档的表格数据
- [STL source code analysis] container (to be supplemented)
猜你喜欢

记一次ViewPager + RecyclerView的内存泄漏

Mysql database foundation: views and variables

go语言defer

Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)
![[understanding of opportunity -34]: fate is within the light cone](/img/3e/9f5630ba382df7f7ce00705445cef8.jpg)
[understanding of opportunity -34]: fate is within the light cone

Deep dive kotlin synergy (18): hot and cold data flow

煥發青春的戴爾和蘋果夾擊,兩大老牌PC企業極速衰敗

电商两位大佬花边新闻刷屏,代表电商回归正常,将有利于实体经济

深潜Kotlin协程(十八):冷热数据流

LVGL 8.2 Image
随机推荐
高通发布物联网案例集 “魔镜”、数字农业已经成为现实
【无标题】
LVGL 8.2 Image styling and offset
智能DNA分子纳米机器人模型来了
煥發青春的戴爾和蘋果夾擊,兩大老牌PC企業極速衰敗
Ant financial's written test question: what can be quantified in the requirements document? [Hangzhou multi tester] [Hangzhou multi tester \wang Sir]
LVGL 8.2图片缩放及旋转
Viewing technological changes through Huawei Corps (V): smart Park
【STL源码剖析】容器(待补充)
焕发青春的戴尔和苹果夹击,两大老牌PC企业极速衰败
ArrayList and sequence table
[STL source code analysis] iterator
LED driver library based on Hal Library
Algorithme leetcode 86. Liste des liens séparés
List介绍
Mysql database foundation: views and variables
LeetCode Algorithm 86. 分隔鏈錶
LVGL 8.2 Image
Q-Learning笔记
Unity Shader - 踩坑 - BRP 管线中的 depth texture 的精度问题(暂无解决方案,推荐换 URP)