網站首頁 編程語言 正文
1、if...else判斷語法
語法的使用和其他語言沒啥區(qū)別。
樣例代碼如下:
// 判斷語句
func panduan(a int) {
if a > 50 {
fmt.Println("a > 50")
} else if a < 30 {
fmt.Println("a < 30")
} else {
fmt.Println("a <= 50 and a >= 30")
}
}
func main() {
panduan(120)
}
執(zhí)行結果
a > 50
2、if嵌套語法
樣例代碼如下
//嵌套判斷
func qiantao(b, c uint) {
if b >= 100 {
b -= 20
if c > b {
fmt.Println("c OK")
} else {
fmt.Println("b OK")
}
}
}
執(zhí)行結果
c OK
3、switch語句
兩種寫法,不需要加break。
樣例代碼如下
//switch使用
func test_switch() {
var a uint = 90
var result string
switch a {
case 90:
result = "A"
case 80, 70, 60:
result = "B"
default:
result = "C"
}
fmt.Printf("result: %v\n", result)
switch {
case a > 90:
result = "A"
case a <= 90 && a >= 80:
result = "B"
default:
result = "C"
}
fmt.Printf("result: %v\n", result)
}
執(zhí)行結果
result: A ? ? ? ? ? ? ?
result: B ?
注意
1、可是在switch后面加變量,后面的case主要做匹配判斷。也可以直接使用switch{},case直接對關系運算結果做匹配。
2、 case中可以選擇匹配多項。
4、類型switch語句
switch語句可以使用type-switch進行類型判斷,感覺很實用的語法。
樣例代碼如下
//測試類型switch
func test_type_switch() {
var x interface{}
x = 1.0
switch i := x.(type) {
case nil:
fmt.Printf("x type = %T\n", i)
case bool, string:
fmt.Printf("x type = bool or string\n")
case int:
fmt.Printf("x type = int\n")
case float64:
fmt.Printf("x type = float64\n")
default:
fmt.Printf("未知\n")
}
}
執(zhí)行結果
x type = float64 ? ??
注意
1、interface{}可以表示任何類型。
2、語法格式變量.(type)
5、fallthrough關鍵字使用
使用fallthrough關鍵字會強制執(zhí)行后面的case語句內容,不管時候觸發(fā)該case條件。
樣例代碼如下
// 測試fallthrough
func test_fallthrough() {
a := 1
switch {
case a < 0:
fmt.Println("1")
fallthrough
case a > 0:
fmt.Println("2")
fallthrough
case a < 0:
fmt.Println("3")
fallthrough
case a < 0:
fmt.Println("4")
case a > 0:
fmt.Println("5")
fallthrough
case a < 0:
fmt.Println("6")
fallthrough
default:
fmt.Println("7")
}
}
執(zhí)行結果
2 ? ? ? ? ? ? ? ? ? ? ?
3 ? ? ? ? ? ? ? ? ? ? ?
4 ?
注意
1、如果一旦在往下執(zhí)行case內容中不存在fallthrough,則會停止繼續(xù)往下執(zhí)行case內容。?
小結
我看到還有個select語句,需要和chan關鍵字進行配合使用,沒不了解,后面先研究一下chan關鍵字。
原文鏈接:https://blog.csdn.net/zhiweihongyan1/article/details/124169432
相關推薦
- 2022-01-19 正則——時間 時分秒 12小時制 24小時制 moment可以轉化的時間 HH:mm:ss hh:m
- 2022-06-18 C#多線程之線程中止Abort()方法_C#教程
- 2022-04-05 用css改變input光標的3種方法
- 2022-01-30 uniapp蘋果底部欄自適應配置
- 2022-07-01 Python中的字典合并與列表合并技巧_python
- 2022-07-28 Redis特殊數(shù)據類型HyperLogLog基數(shù)統(tǒng)計算法講解_Redis
- 2023-01-17 Pytorch-Geometric中的Message?Passing使用及說明_python
- 2022-03-26 C++缺省參數(shù)的具體使用_C 語言
- 最近更新
-
- 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ù)據結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支