Skip to content

Instantly share code, notes, and snippets.

@emailnjv
Created April 15, 2018 03:34
Show Gist options
  • Save emailnjv/2f93c97a525235348dc293796ea2c711 to your computer and use it in GitHub Desktop.
Save emailnjv/2f93c97a525235348dc293796ea2c711 to your computer and use it in GitHub Desktop.
Concurrent HTTP GET Response generator
// RespGen :: Concurrent HTTP GET Response generator
func RespGen(urls ...string) <-chan *http.Response {
var wg sync.WaitGroup
out := make(chan *http.Response)
wg.Add(len(urls))
for _, url := range urls {
go func(url string) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
panic(err)
}
req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0")
time.Sleep(3 * time.Second)
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
out <- resp
wg.Done()
}(url)
}
go func() {
wg.Wait()
close(out)
}()
return out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment