当前位置:网站首页>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 !
边栏推荐
- LVGL 8.2 menu from a drop-down list
- 【IC5000教程】-01-使用daqIDEA图形化debug调试C代码
- Classic interview question: responsible modules, how do you design test cases for these function points? [Hangzhou multi surveyors] [Hangzhou multi surveyors \wang Sir]
- 中移OneOS开发板学习入门
- 博弈论入门
- ESP32-C3入门教程 问题篇⑨——Core 0 panic‘ed (Load access fault). Exception was unhandled. vfprintf.c:1528
- 100 important knowledge points that SQL must master: creating and manipulating tables
- How can the sports app keep the end-to-side background alive to make the sports record more complete?
- ArrayList与顺序表
- Time complexity and space complexity
猜你喜欢

The two e-commerce bigwigs' lacy news screens represent the return of e-commerce to normal, which will be beneficial to the real economy

WireGuard简单配置

数据库什么时候需要使用索引【杭州多测师】【杭州多测师_王sir】

Pytorch notes torch nn. BatchNorm1d
![[STL source code analysis] iterator](/img/e8/7c69cf6e96ecfa053494397a21eff0.jpg)
[STL source code analysis] iterator

深潜Kotlin协程(十六):Channel

go语言defer

Jetpack Compose DropdownMenu跟随手指点击位置显示

Go language defer

Deep dive kotlin synergy (16): Channel
随机推荐
LeetCode Algorithm 86. 分隔链表
What is erdma as illustrated by Coptic cartoon?
中国将强制统一充电接口,苹果如不低头,iPhone将被踢出中国市场
sublist3r报错解决
LVGL 8.2 Checkboxes as radio buttons
[understanding of opportunity -34]: fate is within the light cone
SQL必需掌握的100个重要知识点:使用子查询
Double-DQN笔记
OceanBase 安装 yum 源配置错误及解决办法
Classic interview question: responsible modules, how do you design test cases for these function points? [Hangzhou multi surveyors] [Hangzhou multi surveyors \wang Sir]
Cp2112 teaching example of using USB to IIC communication
Lvgl 8.2 picture scaling and rotation
Collectors. Tomap application
10天学会flutter DAY10 flutter 玩转 动画与打包
考研这些“不靠谱”的经验有多害人?
Time complexity and space complexity
Deep dive kotlin synergy (18): hot and cold data flow
List介绍
Ant financial's written test question: what can be quantified in the requirements document? [Hangzhou multi tester] [Hangzhou multi tester \wang Sir]
List introduction