Multiple Return Values {.en}

多返回值 {.zh}

::: {.en} Go has built-in support for multiple return values. This feature is used often in idiomatic Go, for example to return both result and error values from a function. :::

::: {.zh}

Go 内置支持多返回值。此功能在 Go 中经常使用,例如从函数同时返回结果和错误值。

:::

  1. package main
  2. import "fmt"

::: {.en} The (int, int) in this function signature shows that the function returns 2 ints. :::

::: {.zh}

这个函数中的 (int,int) 表明函数返回 2 个 int 类型的值。

:::

  1. func vals() (int, int) {
  2. return 3, 7
  3. }
  4. func main() {

::: {.en} Here we use the 2 different return values from the call with multiple assignment. :::

::: {.zh}

这里我们使用多赋值操作来使用这 2 个不同的返回值。

:::

  1. a, b := vals()
  2. fmt.Println(a)
  3. fmt.Println(b)

::: {.en} If you only want a subset of the returned values, use the blank identifier _. :::

::: {.zh}

如果你只想要返回值的一部分,请使用空标识符 _

:::

  1. _, c := vals()
  2. fmt.Println(c)
  3. }
  1. $ go run multiple-return-values.go
  2. 3
  3. 7

::: {.en} Accepting a variable number of arguments is another nice feature of Go functions; we’ll look at this next. :::

::: {.zh}

接受可变数量的参数是 Go 函数的另一个不错的特性;我们接下来会学习此特性。

:::