gpt4 book ai didi

go - 组合使用goroutine

转载 作者:数据小太阳 更新时间:2023-10-29 03:40:26 24 4
gpt4 key购买 nike

我正在尝试让代码工作,该代码几乎涉及 goroutine 中的 channel (在 C# 中类似于 yield 的行为)

代码涉及从 slice 中获取可迭代矩阵,如下所示:

elements := []float64{1, 2, 3, 4}

expected := [][]float64{
{1},
{2},
{3},
{4},
{1, 2},
{1, 3},
{2, 3},
{1, 4},
{2, 4},
{3, 4},
{1, 2, 3},
{1, 2, 4},
{1, 3, 4},
{2, 3, 4},
{1, 2, 3, 4},
}

我尝试通过以下方式应用 Knuth:

func Combinadic(values []float64) <-chan []float64 {
ch := make(chan []float64)
go func() {
for i := 0; i < len(values); i++ {

for value := range CombinadicK(values, i+1) {
ch <- value
}

}
close(ch)
}()

return ch

}

func CombinadicK(values []float64, k int) <-chan []float64 {
chnl := make(chan []float64)
go func() {
n := len(values)

t := k

c := make([]int, t+3)
current := make([]float64, t)

x := 0
j := 0

for j = 1; j <= t; j++ {
c[j] = j - 1
}

c[t+1] = n
c[t+2] = 0

j = t

for {
for i := 0; i < len(current); i++ {
current[i] = values[c[i+1]]
}

chnl <- current

if j > 0 {
x = j
} else {
if c[1]+1 < c[2] {
c[1]++
continue
} else {
j = 2
}
}

for {
c[j-1] = j - 2
x = c[j] + 1
if x == c[j+1] {
j++
} else {
break
}
}

c[j] = x
j--

if j >= t {
break
}
}

close(chnl)
}()

return chnl
}

它似乎给出了每行的随机数,但预期的结构(每行的项目数)似乎没问题。

Go Playground中的代码

最佳答案

您有一场数据竞赛。您的结果未定义。

$ go run -race racer.go
==================
WARNING: DATA RACE
Read at 0x00c00009c010 by main goroutine:
reflect.typedmemmove()
/home/peter/go/src/runtime/mbarrier.go:177 +0x0
reflect.packEface()
/home/peter/go/src/reflect/value.go:119 +0x103
reflect.valueInterface()
/home/peter/go/src/reflect/value.go:1027 +0x16f
fmt.(*pp).printValue()
/home/peter/go/src/reflect/value.go:997 +0x38f7
fmt.(*pp).printValue()
/home/peter/go/src/fmt/print.go:868 +0xec7
fmt.(*pp).printArg()
/home/peter/go/src/fmt/print.go:715 +0x2ee
fmt.(*pp).doPrintln()
/home/peter/go/src/fmt/print.go:1172 +0xad
fmt.Fprintln()
/home/peter/go/src/fmt/print.go:263 +0x65
main.main()
/home/peter/go/src/fmt/print.go:273 +0x14b

Previous write at 0x00c00009c010 by goroutine 8:
main.CombinadicK.func1()
/home/peter/racer.go:48 +0x1e6

Goroutine 8 (running) created at:
main.CombinadicK()
/home/peter/racer.go:26 +0x96
main.Combinadic.func1()
/home/peter/racer.go:12 +0xda
==================
[3]
[3]
[4]
[4]
[2 3]
[2 4]
[1 4]
[3 4]
[3 4]
[3 4]
[1 3 4]
[1 3 4]
[1 3 4]
[2 3 4]
[1 2 3 4]
Found 1 data race(s)
exit status 66
$

racer.go:

package main

import (
"fmt"
)

func Combinadic(values []float64) <-chan []float64 {
ch := make(chan []float64)
go func() {
for i := 0; i < len(values); i++ {

for value := range CombinadicK(values, i+1) {
ch <- value
}

}
close(ch)
}()

return ch

}

func CombinadicK(values []float64, k int) <-chan []float64 {
chnl := make(chan []float64)
go func() {
n := len(values)

t := k

c := make([]int, t+3)
current := make([]float64, t)

x := 0
j := 0

for j = 1; j <= t; j++ {
c[j] = j - 1
}

c[t+1] = n
c[t+2] = 0

j = t

for {
for i := 0; i < len(current); i++ {
current[i] = values[c[i+1]]
}

chnl <- current

if j > 0 {
x = j
} else {
if c[1]+1 < c[2] {
c[1]++
continue
} else {
j = 2
}
}

for {
c[j-1] = j - 2
x = c[j] + 1
if x == c[j+1] {
j++
} else {
break
}
}

c[j] = x
j--

if j >= t {
break
}
}

close(chnl)
}()

return chnl
}

func main() {
elements := []float64{1, 2, 3, 4}
for v := range Combinadic(elements) {
fmt.Println(v)
}
}

Playground :https://play.golang.org/p/hhQgVdqe6l1


Go: Data Race Detector

关于go - 组合使用goroutine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56310856/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com