Created
November 21, 2013 05:24
-
-
Save meteorfox/7576483 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
) | |
var urls = []string{ | |
"http://pulsoconf.co/", | |
"http://golang.org/", | |
"http://matt.aimonetti.net/", | |
} | |
type HttpResponse struct { | |
url string | |
response *http.Response | |
err error | |
} | |
func asyncHttpGets(urls []string) <-chan *HttpResponse { | |
ch := make(chan *HttpResponse, len(urls)) // buffered | |
for _, url := range urls { | |
go func(url string) { | |
fmt.Printf("Fetching %s \n", url) | |
resp, err := http.Get(url) | |
resp.Body.Close() | |
ch <- &HttpResponse{url, resp, err} | |
}(url) | |
} | |
return ch | |
} | |
func main() { | |
results := asyncHttpGets(urls) | |
for _ = range urls { | |
result := <-results | |
fmt.Printf("%s status: %s\n", result.url, result.response.Status) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment