Closures {.en}

闭包 {.zh}

::: {.en} Go supports anonymous functions, which can form closures. Anonymous functions are useful when you want to define a function inline without having to name it. :::

::: {.zh}

Go 支持 匿名函数,并能用其构造 闭包。如果要定义一个不需要命名的内联函数,匿名函数非常有用。

:::

  1. package main
  2. import "fmt"

::: {.en} This function intSeq returns another function, which we define anonymously in the body of intSeq. The returned function closes over the variable i to form a closure. :::

::: {.zh}

这个函数 intSeq 返回另一个在 intSeq 的主体内定义的匿名函数。这个返回的函数使用闭包的方式隐藏变量 i。

:::

  1. func intSeq() func() int {
  2. i := 0
  3. return func() int {
  4. i++
  5. return i
  6. }
  7. }
  8. func main() {

::: {.en} We call intSeq, assigning the result (a function) to nextInt. This function value captures its own i value, which will be updated each time we call nextInt. :::

::: {.zh}

我们调用 intSeq 函数,将返回值(一个函数)赋给 nextInt。这个函数值包含了自己的 i 值,这样每次调用 nextInt 时都会更新 i 的值。

:::

  1. nextInt := intSeq()

::: {.en} See the effect of the closure by calling nextInt a few times. :::

::: {.zh}

通过几次调用 nextInt 来查看闭包的效果。

:::

  1. fmt.Println(nextInt())

::: {.en} To confirm that the state is unique to that particular function, create and test a new one. :::

::: {.zh}

要确认状态对于特定函数是唯一的,我们 重新创建并测试一下。

:::

  1. newInts := intSeq()
  2. fmt.Println(newInts())
  3. }
  1. $ go run closures.go
  2. 1
  3. 2
  4. 3
  5. 1

::: {.en} The last feature of functions we’ll look at for now is recursion. :::

::: {.zh}

我们即将要学习的函数的最后一个特性是 递归。

:::