标签:简单 name style wait add when let targe pkg
WaitGroup 会将main goroutine阻塞直到所有的goroutine运行结束,从而达到并发控制的目的。使用方法非常简单,真心佩服创造Golang的大师们!
官网说明:一个WaitGroup锁等待一个goroutines合集结束。main goroutine里面调用Add方法设置需要等待的goroutines 数量,然后运行每一个goroutine,并且当其结束时调用Done方法。同时,main goroutine 被锁住直到所有的goroutines完成。
使用方法(官网Example):
var wg sync.WaitGroup
var urls = []string{
"http://www.golang.org/",
"http://www.google.com/",
"http://www.somestupidname.com/",
}
for _, url := range urls {
// Increment the WaitGroup counter.
wg.Add(1)
// Launch a goroutine to fetch the URL.
go func(url string) {
// Decrement the counter when the goroutine completes.
defer wg.Done()
// Fetch the URL.
http.Get(url)
}(url)
}
// Wait for all HTTP fetches to complete.
wg.Wait()
标签:简单 name style wait add when let targe pkg
原文地址:http://www.cnblogs.com/DillGao/p/6234461.html