美文网首页
Golang sync.WaitGroup用法举例2

Golang sync.WaitGroup用法举例2

作者: FredricZhu | 来源:发表于2019-06-08 08:18 被阅读0次
    package main
    
    import (
        "fmt"
        "sync"
    )
    
    func main() {
        hello := func(wg *sync.WaitGroup, id int) {
            defer wg.Done()
            fmt.Printf("hello from %v\n", id)
        }
    
        numGreeters := 5
        var wg sync.WaitGroup
        wg.Add(numGreeters)
    
        for i := 0; i < numGreeters; i++ {
            go hello(&wg, i+1)
        }
        wg.Wait()
    }
    
    image.png

    相关文章

      网友评论

          本文标题:Golang sync.WaitGroup用法举例2

          本文链接:https://www.haomeiwen.com/subject/ubibxctx.html