Why doesn't Go allow nested function declarati

2020-02-17 08:20发布

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?

6条回答
beautiful°
2楼-- · 2020-02-17 08:51

I think there are 3 reasons why this obvious feature isn't allowed

  1. It would complicate the compiler slightly. At the moment the compiler knows all functions are at the top level.
  2. It would make a new class of programmer error - you could refactor something and accidentally nest some functions.
  3. 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.

查看更多
我只想做你的唯一
3楼-- · 2020-02-17 08:57

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; }()
}
查看更多
Bombasti
4楼-- · 2020-02-17 09:04

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楼-- · 2020-02-17 09:05

Sure they are. You just have to assign them to a variable:

func main() {
    inc := func(x int) int { return x+1; }
}
查看更多
Emotional °昔
6楼-- · 2020-02-17 09:05

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.

查看更多
Summer. ? 凉城
7楼-- · 2020-02-17 09:13

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(...)
}
查看更多
登录 后发表回答