在Go语言中使用匿名函数创建闭包

GolangGolangBeginner
立即练习

This tutorial is from open-source community. Access the source code

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在本实验中,你将学习如何使用匿名函数在 Go 语言中创建闭包。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Golang`")) -.-> go/FunctionsandControlFlowGroup(["`Functions and Control Flow`"]) go/FunctionsandControlFlowGroup -.-> go/closures("`Closures`") subgraph Lab Skills go/closures -.-> lab-15461{{"`在Go语言中使用匿名函数创建闭包`"}} end

闭包

你需要创建一个返回另一个函数的函数。返回的函数每次被调用时,都应将一个变量的值加 1。该变量对于每个返回的函数来说应该是唯一的。

  • 函数 intSeq 应返回另一个函数。
  • 返回的函数每次被调用时,都应将一个变量的值加 1。
  • 该变量对于每个返回的函数来说应该是唯一的。
$ go run closures.go
1
2
3
1

## 目前我们要研究的函数的最后一个特性是
## 递归。

以下是完整代码:

// Go 支持 [_匿名函数_](https://en.wikipedia.org/wiki/Anonymous_function),
// 它可以形成 <a href="https://en.wikipedia.org/wiki/Closure_(computer_science)"><em>闭包</em></a>。
// 当你想在代码中内联定义一个函数而无需为其命名时,匿名函数很有用。

package main

import "fmt"

// 函数 `intSeq` 返回另一个函数,
// 我们在 `intSeq` 的函数体中匿名定义它。返回的函数
// 通过变量 `i` 形成一个闭包。
func intSeq() func() int {
	i := 0
	return func() int {
		i++
		return i
	}
}

func main() {

	// 我们调用 `intSeq`,将结果(一个函数)
	// 赋给 `nextInt`。这个函数值捕获了它自己的
	// `i` 值,每次我们调用 `nextInt` 时,该值都会更新。
	nextInt := intSeq()

	// 通过多次调用 `nextInt` 来查看闭包的效果。
	fmt.Println(nextInt())
	fmt.Println(nextInt())
	fmt.Println(nextInt())

	// 为了确认状态对于那个特定函数是唯一的,
	// 创建并测试一个新的函数。
	newInts := intSeq()
	fmt.Println(newInts())
}

总结

在本实验中,你学习了如何使用匿名函数在 Go 语言中创建闭包。当你想要内联定义一个函数而无需为其命名时,闭包非常有用。

您可能感兴趣的其他 Golang 教程