Skip to content

Instantly share code, notes, and snippets.

@rickt
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rickt/06192d2037ef788d89d6 to your computer and use it in GitHub Desktop.
Save rickt/06192d2037ef788d89d6 to your computer and use it in GitHub Desktop.
example of how to do asynchronous http gets
package main
import (
"fmt"
"net/http"
"time"
)
var urls = []string{
"http://rickt.org",
"http://code.rickt.org",
"http://golang.org",
"http://regencydandy.com",
}
type HttpResponse struct {
url string
response *http.Response
err error
}
func asyncHttpGets(urls []string) []*HttpResponse {
ch := make(chan *HttpResponse)
responses := []*HttpResponse{}
for _, url := range urls {
go func(url string) {
fmt.Printf("Fetching %s \n", url)
resp, err := http.Get(url)
ch <- &HttpResponse{url, resp, err}
}(url)
}
for {
select {
case r := <-ch:
fmt.Printf("%s was fetched\n", r.url)
responses = append(responses, r)
if len(responses) == len(urls) {
return responses
}
case <-time.After(50 * time.Millisecond):
// uncomment below Printf if you'd like a "." to show every 50ms delay
// fmt.Printf(".")
}
}
return responses
}
func main() {
results := asyncHttpGets(urls)
for _, result := range 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