美文网首页
sync.WaitGroup

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