Learning how to time pulling data from multiple URLs...
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
754 B

package main
import (
"fmt"
"io/ioutil"
"net/http"
"time"
)
func getPage(url string) (int, time.Duration, error) {
start := time.Now()
resp, err := http.Get(url)
if err != nil {
return 0, 0, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, 0, err
}
return len(body), time.Since(start), nil
}
func main() {
sites := map[string]string{
"Google": "http://google.com",
"Yahoo": "http://yahoo.com",
"Bing": "http://bing.com",
}
start := time.Now()
for name, url := range sites {
length, dur, err := getPage(url)
if err != nil {
fmt.Printf("%s %s\n", name, err)
}
fmt.Printf("%s %d %v\n", name, length, dur)
}
fmt.Printf("Total time: %v\n", time.Since(start))
}