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.

39 lines
598 B

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http"
  6. )
  7. func getPage(url string) (int, error) {
  8. resp, err := http.Get(url)
  9. if err != nil {
  10. return 0, err
  11. }
  12. defer resp.Body.Close()
  13. body, err := ioutil.ReadAll(resp.Body)
  14. if err != nil {
  15. return 0, err
  16. }
  17. return len(body), nil
  18. }
  19. func main() {
  20. sites := map[string]string{
  21. "Google": "http://google.com",
  22. "Yahoo": "http://yahoo.com",
  23. "Bing": "http://bing.com",
  24. }
  25. for name, url := range sites {
  26. length, err := getPage(url)
  27. if err != nil {
  28. fmt.Printf("%s %s\n", name, err)
  29. }
  30. fmt.Printf("%s %d\n", name, length)
  31. }
  32. }