/
waitgroup.go
37 lines (34 loc) · 756 Bytes
/
waitgroup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package waitgroup
import (
"fmt"
"io/ioutil"
"net/http"
"sync"
)
func getUrl(wg *sync.WaitGroup, url string, ch chan string) {
res, err := http.Get(url)
if err != nil {
fmt.Println(err.Error())
}
b, _ := ioutil.ReadAll(res.Body)
_ = res.Body.Close()
wg.Done()
ch <- string(b)
}
func WaitGroup() {
// WaitGroup: first: init wg
// WaitGroup: then: before goroutine wg.add
// WaitGroup: then: after goroutine wg.done
// WaitGroup: last: main goroutine wg.wait
resultCh := make(chan string)
wg := &sync.WaitGroup{}
urls := []string{"http://www.baidu.com", "https://gorm.io"}
for _, url := range urls {
wg.Add(1)
go getUrl(wg, url, resultCh)
}
wg.Wait()
for i := 0; i < len(urls); i++ {
fmt.Println("result: ", <-resultCh)
}
}