Browse Source

Use a struct to pass values, rather than vars

master
jimi 9 years ago
parent
commit
691110d949
  1. 25
      main.go

25
main.go

@ -7,21 +7,32 @@ import (
"time"
)
func getPage(url string) (int, time.Duration, error) {
type Data struct {
length int
duration time.Duration
}
func getPage(url string) (Data, error) {
var d Data
start := time.Now()
resp, err := http.Get(url)
if err != nil {
return 0, 0, err
return d, err
}
defer resp.Body.Close()
start := time.Now()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, 0, err
return d, err
}
dur := time.Since(start)
d.length = len(body)
d.duration = dur
return len(body), time.Since(start), nil
return d, nil
}
func main() {
@ -34,11 +45,11 @@ func main() {
start := time.Now()
for name, url := range sites {
length, dur, err := getPage(url)
data, err := getPage(url)
if err != nil {
fmt.Printf("%s %s\n", name, err)
}
fmt.Printf("%s %d %v\n", name, length, dur)
fmt.Printf("%s %d %v\n", name, data.length, data.duration)
}
fmt.Printf("Total time: %v\n", time.Since(start))

Loading…
Cancel
Save