Golang sync.WaitGroup用法举例2
2019-06-08 本文已影响0人
FredricZhu
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