Created
July 29, 2014 20:12
Revisions
-
pranjal5215 created this gist
Jul 29, 2014 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,57 @@ package main import ( "fmt" "net" "net/http" "time" ) var urls = []string{ "http://pulsoconf.co/", "http://golang.org/", "http://matt.aimonetti.net/", } var timeout = time.Duration(5e7) type HttpResponse struct { url string response *http.Response err error } func dialTimeout(network, addr string) (net.Conn, error) { return net.DialTimeout(network, addr, timeout) } func asyncHttpGets(urls []string) <-chan *HttpResponse { ch := make(chan *HttpResponse, len(urls)) // buffered transport := http.Transport{ Dial: dialTimeout, } client := http.Client{ Transport: &transport, } for _, url := range urls { go func(url string) { fmt.Printf("Fetching %s \n", url) resp, err := client.Get(url) if err != nil { ch <- &HttpResponse{url, nil, err} } defer resp.Body.Close() ch <- &HttpResponse{url, resp, err} }(url) } return ch } func main() { ch := asyncHttpGets(urls) for _ = range urls { x := <-ch fmt.Printf("%s status: %s\n", x.url, x.response.Status) } }