当前位置:网站首页>Use of switch statement in go language learning
Use of switch statement in go language learning
2022-06-30 11:11:00 【1024 Q】
Basic grammar
fallthrough Usage method
Multiple condition matching
Judgment interface (interface) type
Empty interface
Get the actual type
Basic grammarIt's about if-else Already mentioned when , If there are multiple criteria ,Go The language provides Switch-Case The way . If switch The following without conditions is equivalent to 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 Usage method By default ,case It will be carried out after meeting the requirements break, Back case Even if the conditions are met, it will not cycle , If you want to continue , You need to add 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") }}All the case Will be executed
Multiple condition matchingcondition 1 triggered
condition 2 triggered
Default triggered
If the same condition is met , You can also list the same conditions in this way , Equivalent to or condition
switch i { case 0, 1: f() default: g()} Judgment interface (interface) type Empty interface We will talk about interfaces later , adopt switch It can be done to type Judge , Get the real type of the interface .
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) }}In the example above , We define an empty interface
var value interface{}Use at the same time switch To judge the type
switch q:= value.(type) {Because the empty interface has no content , So the type is nil, Triggered default
value is of type: <nil> Get the actual type Let's transform the above example , At the same time, let the empty interface have the actual value , Let's take a look at the effect of implementation
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) }}Here are some knowledge points that have not been mentioned :
How to define a function
Defined a map, But the type of the value is null interface , This means that it can be any type of value , This will be explained in detail in the interface chapter , So don't get tangled up here , Keep looking down
assignment , Specially value Different types , string/int/float type
Finally, the variable is passed to through the loop valueType function , See what the program outputs
map[age:21 height:167.64 name:Alice]
value is of type: string
value is of int type
value is of float64 type
This is about Go Language learning Switch This is the end of the article on the use of statements , More about Go Language Switch Please search the previous articles of software development network or continue to browse the relevant articles below. I hope you will support software development network more in the future !
边栏推荐
- 20万奖金池!【阿里安全 × ICDM 2022】大规模电商图上的风险商品检测赛火热报名中!...
- 我们公司使用 7 年的这套通用解决方案,打通了几十个系统,稳的一批!
- Train an image classifier demo in pytorch [learning notes]
- 数学(快速幂)
- SQL必需掌握的100个重要知识点:使用表别名
- 华三交换机清空配置
- 200000 bonus pool! [Alibaba security × ICDM 2022] the risk commodity inspection competition on the large-scale e-commerce map is in hot registration
- Ant financial's written test question: what can be quantified in the requirements document? [Hangzhou multi tester] [Hangzhou multi tester \wang Sir]
- datax - 艰难debug路
- Retest the cloud native database performance: polardb is still the strongest, while tdsql-c and gaussdb have little change
猜你喜欢

Cp2112 teaching example of using USB to IIC communication
![[STL source code analysis] container (to be supplemented)](/img/69/0c6e0e8ecb3ebc8c9b8503f5a8b4e5.jpg)
[STL source code analysis] container (to be supplemented)

DQN笔记

考研这些“不靠谱”的经验有多害人?

8 lines of code to achieve quick sorting, easy to understand illustrations!

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

MySQL export SQL script file
![[机缘参悟-34]:光锥之内皆命运](/img/3e/9f5630ba382df7f7ce00705445cef8.jpg)
[机缘参悟-34]:光锥之内皆命运

【IC5000教程】-01-使用daqIDEA图形化debug调试C代码

数学(快速幂)
随机推荐
The intelligent DNA molecular nano robot model is coming
The precision problem of depth texture in unity shader - stepping pit - BRP pipeline (there is no solution, it is recommended to replace URP)
datax - 艰难debug路
IDEA 又出新神器,一套代码适应多端!
ARouter 最新问题合集
The two e-commerce bigwigs' lacy news screens represent the return of e-commerce to normal, which will be beneficial to the real economy
LiveData源码赏析三 —— 常见问题
数据库什么时候需要使用索引【杭州多测师】【杭州多测师_王sir】
Key library function based on Hal Library
SQL必需掌握的100个重要知识点:更新和删除数据
基于HAL库的LED驱动库
Every time I look at my colleagues' interface documents, I get confused and have a lot of problems...
From introduction to mastery of MySQL 50 lectures (32) -scylladb production environment cluster building
100 important knowledge points that SQL must master: summary data
【STL源码剖析】容器(待补充)
go语言defer
【leetcode 239】滑动窗口
100 important knowledge points that SQL must master: join table
100 important knowledge points that SQL must master: using stored procedures
什么是微信小程序,带你推开小程序的大门