From 691110d949dad36f5a628a1d15238f1abdd13847 Mon Sep 17 00:00:00 2001 From: jimi Date: Mon, 8 Feb 2016 08:28:25 -0600 Subject: [PATCH] Use a struct to pass values, rather than vars --- main.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index d2e81d4..1c2d5bb 100644 --- a/main.go +++ b/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))