duanqu9279 2014-11-01 20:20
浏览 73
已采纳

Go中捕获的闭包(用于循环变量)

Shouldn't Go compiler capture for...range loop variables as a locally assigned closure variable?

Long Version:

This caused me some confusion in C# too and I was trying to understand it; that why it is fixed in C# 5.0 foreach (reason: the loop variable can not change inside the body of loop) and the reasoning for not fixing it in C# for loops (reason: the loop variable can change inside the body of loop).

Now (to me) for...range loops in Go seems pretty much like foreach loops in C#, but despite the fact that we can not alter those variables (like k and v in for k, v := range m { ... }); still we have to copy them to some local closures first, for them to behave as expected.

What is the reasoning behind this? (I suspect it's because Go treats any for loop the same way; but I'm not sure).

Here is some code to examine described behavior:

func main() {
    lab1() // captured closure is not what is expected
    fmt.Println(" ")

    lab2() // captured closure is not what is expected
    fmt.Println(" ")

    lab3() // captured closure behaves ok
    fmt.Println(" ")
}

func lab3() {
    m := make(map[int32]int32)
    var i int32
    for i = 1; i <= 10; i++ {
        m[i] = i
    }

    l := [](func() (int32, int32)){}
    for k, v := range m {
        kLocal, vLocal := k, v // (C) captures just the right values assigned to k and v
        l = append(l, func() (int32, int32) {
            return kLocal, vLocal
        })
    }

    for _, x := range l {
        k, v := x()
        fmt.Println(k, v)
    }
}

func lab2() {
    m := make(map[int32]int32)
    var i int32
    for i = 1; i <= 10; i++ {
        m[i] = i
    }

    l := [](func() (int32, int32)){}
    for k, v := range m {
        l = append(l, func() (int32, int32) {
            kLocal, vLocal := k, v // (B) captures just the last values assigned to k and v from the range
            return kLocal, vLocal
        })
    }

    for _, x := range l {
        k, v := x()
        fmt.Println(k, v)
    }
}

func lab1() {
    m := make(map[int32]int32)
    var i int32
    for i = 1; i <= 10; i++ {
        m[i] = i
    }

    l := [](func() (int32, int32)){}
    for k, v := range m {
        l = append(l, func() (int32, int32) { return k, v }) // (A) captures just the last values assigned to k and v from the range
    }

    for _, x := range l {
        k, v := x()
        fmt.Println(k, v)
    }
}

As it is shown in lab1, at the comment // (A) we get just the last values from the range; the output is like printing 9,9 ten times instead of showing expected result like 1,1, 2,2, ... (and of-course maps are not necessarily sorted in Go so we may see 3,3 ten times as the last pair of values; instead of 10,10 ten times as the last pair of values). The same goes for code at comment // (B) at lab2, which was expected because we are trying to capture outer variables inside the inner scope (I put this one too just to try that). In lab3 at code at comment // (C) everything works fine and you will see ten pairs of numbers there like 1,1, 2,2, ....

I was trying to use closure+function as a replacement for tuples in Go.

  • 写回答

1条回答 默认 最新

  • dongle0396 2014-11-01 22:42
    关注

    Do you want the closure over the variable or the value? For example,

    package main
    
    import "fmt"
    
    func VariableLoop() {
        f := make([]func(), 3)
        for i := 0; i < 3; i++ {
            // closure over variable i
            f[i] = func() {
                fmt.Println(i)
            }
        }
        fmt.Println("VariableLoop")
        for _, f := range f {
            f()
        }
    }
    
    func ValueLoop() {
        f := make([]func(), 3)
        for i := 0; i < 3; i++ {
            i := i
            // closure over value of i
            f[i] = func() {
                fmt.Println(i)
            }
        }
        fmt.Println("ValueLoop")
        for _, f := range f {
            f()
        }
    }
    
    func VariableRange() {
        f := make([]func(), 3)
        for i := range f {
            // closure over variable i
            f[i] = func() {
                fmt.Println(i)
            }
        }
        fmt.Println("VariableRange")
        for _, f := range f {
            f()
        }
    }
    
    func ValueRange() {
        f := make([]func(), 3)
        for i := range f {
            i := i
            // closure over value of i
            f[i] = func() {
                fmt.Println(i)
            }
        }
        fmt.Println("ValueRange")
        for _, f := range f {
            f()
        }
    }
    
    func main() {
        VariableLoop()
        ValueLoop()
        VariableRange()
        ValueRange()
    }
    

    Output:

    VariableLoop
    3
    3
    3
    ValueLoop
    0
    1
    2
    VariableRange
    2
    2
    2
    ValueRange
    0
    1
    2
    

    References:

    The Go Programming Language Specification

    Function literals

    Function literals are closures: they may refer to variables defined in a surrounding function. Those variables are then shared between the surrounding function and the function literal, and they survive as long as they are accessible.

    Go FAQ: What happens with closures running as goroutines?

    To bind the current value of v to each closure as it is launched, one must modify the inner loop to create a new variable each iteration. One way is to pass the variable as an argument to the closure.

    Even easier is just to create a new variable, using a declaration style that may seem odd but works fine in Go.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?