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?
I think there are 3 reasons why this obvious feature isn't allowed
Those are just my opinions though - I haven't seen an official pronouncement from the language designers.
You just have to call it immediately by adding
()
to the end.Edit: cannot have function name...so it's just a lambda func getting called right away:
Here's a way to implement nested functions and functions within nested functions
Sure they are. You just have to assign them to a variable:
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.
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:
Inner functions are often handy for local goroutines: