網(wǎng)站首頁 編程語言 正文
基本語法
在講述if-else時已經(jīng)提到,如果有多個判斷條件,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 0
fallthrough使用方法
默認情況下,case滿足執(zhí)行后會進行break,后面case即使?jié)M足條件也不再循環(huán),如果想繼續(xù)執(zhí)行,則需要添加fallthrough,
package main import "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都會被執(zhí)行
condition 1 triggered
condition 2 triggered
Default triggered
多條件匹配
如果同一個條件滿足,也可以這樣羅列到同一條件,相當于或條件
switch i { case 0, 1: f() default: g() }
判斷接口(interface)類型
空接口
后面我們會講到接口,通過switch可以對type進行判斷,獲取接口的真實類型。
package main import "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,觸發(fā)了default
value is of type: <nil>
獲取實際類型
我們對上面的例子進行改造,同時讓空接口擁有實際的值,再來看看執(zhí)行的效果
package main import "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) } }
這里有幾個還沒有講到的知識點:
- 函數(shù)的定義方法
- 定義了一個map,但是值的類型為空接口,意思就是可以是任何類型的值,這在接口章節(jié)還會詳細講解,所以大家看到這里不要糾結,繼續(xù)往下看
- 賦值時,特意給value不同的類型, string/int/float類型
最后通過循環(huán)將變量傳給valueType函數(shù),看看程序輸出什么結果
map[age:21 height:167.64 name:Alice]
value is of type: string
value is of int type
value is of float64 type
原文鏈接:https://blog.csdn.net/xiaoquqi/article/details/125532907
相關推薦
- 2022-08-14 Selenium定位瀏覽器彈窗方法實例總結_python
- 2022-06-09 忘記Grafana不要緊2種Grafana重置admin密碼方法詳細步驟_服務器其它
- 2022-12-14 PostgreSql?JDBC事務操作方法詳解_PostgreSQL
- 2022-06-17 一文輕松了解ASP.NET與ASP.NET?Core多環(huán)境配置對比_實用技巧
- 2022-04-20 超全整理visual?studio快捷鍵使用技巧_相關技巧
- 2022-07-13 RedisDesktopManager遠程連接redis的實現(xiàn)_Redis
- 2023-03-15 pandas將Series轉成DataFrame的實現(xiàn)_python
- 2022-06-29 Oracle去除重復數(shù)據(jù)_oracle
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支