当前位置:网站首页>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 !
边栏推荐
- Key library function based on Hal Library
- 压缩状态DP位运算
- 基于HAL库的按键(KEY)库函数
- pytorch 筆記 torch.nn.BatchNorm1d
- LeetCode Algorithm 86. 分隔链表
- The first China Digital Collection conference will be held soon
- LVGL 8.2 Checkboxes as radio buttons
- Retest the cloud native database performance: polardb is still the strongest, while tdsql-c and gaussdb have little change
- LVGL 8.2 re-coloring
- Collectors. Tomap application
猜你喜欢

Every time I look at my colleagues' interface documents, I get confused and have a lot of problems...

第一届中国数字藏品大会即将召开

Pytorch Notebook. Nn. Batchnorm1d

Time complexity and space complexity

The life, working principle and application of electrochemical oxygen sensor

The precision problem of depth texture in unity shader - stepping pit - BRP pipeline (there is no solution, it is recommended to replace URP)

在IPhone12的推理延迟仅为1.6 ms!Snap等详析Transformer结构延迟,并用NAS搜出移动设备的高效网络结构...

中国将强制统一充电接口,苹果如不低头,iPhone将被踢出中国市场

List introduction

中移OneOS开发板学习入门
随机推荐
MySQL导出sql脚本文件
Qt之实现QQ天气预报窗体翻转效果
煥發青春的戴爾和蘋果夾擊,兩大老牌PC企業極速衰敗
The number of users of the home-made self-developed system exceeded 400million, breaking the monopoly of American enterprises, and Google repented
Kotlin 协程调度切换线程是时候解开谜团了
Go语言学习之Switch语句的使用
记一次ViewPager + RecyclerView的内存泄漏
100 important knowledge points that SQL must master: using stored procedures
LVGL 8.2 menu from a drop-down list
数据库什么时候需要使用索引【杭州多测师】【杭州多测师_王sir】
iptables目标TPROXY
IDEA 又出新神器,一套代码适应多端!
The jetpack compose dropdownmenu is displayed following the finger click position
压缩状态DP位运算
焕发青春的戴尔和苹果夹击,两大老牌PC企业极速衰败
【IC5000教程】-01-使用daqIDEA图形化debug调试C代码
go语言defer
The life, working principle and application of electrochemical oxygen sensor
深潜Kotlin协程(十七):演员
LVGL 8.2 Image