Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
循环时goroutines内的值不正确_Go - Fatal编程技术网

循环时goroutines内的值不正确

循环时goroutines内的值不正确,go,Go,我已经通读并通过-race标志运行了我的代码,但我似乎无法指出这里的错误: package main import ( "fmt" ) func main() { i := 1 totalHashFields := 6 for i <= totalHashFields { Combinations(totalHashFields, i, func(c []int) { fmt.Println("Outside go

我已经通读并通过-race标志运行了我的代码,但我似乎无法指出这里的错误:

package main

import (
    "fmt"
)

func main() {
    i := 1
    totalHashFields := 6
    for i <= totalHashFields {
        Combinations(totalHashFields, i, func(c []int) {
            fmt.Println("Outside goroutine:", c)
            go func(c []int) {
                fmt.Println("Inside goroutine:", c)
            }(c)
        })
        i++
    }
}

func Combinations(n, m int, emit func([]int)) {
    s := make([]int, m)
    last := m - 1
    var rc func(int, int)
    rc = func(i, next int) {
        for j := next; j < n; j++ {
            s[i] = j
            if i == last {
                emit(s)
            } else {
                rc(i+1, j+1)
            }
        }
        return
    }
    rc(0, 0)
}

基本上,即使我将c作为参数传递给我的匿名go函数,该值始终不同于此范围之外的值。在上面的输出中,我期望2个“内部”值也分别为[0 1 4]和[0 1 2 3 4 5]。

问题是,您在distinc int切片上执行所有工作,但这些工作共享一个共同的支持数组:在完成
组合后
切片
s
将充满
5
s。main中的
c
与s共享底层支持数组

但是您的goroutines在组合完成之前不会开始执行,因此一旦开始,将看到
s
的最终值,即5s

在这里,像您这样传入切片并没有帮助,因为这样可以正确复制
c
,但不能复制备份数组

试一试


要制作c.

的“深度复制”,您应该发布一个工作示例。例如,您没有显示
组合
的功能,也没有显示
数据
的声明和使用方式。我已清理了代码,使其不包含任何不相关的内容。通过我最近的编辑,您应该可以仅复制/粘贴。
Outside goroutine: [0 1 4]
Inside goroutine: [5 5 5]
Outside goroutine: [0 1 2 3 4 5]
Inside goroutine: [5 5 5 5 5 5]
Combinations(totalHashFields, i, func(c []int) {
    fmt.Println("Outside goroutine:", c)
    cpy := make([]int, len(c))
    copy(cpy, c)
    go func(c []int) {
        fmt.Println("Inside goroutine:", c)
    }(cpy)
})