concurrency - 没有看到 goroutines 的预期副作用

标签 concurrency go append slice goroutine

我正在尝试掌握 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/

相关文章:

amazon-web-services - Golang Aws S3 NoSuchKey : The specified key does not exist

python - 在 Python 中创建列表对象类

java - 线程串行运行而不是并行

concurrency - 使用共享 map 的不错的惯用方式

java - 两个方法同时使用@Asynchronous注解

go - 如何使用 request.Context 而不是 CloseNotifier?

parsing - Go中如何高效方便的解析一条简单的消息?

python - 如何在 Python Pandas 中的数组、列表或数据帧中的每个单个数字元素的末尾 append 1?

jquery - 使用 JQuery 很好地向 DOM 添加 HTML 元素

linux - 如何更改网络设备操作表的安全性?