go协程同步

Posted zengyjun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了go协程同步相关的知识,希望对你有一定的参考价值。

package main

import (
    "fmt"
    "sync"
)

func print(idx int, wg *sync.WaitGroup) {
    fmt.Printf("index %d: 
", idx)
    wg.Done()

}
func main() {
    var wg sync.WaitGroup
    for i := 0; i < 10; i++ {
        wg.Add(1)
        go print(i, &wg)
    }
    wg.Wait()
}
运行结果:
index 3
index 7
index 9
index 1
index 2
index 4
index 0
index 5
index 6
index 8

 

以上是关于go协程同步的主要内容,如果未能解决你的问题,请参考以下文章