-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgorout_v2.go
47 lines (38 loc) · 924 Bytes
/
gorout_v2.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
38
39
40
41
42
43
44
45
46
47
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"sync"
)
// WaitGroup is used to wait for the program to finish goroutines.
var wg sync.WaitGroup
func wresponseSize(url string) {
// Schedule the call to WaitGroup's Done to tell goroutine is completed.
defer wg.Done()
fmt.Println("Step1: ", url)
response, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
fmt.Println("Step2: ", url)
defer response.Body.Close()
fmt.Println("Step3: ", url)
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println("Step4: ", len(body))
}
func main() {
// Add a count of three, one for each goroutine.
wg.Add(3)
fmt.Println("Start Goroutines")
go wresponseSize("https://www.godoc.org")
go wresponseSize("https://stackoverflow.com")
go wresponseSize("https://coderwall.com")
// Wait for the goroutines to finish.
wg.Wait()
fmt.Println("Terminating Program")
}