我是 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/