forked from torbiak/gopl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
50 lines (44 loc) · 1.02 KB
/
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
49
50
// ex1.10 fetches URLs in parallel and reports their times and sizes.
package main
import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"time"
)
func main() {
start := time.Now()
ch := make(chan string)
for _, url := range os.Args[1:] {
go fetch(url, ch) // start a goroutine
}
for range os.Args[1:] {
fmt.Println(<-ch) // receive from channel ch
}
fmt.Printf("%.2fs elapsed\n", time.Since(start).Seconds())
}
func fetch(uri string, ch chan<- string) {
start := time.Now()
resp, err := http.Get(uri)
if err != nil {
ch <- fmt.Sprint(err) // send to channel ch
return
}
f, err := os.Create(url.QueryEscape(uri))
if err != nil {
ch <- err.Error()
}
nbytes, err := io.Copy(f, resp.Body)
resp.Body.Close() // don't leak resources
if closeErr := f.Close(); err == nil { // example from chapter 5.8, gopl.io/ch5/fetch
err = closeErr
}
if err != nil {
ch <- fmt.Sprintf("while reading %s: %v", uri, err)
return
}
secs := time.Since(start).Seconds()
ch <- fmt.Sprintf("%.2fs %7d %s", secs, nbytes, uri)
}