go - Golang 中的匿名函数

标签 go

我是 Go 语言和函数式编程的新手。

我的问题是:但是你能不能列举golang中匿名函数的好处。我从这个了解site匿名函数是“只需要运行一次并且不需要被引用的段代码”。但我找不到它们的好处。

最佳答案

一个函数字面量表示一个匿名函数。 specification mentions the primary benefit of function literals :

Function literals are closures: they may refer to variables defined in a surrounding function. Those variables are then shared between the surrounding function and the function literal, and they survive as long as they are accessible.

以下是匿名函数的一些使用示例:sort.Slice , http mux.HandleFunc , panic recovery , goroutines , filepath.Walk , ast.Inspect .

关于go - Golang 中的匿名函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47999605/

相关文章:

go - Beego - 端点测试

go - 生产中的 log.SetFlags(log.LstdFlags | log.Lshortfile)

go - 如何部署 travis 自动构建,我的失败了,我不知道如何修复

c - 尝试扩展 golang Pigpio 包装函数 gpioWaveAddGeneric

html - 获取模板以在 Golang 中正确读取 CSS 文件

file-upload - 使用 OS Open 将 Golang 文件上传到 s3

go-swagger - 找不到 swagger 服务命令

go - 仅从 Go 中的函数引用获取接收器类型和方法名称

go - 如何使用 gccgo 导入非标准库包

go - 使用 Gin 框架验证 Golang 中的枚举