Skip to content

Go條件控制

在 Go 中,條件控制語句總共有三種ifswitchselectselect相對前兩者而言比較特殊,本節不會講解,將會留到並發那一節再做介紹。

if else

if else 至多兩個判斷分支,語句格式如下

go
if expression {

}

或者

go
if expression {

}else {

}

expression必須是一個布爾表達式,即結果要麼為真要麼為假,必須是一個布爾值,例子如下:

go
func main() {
   a, b := 1, 2
   if a > b {
      b++
   } else {
      a++
   }
}

也可以把表達式寫的更復雜些,必要時為了提高可讀性,應當使用括號來顯式的表示誰應該優先計算。

go
func main() {
   a, b := 1, 2
    if a<<1%100+3 > b*100/20+6 { // (a<<1%100)+3 > (b*100/20)+6
      b++
   } else {
      a++
   }
}

同時if語句也可以包含一些簡單的語句,例如:

go
func main() {
  if x := 1 + 1; x > 2 {
    fmt.Println(x)
  }
}

else if

else if 語句可以在if else的基礎上創建更多的判斷分支,語句格式如下:

go
if expression1 {

}else if expression2 {

}else if expression3 {

}else {

}

在執行的過程中每一個表達式的判斷是從左到右,整個if語句的判斷是從上到下 。一個根據成績打分的例子如下,第一種寫法

go
func main() {
   score := 90
   var ans string
   if score == 100 {
      ans = "S"
   } else if score >= 90 && score < 100 {
      ans = "A"
   } else if score >= 80 && score < 90 {
      ans = "B"
   } else if score >= 70 && score < 80 {
      ans = "C"
   } else if score >= 60 && score < 70 {
      ans = "E"
   } else if score >= 0 && score < 60 {
      ans = "F"
   } else {
      ans = "nil"
   }
   fmt.Println(ans)
}

第二種寫法利用了if語句是從上到下的判斷的前提,所以代碼要更簡潔些。

go
func main() {
  score := 90
  var ans string
  if score >= 0 && score < 60 {
    ans = "F"
  } else if score < 70 {
    ans = "D"
  } else if score < 80 {
    ans = "C"
  } else if score < 90 {
    ans = "B"
  } else if score < 100 {
    ans = "A"
  } else if score == 100 {
    ans = "S"
    }else {
        ans = "nil"
    }
  fmt.Println(ans)
}

switch

switch語句也是一種多分支的判斷語句,語句格式如下:

go
switch expr {
  case case1:
    statement1
  case case2:
    statement2
  default:
    default statement
}

一個簡單的例子如下

go
func main() {
   str := "a"
   switch str {
   case "a":
      str += "a"
      str += "c"
   case "b":
      str += "bb"
      str += "aaaa"
   default: // 當所有case都不匹配後,就會執行default分支
      str += "CCCC"
   }
   fmt.Println(str)
}

還可以在表達式之前編寫一些簡單語句,例如聲明新變量

go
func main() {
  switch num := f(); { // 等價於 switch num := f(); true {
  case num >= 0 && num <= 1:
    num++
  case num > 1:
    num--
    fallthrough
  case num < 0:
    num += num
  }
}

func f() int {
  return 1
}

switch語句也可以沒有入口處的表達式。

go
func main() {
   num := 2
   switch { // 等價於 switch true {
   case num >= 0 && num <= 1:
      num++
   case num > 1:
      num--
   case num < 0:
      num *= num
   }
   fmt.Println(num)
}

通過fallthrough關鍵字來繼續執行相鄰的下一個分支。

go
func main() {
   num := 2
   switch {
   case num >= 0 && num <= 1:
      num++
   case num > 1:
      num--
      fallthrough // 執行完該分支後,會繼續執行下一個分支
   case num < 0:
      num += num
   }
   fmt.Println(num)
}

label

標簽語句,給一個代碼塊打上標簽,可以是gotobreakcontinue的目標。例子如下:

go
func main() {
  A:
    a := 1
  B:
    b := 2
}

單純的使用標簽是沒有任何意義的,需要結合其他關鍵字來進行使用。

goto

goto將控制權傳遞給在同一函數對應標簽的語句,示例如下:

go
func main() {
   a := 1
   if a == 1 {
      goto A
   } else {
      fmt.Println("b")
   }
A:
   fmt.Println("a")
}

在實際應用中goto用的很少,跳來跳去的很降低代碼可讀性,性能消耗也是一個問題。

Golang學習網由www.golangdev.cn整理維護