当前位置:网站首页> 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语句内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!
边栏推荐
- LVGL 8.2 Checkboxes as radio buttons
- 8行代码实现快速排序,简单易懂图解!
- Pytorch notes: validation, model eval V.S torch. no_ grad
- datax json说明
- MySQL从入门到精通50讲(三十二)-ScyllaDB生产环境集群搭建
- [understanding of opportunity -34]: fate is within the light cone
- LVGL 8.2 menu from a drop-down list
- LVGL 8.2 re-coloring
- The number of users of the home-made self-developed system exceeded 400million, breaking the monopoly of American enterprises, and Google repented
- Iptables target tproxy
猜你喜欢

Sarsa笔记

List介绍

China will force a unified charging interface. If Apple does not bow its head, iPhone will be kicked out of the Chinese market

Retest the cloud native database performance: polardb is still the strongest, while tdsql-c and gaussdb have little change

电化学氧气传感器寿命、工作原理及应用介绍

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

8行代码实现快速排序,简单易懂图解!

20万奖金池!【阿里安全 × ICDM 2022】大规模电商图上的风险商品检测赛火热报名中!...

数学知识复习:第二型曲线积分

The intelligent DNA molecular nano robot model is coming
随机推荐
Q-Learning笔记
[leetcode 239] sliding window
【无标题】
Pytorch Notebook. Nn. Batchnorm1d
The two e-commerce bigwigs' lacy news screens represent the return of e-commerce to normal, which will be beneficial to the real economy
中移OneOS开发板学习入门
20万奖金池!【阿里安全 × ICDM 2022】大规模电商图上的风险商品检测赛火热报名中!...
小程序中读取腾讯文档的表格数据
The number of users of the home-made self-developed system exceeded 400million, breaking the monopoly of American enterprises, and Google repented
基于HAL库的按键(KEY)库函数
Deep dive kotlin synergy (16): Channel
经典面试题:负责的模块,针对这些功能点你是怎么设计测试用例的?【杭州多测师】【杭州多测师_王sir】...
数学知识复习:第二型曲线积分
单片机 MCU 固件打包脚本软件
Classic interview question: responsible modules, how do you design test cases for these function points? [Hangzhou multi surveyors] [Hangzhou multi surveyors \wang Sir]
WireGuard简单配置
ESP32-C3入门教程 IoT篇⑤——阿里云 物联网平台 EspAliYun RGB LED 实战之批量生产的解决方案
Time complexity and space complexity
pytorch 筆記 torch.nn.BatchNorm1d
时间复杂度与空间复杂度