If/Else {.en}

If/Else 分支 {.zh}

::: {.en} Branching with if and else in Go is straight-forward. :::

::: {.zh}

if/else 分支结构在 Go 语言中非常直接。 :::

  1. package main
  2. import "fmt"
  3. func main() {

::: {.en} Here’s a basic example. :::

::: {.zh}

这是一个基本的例子。

:::

  1. if 7%2 == 0 {
  2. fmt.Println("7 is even")
  3. } else {
  4. fmt.Println("7 is odd")
  5. }

::: {.en} You can have an if statement without an else. :::

::: {.zh}

if 语句可以不包含 else

:::

  1. if 8%4 == 0 {
  2. fmt.Println("8 is divisible by 4")
  3. }

::: {.en} A statement can precede conditionals; any variables declared in this statement are available in all branches. :::

::: {.zh}

条件语句前可以有一个声明语句;声明的任何变量都可以用于所有分支。

:::

  1. if num := 9; num < 0 {
  2. fmt.Println(num, "is negative")
  3. } else if num < 10 {
  4. fmt.Println(num, "has 1 digit")
  5. } else {
  6. fmt.Println(num, "has multiple digits")
  7. }
  8. }

::: {.en} Note that you don’t need parentheses around conditions in Go, but that the braces are required. :::

::: {.zh}

请注意,Go 中的条件语句不需要括号,但需要大括号。

:::

  1. $ go run if-else.go
  2. 7 is odd
  3. 8 is divisible by 4
  4. 9 has 1 digit

::: {.en} There is no ternary if in Go, so you’ll need to use a full if statement even for basic conditions. :::

::: {.zh}

Go 中没有 三目运算符,因此即使基本条件也需要使用完整的 if 语句。

:::