我正在尝试掌握 goroutines。拿这段代码:
package main
import "fmt"
var (
b1 []float64
b2 []float64
)
func main() {
go fill(&b1, 10)
go fill(&b2, 10)
fmt.Println(b1,b2)
var s string
fmt.Scanln(&s)
}
func fill(a *[]float64, n int) {
for i:=0; i<n; i++ {
*a = append(*a, rand.Float64()*100)
}
}
如您所见,我正在尝试填充两个 slice 。但是当以这种方式运行时(使用 go fill()
),它会打印两个空 slice 。为什么这不起作用?
最佳答案
在您使用 sync.WaitGroup
明确等待它们之前,您启动的任何 goroutine 都不能保证已经完成(甚至开始!) 、 channel 或其他机制。 This works :
package main
import (
"fmt"
"math/rand"
"sync"
)
var (
b1 []float64
b2 []float64
)
func main() {
wg := new(sync.WaitGroup)
wg.Add(2)
go fill(&b1, 10, wg)
go fill(&b2, 10, wg)
wg.Wait()
fmt.Println(b1)
fmt.Println(b2)
}
func fill(a *[]float64, n int, wg *sync.WaitGroup) {
for i := 0; i < n; i++ {
*a = append(*a, rand.Float64()*100)
}
wg.Done()
}
(就风格而言,如果是我 I'd make this function return the enlarged slice so it's similar to append()
itself 和 Go 的代码审查评论 suggest passing values,尽管是 it's not at all unconventional to extend a slice passed as a pointer receiver ("this") parameter。)
关于concurrency - 没有看到 goroutines 的预期副作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29682422/