dongqiuge5435 2019-05-22 18:14
浏览 104
已采纳

在函数内部定义函数

When defining a inner function which utilizes the variables of outer scope, should I pass the variables to the inner function as parameters?

In my example, generate and generate2 both give me same result, is there a reason I should choose any one of them?

The code picks key 1 to generate combinations with key 3,4,5, then picks key 2 to generate combinations with key 3,4,5.

package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello, playground")

    src := map[int][]string{
        1: []string{"1", "11", "111"},
        2: []string{"2", "22"},
        3: []string{"3"},
        4: []string{"4"},
        5: []string{"5", "55"},
    }

    result2 := generate2(src)

    fmt.Println(result2)

    result := generate(src)

    fmt.Println(result)

}

func generate(src map[int][]string) []string {
    var combo []string
    var add = func(f []string) {
        for _, v := range f {

            for _, p := range src[3] {
                for _, q := range src[4] {
                    for _, r := range src[5] {
                        combo = append(combo, v+p+q+r)
                    }
                }
            }

        }
    }

    add(src[1])
    add(src[2])
    return combo
}

func generate2(src map[int][]string) []string {
    var combo []string
    var add = func(f []string, combo []string, src map[int][]string) []string {
        for _, v := range f {
            for _, p := range src[3] {
                for _, q := range src[4] {
                    for _, r := range src[5] {
                        combo = append(combo, v+p+q+r)
                    }
                }
            }
        }
        return combo
    }

    combo = add(src[1], combo, src)
    combo = add(src[2], combo, src)
    return combo
}
  • 写回答

2条回答 默认 最新

  • dpdfh60088 2019-05-22 18:27
    关注

    When defining a inner function which utilizes the variables of outer scope, should I pass the variables to the inner function as parameters?

    It depends on what you want to achieve.

    What you call "a function inside a function" is actually called "a closure" (and some people call it "lambda").

    Closures capture variables from the outer lexical scope, referenced in its body. In Go, this capturing is done "by reference" or "by name" which basically means each time a closure is called it will "see" current values of the variables it closes over, not the values these variables had at the time the closure was created—observe that the program:

    package main
    
    import (
        "fmt"
    )
    
    func main() {
        i := 42
    
        fn := func() {
          fmt.Println(i)
        }
    
        fn()
        i = 12
        fn()
    }
    

    would output

    42
    12
    

    Conversely, when you pass values as arguments to calls to a closure, each call will see exactly the values passed to it.

    I hope you now see that what strategy to pick largely depends on what you want.

    Conceptually, you may think of a closure as being an instance of an ad-hoc anonymous struct data type, the fields of which are pointers to the variables the closure closes over, and each call to that closure being analogous to calling some (anonymous, sole) method provided by that type (actually, that's what the compiler usually does behind your back to implement a closure). Such "method" may have arguments, and whether it should have them, and what should go to the type's fields and what should be that method's arguments can be judged using the usual approach you employ with regular types.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分