-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
48 lines (40 loc) · 969 Bytes
/
main.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
// Fetch ranges over the CLI arguments (that should be URLs) and fetches each of
// them. It reports the size of the resource at the URL and the time it took to
// download it. It does so concurrently.
//
// Adapted from https://github.com/adonovan/gopl.io/tree/master/ch1/fetchall.
package main
import (
"fmt"
"io"
"net/http"
"os"
"time"
)
func main() {
start := time.Now()
ch := make(chan string)
for _, arg := range os.Args[1:] {
go fetch(arg, ch)
}
for range os.Args[1:] {
fmt.Println(<-ch)
}
fmt.Printf("%.3fs elapsed\n", time.Since(start).Seconds())
}
func fetch(url string, ch chan<- string) {
start := time.Now()
resp, err := http.Get(url)
if err != nil {
ch <- fmt.Sprint(err)
return
}
defer resp.Body.Close()
n, err := io.Copy(io.Discard, resp.Body)
if err != nil {
ch <- fmt.Sprintf("while reading %s: %v", url, err)
return
}
// time size url
ch <- fmt.Sprintf("%.3fs %7d %s", time.Since(start).Seconds(), n, url)
}