Reputation: 32376
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?
Upvotes: 138
Views: 76480
Reputation: 2335
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()
}
Upvotes: 21
Reputation: 389
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(...)
}
Upvotes: 16
Reputation: 431
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; }()
}
Upvotes: -3
Reputation: 54107
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.
Upvotes: 79
Reputation: 166704
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.
Upvotes: 30