Created
July 3, 2017 16:51
-
-
Save benjamw/d036f7f8294c70a8bb7831e6c6dc99da to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan. | |
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ | |
// See page 17. | |
//!+ | |
// Fetchall fetches URLs in parallel and reports their times and sizes. | |
package main | |
import ( | |
"fmt" | |
"io" | |
"io/ioutil" | |
"net/http" | |
"os" | |
"time" | |
) | |
func main() { | |
start := time.Now() | |
ch := make(chan string) | |
for _, url := range os.Args[1:] { | |
fmt.Printf("Fetching %s...\n", url) | |
go fetch(url, ch) // start a goroutine | |
} | |
for range os.Args[1:] { | |
fmt.Println("Listening for response...") | |
fmt.Println(<-ch) // receive from channel ch | |
} | |
fmt.Printf("%.2fs elapsed\n", time.Since(start).Seconds()) | |
} | |
func fetch(url string, ch chan<- string) { | |
start := time.Now() | |
resp, err := http.Get(url) | |
defer resp.Body.Close() // don't leak resources | |
if err != nil { | |
ch <- fmt.Sprint(err) // send to channel ch | |
return | |
} | |
nbytes, err := io.Copy(ioutil.Discard, resp.Body) | |
if err != nil { | |
ch <- fmt.Sprintf("while reading %s: %v", url, err) | |
return | |
} | |
secs := time.Since(start).Seconds() | |
ch <- fmt.Sprintf("%.2fs %7d %s", secs, nbytes, url) | |
} | |
//!- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
D:\Development\go_projects\projects\src\gopl.io>fetchall_b1.exe http://iohelix.net/games https://google.com http://gopl.io https://golang.org http://ihbuc5xciuv75f7vboyg8 | |
6rciugvivo.com | |
Fetching http://iohelix.net/games... | |
Fetching https://google.com... | |
Fetching http://gopl.io... | |
Fetching https://golang.org... | |
Fetching http://ihbuc5xciuv75f7vboyg86rciugvivo.com... | |
Listening for response... | |
Get http://ihbuc5xciuv75f7vboyg86rciugvivo.com: dial tcp: lookup ihbuc5xciuv75f7vboyg86rciugvivo.com: no such host | |
Listening for response... | |
0.18s 2662 http://iohelix.net/games | |
Listening for response... | |
0.22s 4154 http://gopl.io | |
Listening for response... | |
0.37s 7902 https://golang.org | |
Listening for response... | |
0.45s 12947 https://google.com | |
0.45s elapsed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment