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.

56 lines
855 B

package main
import (
"fmt"
"io/ioutil"
"net/http"
"time"
)
type Data struct {
length int
duration time.Duration
}
func getPage(url string) (Data, error) {
var d Data
resp, err := http.Get(url)
if err != nil {
return d, err
}
defer resp.Body.Close()
start := time.Now()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return d, err
}
dur := time.Since(start)
d.length = len(body)
d.duration = dur
return d, 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 {
data, err := getPage(url)
if err != nil {
fmt.Printf("%s %s\n", name, err)
}
fmt.Printf("%s %d %v\n", name, data.length, data.duration)
}
fmt.Printf("Total time: %v\n", time.Since(start))
}