流程控制語句
if
if 5 > 9 {
fmt.Println("5>9")
}
- 如果邏輯表達式成立,就會執(zhí)行{}里的內(nèi)容。
- 邏輯表達式不需要加()。
- "{"必須緊跟在邏輯表達式后面,不能另起一行。
if c, d, e := 5, 9, 2; c < d && (c > e || c > 3) { //初始化多個局部變量。復(fù)雜的邏輯表達式
fmt.Println("fit")
}
- 邏輯表達中可以含有變量或常量。
- if句子中允許包含1個(僅1個)分號,在分號前初始化一些局部變量(即只在if塊內(nèi)可見)。
if-else的用法
color := "black"
if color == "red" { //if只能有一個
fmt.Println("stop")
} else if color == "green" {
fmt.Println("go")
} else if color == "yellow" { //else if可以有0個、一個或者連續(xù)多個
fmt.Println("stop")
} else { //else有0個或1個
fmt.Printf("invalid traffic signal: %s\n", strings.ToUpper(color))
}
if表達式嵌套
if xxx {
if xxx {
}else if xxx{
}else{
}
}else{
if xxx {
}else{
}
}
??注意太深的嵌套不利于代碼的維護,比如
if (true) {
if (true) {
if (true) {
if (true) {
if (true) {
}
}
}
}
}
switch
color := "black"
switch color {
case "green" : //相當(dāng)于 if color== "green"
fmt.Println("go")
case "red" : //相當(dāng)于else if color== "red"
fmt.Println("stop")
default: //相當(dāng)于else
fmt.Printf("invalid traffic signal: %s\n", strings.ToUpper(color))
}
- switch-case-default可能模擬if-else if-else,但只能實現(xiàn)相等判斷。
- switch和case后面可以跟常量、變量或函數(shù)表達式,只要它們表示的數(shù)據(jù)類型相同就行。
- case后面可以跟多個值,只要有一個值滿足就行。
func add(a int) int {
return a + 10
}
func switch_expression() {
var a int = 5
switch add(a) { //switch后跟一個函數(shù)表達式
case 15: //case后跟一個常量
fmt.Println("right")
default:
fmt.Println("wrong")
}
const B = 15
switch B { //switch后跟一個常量
case add(a): //case后跟一個函數(shù)表達式
fmt.Println("right")
default:
fmt.Println("wrong")
}
}
??switch后帶表達式時,switch-case只能模擬相等的情況;如果switch后不帶表達式,case后就可以跟任意的條件表達式。
func switch_condition() {
color := "yellow"
switch color {
case "green":
fmt.Println("go")
case "red", "yellow": //用逗號分隔多個condition,它們之間是“或”的關(guān)系,只需要有一個condition滿足就行
fmt.Println("stop")
}
//switch后帶表達式時,switch-case只能模擬相等的情況;如果switch后不帶表達式,case后就可以跟任意的條件表達式
switch {
case add(5) > 10:
fmt.Println("right")
default:
fmt.Println("wrong")
}
}
switch Type
func switch_type() {
var num interface{} = 6.5
switch num.(type) { //獲取interface的具體類型。.(type)只能用在switch后面
case int:
fmt.Println("int")
case float32:
fmt.Println("float32")
case float64:
fmt.Println("float64")
case byte:
fmt.Println("byte")
default:
fmt.Println("neither")
}
switch value := num.(type) { //相當(dāng)于在每個case內(nèi)部申明了一個變量value
case int: //value已被轉(zhuǎn)換為int類型
fmt.Printf("number is int %d\n", value)
case float64: //value已被轉(zhuǎn)換為float64類型
fmt.Printf("number is float64 %f\n", value)
case byte, string: //如果case后有多個類型,則value還是interface{}類型
fmt.Printf("number is inerface %v\n", value)
default:
fmt.Println("neither")
}
//等價形式
switch num.(type) {
case int:
value := num.(int)
fmt.Printf("number is int %d\n", value)
case float64:
value := num.(float64)
fmt.Printf("number is float64 %f\n", value)
case byte:
value := num.(byte)
fmt.Printf("number is byte %d\n", value)
default:
fmt.Println("neither")
}
}
fallthrough 強制執(zhí)行下一個case(或default)
func fall_throth(age int) {
fmt.Printf("您的年齡是%d, 您可以:\n", age)
switch {
case age > 50:
fmt.Println("出任國家首腦")
fallthrough
case age > 25:
fmt.Println("生育子女")
fallthrough
case age > 22:
fmt.Println("結(jié)婚")
fallthrough
case age > 18:
fmt.Println("開車")
fallthrough
case age > 16:
fmt.Println("參加工作")
case age > 15:
fmt.Println("上高中")
fallthrough
case age > 3:
fmt.Println("上幼兒園")
}
}
for
arr := []int{1, 2, 3, 4, 5}
for i := 0; i < len(arr); i++ { //正序遍歷切片
fmt.Printf("%d: %d\n", i, arr[i])
}
for 初始化局部變量;條件表達式;后續(xù)操作
for sum, i := 0, 0; i < len(arr) && sum < 100; sum, i = sum*1, i+1
- 局部變量指僅在for塊內(nèi)可見。
- 初始化變量可以放在for上面。
- 后續(xù)操作可以放在for塊內(nèi)部。
- 只有條件判斷時,前后的分號可以不要。
- for{}是一個無限循環(huán)。
for range
- 遍歷數(shù)組或切片
- for i, ele := range arr
- 遍歷string
- for i, ele := range "我會唱ABC" //ele是rune類型
- 遍歷map,go不保證遍歷的順序
- for key, value := range m
- 遍歷channel,遍歷前一定要先close
- for ele := range ch
- for range拿到的是數(shù)據(jù)的拷貝
for嵌套
??矩陣乘法需要用到三層for循環(huán)嵌套。
mat_mul.png
func nest_for() {
const SIZE = 4
A := [SIZE][SIZE]float64{}
//初始化二維數(shù)組
//兩層for循環(huán)嵌套
for i := 0; i < SIZE; i++ {
for j := 0; j < SIZE; j++ {
A[i][j] = rand.Float64() //[0,1)上的隨機數(shù)
}
}
B := [SIZE][SIZE]float64{}
for i := 0; i < SIZE; i++ {
for j := 0; j < SIZE; j++ {
B[i][j] = rand.Float64() //[0,1)上的隨機數(shù)
}
}
rect := [SIZE][SIZE]float64{}
//三層for循環(huán)嵌套
for i := 0; i < SIZE; i++ {
for j := 0; j < SIZE; j++ {
prod := 0.0
for k := 0; k < SIZE; k++ {
prod += A[i][k] * B[k][j]
}
rect[i][j] = prod
}
}
i, j := 2, 1
fmt.Println(A[i]) //二維數(shù)組第i行
//打印二維數(shù)組的第j列
//注意:B[:][j]這不是二維數(shù)組第j列,這是二維數(shù)組第j行!
for _, row := range B {
fmt.Printf("%g ", row[j])
}
fmt.Println()
fmt.Println(rect[i][j])
}
break與continue
- break與continue用于控制for循環(huán)的代碼流程,并且只針對最靠近自己的外層for循環(huán)。
- break:退出for循環(huán),且本輪break下面的代碼不再執(zhí)行。
- continue:本輪continue下面的代碼不再執(zhí)行,進入for循環(huán)的下一輪。
//break和continue都是針對for循環(huán)的,不針對if或switch
//break和continue都是針對套在自己外面的最靠里的那層for循環(huán),不針對更外層的for循環(huán)(除非使用Label)
func complex_break_continue() {
const SIZE = 5
arr := [SIZE][SIZE]int{}
for i := 0; i < SIZE; i++ {
fmt.Printf("開始檢查第%d行\(zhòng)n", i)
if i%2 == 1 {
for j := 0; j < SIZE; j++ {
fmt.Printf("開始檢查第%d列\(zhòng)n", j)
if arr[i][j]%2 == 0 {
continue //針對第二層for循環(huán)
}
fmt.Printf("將要檢查第%d列\(zhòng)n", j+1)
}
break //針對第一層for循環(huán)
}
}
}
goto與Label
var i int = 4
MY_LABEL:
i += 3
fmt.Println(i)
goto MY_LABEL //返回定義MY_LABEL的那一行,把代碼再執(zhí)行一遍(會進入一個無限循環(huán))
if i%2 == 0 {
goto L1 //Label指示的是某一行代碼,并沒有圈定一個代碼塊,所以goto L1也會執(zhí)行L2后的代碼
} else {
goto L2//先使用Label
}
L1:
i += 3
L2: //后定義Label。Label定義后必須在代碼的某個地方被使用
i *= 3
??goto與Label結(jié)合可以實現(xiàn)break的功能,甚至比break更強大。
for i := 0; i < SIZE; i++ {
L2:
for j := 0; j < SIZE; j++ {
goto L1
}
}
L1:
xxx
- break、continue與Label結(jié)合使用可以跳轉(zhuǎn)到更外層的for循環(huán)。
- continue和break針對的Label必須寫在for前面,而goto可以針對任意位置的Label。
func break_label() {
const SIZE = 5
arr := [SIZE][SIZE]int{}
L1:
for i := 0; i < SIZE; i++ {
L2:
fmt.Printf("開始檢查第%d行\(zhòng)n", i)
if i%2 == 1 {
L3:
for j := 0; j < SIZE; j++ {
fmt.Printf("開始檢查第%d列\(zhòng)n", j)
if arr[i][j]%3 == 0 {
break L1 //直接退出最外層的fot循環(huán)
} else if arr[i][j]%3 == 1 {
goto L2 //continue和break針對的Label必須寫在for前面,而goto可以針對任意位置的Label
} else {
break L3
}
}
}
}
}