From 18e7d48e42dfbdad2a9e363de2bfb5cfd7021aaa Mon Sep 17 00:00:00 2001 From: jimi Date: Fri, 5 Feb 2016 17:56:21 -0600 Subject: [PATCH] Initial commit... getPage function queries the url and return the body length main function walks through a map of URLs sending each to getPage --- main.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 main.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..e7dd714 --- /dev/null +++ b/main.go @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" + "io/ioutil" + "net/http" +) + +func getPage(url string) (int, error) { + + resp, err := http.Get(url) + if err != nil { + return 0, err + } + + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return 0, err + } + + return len(body), nil +} + +func main() { + sites := map[string]string{ + "Google": "http://google.com", + "Yahoo": "http://yahoo.com", + "Bing": "http://bing.com", + } + + for name, url := range sites { + length, err := getPage(url) + if err != nil { + fmt.Printf("%s %s\n", name, err) + } + fmt.Printf("%s %d\n", name, length) + } +}