问题
Edit: If it was not clear what I was asking: what are the problems that are mitigated by not allowing nested function declarations?
Lambdas work as expected:
func main() {
inc := func(x int) int { return x+1; }
}
However, the following declaration inside a declaration is not allowed:
func main() {
func inc(x int) int { return x+1; }
}
For what reason are nested functions not allowed?
回答1:
I think there are 3 reasons why this obvious feature isn't allowed
- It would complicate the compiler slightly. At the moment the compiler knows all functions are at the top level.
- It would make a new class of programmer error - you could refactor something and accidentally nest some functions.
- Having a different syntax for functions and closures is a good thing. Making a closure is potentially more expensive than making a function so you should know you are doing it.
Those are just my opinions though - I haven't seen an official pronouncement from the language designers.
回答2:
Sure they are. You just have to assign them to a variable:
func main() {
inc := func(x int) int { return x+1; }
}
回答3:
Frequently Asked Questions (FAQ)
Why does Go not have feature X?
Every language contains novel features and omits someone's favorite feature. Go was designed with an eye on felicity of programming, speed of compilation, orthogonality of concepts, and the need to support features such as concurrency and garbage collection. Your favorite feature may be missing because it doesn't fit, because it affects compilation speed or clarity of design, or because it would make the fundamental system model too difficult.
If it bothers you that Go is missing feature X, please forgive us and investigate the features that Go does have. You might find that they compensate in interesting ways for the lack of X.
What would justify the complexity and expense of adding nested functions? What do yau want to do that you can't do without nested functions? Et cetera.
回答4:
Here's a way to implement nested functions and functions within nested functions
package main
import "fmt"
func main() {
nested := func() {
fmt.Println("I am nested")
deeplyNested := func() {
fmt.Println("I am deeply nested")
}
deeplyNested()
}
nested()
}
回答5:
Nested functions are allowed in Go. You just need to assign them to local variables within the outer function, and call them using those variables.
Example:
func outerFunction(iterations int, s1, s2 string) int {
someState := 0
innerFunction := func(param string) int {
// Could have another nested function here!
totalLength := 0
// Note that the iterations parameter is available
// in the inner function (closure)
for i := 0; i < iterations; i++) {
totalLength += len(param)
}
return totalLength
}
// Now we can call innerFunction() freely
someState = innerFunction(s1)
someState += innerFunction(s2)
return someState
}
myVar := outerFunction(100, "blah", "meh")
Inner functions are often handy for local goroutines:
func outerFunction(...) {
innerFunction := func(...) {
...
}
go innerFunction(...)
}
回答6:
You just have to call it immediately by adding ()
to the end.
func main() {
func inc(x int) int { return x+1; }()
}
Edit: cannot have function name...so it's just a lambda func getting called right away:
func main() {
func(x int) int { return x+1; }()
}
来源:https://stackoverflow.com/questions/21961615/why-doesnt-go-allow-nested-function-declarations-functions-inside-functions