sync.WaitGroup
作者:
Feng_Sir | 来源:发表于
2021-03-26 11:13 被阅读0次package main
import (
"fmt"
"sync"
"net/http"
)
func main() {
var wg sync.WaitGroup
var urls = []string{
"http://www.golang.org/",
"http://www.google.com/",
"http://www.baiyuxiong.com/",
}
for _, url := [range](http://www.php.net/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)
fmt.Println(url);
}(url)
}
// Wait for all HTTP fetches to complete.
wg.Wait()
fmt.Println("over");
}
本文标题:sync.WaitGroup
本文链接:https://www.haomeiwen.com/subject/dodbqltx.html
网友评论