Skip to content

Instantly share code, notes, and snippets.

@igorzakhar
Forked from ismasan/gist:3804361
Created January 15, 2018 15:29
Show Gist options
  • Save igorzakhar/fe79541bb5e430d300f1a1fb296cbfc4 to your computer and use it in GitHub Desktop.
Save igorzakhar/fe79541bb5e430d300f1a1fb296cbfc4 to your computer and use it in GitHub Desktop.
async fetching of urls using goroutines and channels
package main
import (
"fmt"
"net/http"
"time"
)
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) []*HttpResponse {
ch := make(chan *HttpResponse, len(urls)) // buffered
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
}
default:
fmt.Printf(".")
time.Sleep(5e7)
}
}
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