-
Notifications
You must be signed in to change notification settings - Fork 0
/
concurrentGet.go
49 lines (40 loc) · 760 Bytes
/
concurrentGet.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
48
49
package main
import (
"log"
"net/http"
"time"
)
type links struct {
url string
err error
latency time.Duration
}
func get(url string, ch chan<- links) {
start := time.Now()
if res, err := http.Get(url); err != nil {
ch <- links{url, err, 0} //I got some error while fetching url
} else {
t := time.Since(start).Round(time.Millisecond)
ch <- links{url, nil, t}
res.Body.Close()
}
}
func main() {
resultsChan := make(chan links)
urls := []string{
"https://www.google.com",
"https://www.amazon.in",
"https://www.facebook.com",
}
for _, url := range urls {
go get(url, resultsChan)
}
for range urls {
r := <-resultsChan
if r.err != nil {
log.Printf(r.err)
} else {
log.Printf(r.url, r.latency)
}
}
}