当前位置:网站首页>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 !
边栏推荐
- 林克庆到番禺区调研“发展要安全”工作 以“时时放心不下”责任感抓好安全发展各项工作
- 考研这些“不靠谱”的经验有多害人?
- ESP32-C3入门教程 IoT篇⑤——阿里云 物联网平台 EspAliYun RGB LED 实战之批量生产的解决方案
- LVGL 8.2 Image styling and offset
- LeetCode Algorithm 86. 分隔链表
- LVGL 8.2 Simple Image button
- 我们公司使用 7 年的这套通用解决方案,打通了几十个系统,稳的一批!
- SQL必需掌握的100个重要知识点:汇总数据
- AMS源码解析
- When does the database need to use the index [Hangzhou multi surveyors] [Hangzhou multi surveyors _ Wang Sir]
猜你喜欢

电商两位大佬花边新闻刷屏,代表电商回归正常,将有利于实体经济
![When does the database need to use the index [Hangzhou multi surveyors] [Hangzhou multi surveyors _ Wang Sir]](/img/2a/f07a7006e0259d78d046b30c761764.jpg)
When does the database need to use the index [Hangzhou multi surveyors] [Hangzhou multi surveyors _ Wang Sir]

Cp2112 teaching example of using USB to IIC communication

List介绍

【STL源码剖析】迭代器
![[机缘参悟-34]:光锥之内皆命运](/img/3e/9f5630ba382df7f7ce00705445cef8.jpg)
[机缘参悟-34]:光锥之内皆命运

ESP32-C3入门教程 问题篇⑨——Core 0 panic‘ed (Load access fault). Exception was unhandled. vfprintf.c:1528

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

Pytorch Notebook. Nn. Batchnorm1d

Pytorch notes torch nn. BatchNorm1d
随机推荐
【STL源码剖析】容器(待补充)
WireGuard简单配置
Retest the cloud native database performance: polardb is still the strongest, while tdsql-c and gaussdb have little change
List介绍
深潜Kotlin协程(十八):冷热数据流
100 important knowledge points that SQL must master: creating and manipulating tables
软件测试工程师面试基础题(应届生和测试小菜必备)最基础的面试题
基于HAL库的LED驱动库
The precision problem of depth texture in unity shader - stepping pit - BRP pipeline (there is no solution, it is recommended to replace URP)
[leetcode 239] sliding window
【西安交通大学】考研初试复试资料分享
Pytorch notes: validation, model eval V.S torch. no_ grad
LVGL 8.2 Drop down in four directions
LVGL 8.2 Simple Image button
DataX JSON description
100 important knowledge points that SQL must master: updating and deleting data
SQL必需掌握的100个重要知识点:汇总数据
8 lines of code to achieve quick sorting, easy to understand illustrations!
单片机 MCU 固件打包脚本软件
100 important knowledge points that SQL must master: summary data