專做外貿的網(wǎng)站有哪些免費十八種禁用網(wǎng)站
Go語言入門之流程控制簡述
1.if語句
if
語句和其他語言一樣,只不過go語言
的if
不需要用括號包裹
if
語句的分支代碼塊的左大括號與if
關鍵字在同一行上,這是go
代碼風格的統(tǒng)一要求
簡單實例:
func main() {// 猜數(shù)字a := 2if a > 0 {if a > 3 {fmt.Println("數(shù)字過大")} else if a == 1 {fmt.Println("再大一點")} else if a == 2 {fmt.Println("再大一點")} else if a == 3 {fmt.Println("猜對了")}} else {fmt.Println("數(shù)字過小")}
}
// 輸入的是2,輸出的結果是:再大一點
if語句的特殊寫法
if 條件判斷還有一種特殊的寫法,可以在 if 表達式之前添加一個執(zhí)行語句,再根據(jù)變量值進行判斷
// 特殊寫法,這種寫法將a的作用范圍限制在if表達式中
if a := 10; a >5 {fmt.Println(a)return
}
2.for語句
go語言只支持for循環(huán),沒有while
(1)for語句的7種用法
第一種:普通寫法
sum := 0
for i := 0; i < 10; i++ {sum += i
}
第二種:無限循環(huán)
for{// 無限循環(huán)代碼塊
}
第三種:for后只寫條件
n := 0
// for后面只加條件
for n < 3 {n++
}
fmt.Println(n)
第四種:for range 遍歷切片
var slice []int = []int{1, 2, 3, 4, 5}
// for range循環(huán)切片或數(shù)組
for index, value := range slice {fmt.Printf("index:%d,value:%d \n", index, value)
}
第五種:for range 遍歷字符串和整數(shù)
// 可以循環(huán)字符串 字符串底層為一個byte切片
// 字符串大小為16字節(jié),其有一個指針和int型長度組成
for _, j := range "zhangsan" {fmt.Printf("%c \n", j)
}// for range還可以遍歷整數(shù)
for i := range 5 {fmt.Printf("%d", i+1)
}
// 打印結果:12345
第六種:遍歷map
// 循環(huán)map
mmap := map[int]string{11: "hh",22: "ff",33: "ww",
}
for key, value := range mmap {fmt.Println(key, value)
}
第七種:for range 遍歷channel
// 循環(huán)channel
chnl := make(chan int)
go func() {chnl <- 100chnl <- 1000chnl <- 10000chnl <- 100000close(chnl)
}()
for i := range chnl {fmt.Println(i)
}
(2)for語句的4種結束方法
第一種:return
func main() {step := 2for step > 0 {step--fmt.Println(step)//執(zhí)行一次就結束了return}//不會執(zhí)行fmt.Println("結束之后的語句....")
}
第二種:break
注意:go語言中break只能跳出一層for循環(huán)
func main() {step := 2for step > 0 {step--fmt.Println(step)//跳出循環(huán),還會繼續(xù)執(zhí)行循環(huán)外的語句break}//會執(zhí)行fmt.Println("結束之后的語句....")
}
第三種:panic
func main() {step := 2for step > 0 {step--fmt.Println(step)//報錯了,直接結束panic("出錯了")}//不會執(zhí)行fmt.Println("結束之后的語句....")
}
第四種:goto
func main() {for x := 0; x < 10; x++ {if x == 2 {// 跳轉到標簽goto breakHere}}// 手動返回, 避免執(zhí)行進入標簽return// 標簽
breakHere:fmt.Println("done")
}
3.switch語句
Go語言的switch同其他語言的差不多,使用 switch 語句可方便地對大量的值進行條件判斷。
switch 分支表達式可以是任意類型,不限于常量。可省略 break,默認自動終止。
switch后如果沒有表達式會對第一個true進行匹配
例子如下:
// 成績判定grade := "B"// 輸入90var score int = 90switch score {case 90:grade = "A"case 80:grade = "B"case 50, 60, 70:grade = "C"// 無論 default出現(xiàn)在什么位置,它只會在所有case都沒有匹配上的情況下才會被執(zhí)行default:grade = "D"}fmt.Println(grade)//結果是:A
注意:
Go里面switch默認相當于每個case最后帶有break
匹配成功后不會自動向下執(zhí)行其他case,而是跳出整個switch
如果需要執(zhí)行完一個case之后,進入下一個case不跳出
swtich
這就需要用到
fallthrough
,無論如何都會進入下個casea := "hello"switch {case a == "hello":fmt.Print("hello ")fallthroughcase a == "world":fmt.Println("world")} //打印結果:hello world
4.goto語句
goto 語句通過
標簽
進行代碼間的無條件跳轉goto 語句可以實現(xiàn)快速跳出循環(huán)、避免重復退出
因此 goto 語句能簡化一些代碼的實現(xiàn)過程
func main() {if 20 > 10 {goto GotoTag1}return
GotoTag1:fmt.Println("tag1")if 10 > 5 {goto GotoTag2}return
GotoTag2:fmt.Println("tag2")
}
5.break語句
break 語句可以結束 for、switch 和 select 的代碼塊
break 語句還可以在語句后面添加
標簽
,表示退出某個標簽對應的代碼塊
標簽
要求必須定義在對應的for
、switch
和select
的代碼塊上。
func main() {
OuterLoop:for i := 0; i < 2; i++ {for j := 0; j < 5; j++ {switch j {case 2:fmt.Println(i, j)break OuterLoopcase 3:fmt.Println(i, j)break OuterLoop}}}
}
6.continue語句
continue 語句可以結束當前循環(huán),開始下一次的循環(huán)迭代過程
僅限在 for 循環(huán)內使用
在 continue 語句后添加
標簽
時,表示開始標簽對應的循環(huán)
func main() {
OuterLoop:for i := 0; i < 2; i++ {for j := 0; j < 5; j++ {switch j {case 2:fmt.Println(i, j)continue OuterLoop}}}
}