func MakeRequest(url string, ch chan<- string) {
start := time.Now()
resp, err := http.Get(url)
if err != nil {
ch <- fmt.Sprintf(err.Error())
}
secs := time.Since(start).Seconds()
body := resp.StatusCode
defer resp.Body.Close()
ch <- fmt.Sprintf("%.2f elapsed with response status code: %d %s", secs, body, url)
}
func main() {
start := time.Now()
ch := make(chan string)
urls := []string{
"https://www.vox.com/",
"https://www.nbcnews.com/",
"https://www.vox.com/",
"https://www.nbcnews.com/",
"https://www.vox.com/",
"https://www.nbcnews.com/",
"https://www.vox.com/",
"https://www.nbcnews.com/",
"https://www.vox.com/",
"https://www.nbcnews.com/",
"https://www.vox.com/",
"https://www.nbcnews.com/",
"https://www.vox.com/",
"https://www.nbcnews.com/",
"https://www.vox.com/",
"https://www.nbcnews.com/",
"https://www.vox.com/",
"https://www.nbcnews.com/",
"https://www.vox.com/",
"https://www.nbcnews.com/",
"https://www.vox.com/",
"https://www.nbcnews.com/",
"https://www.vox.com/",
"https://www.nbcnews.com/",
"https://www.vox.com/",
"https://www.nbcnews.com/",
"https://www.vox.com/",
"https://www.nbcnews.com/",
"https://www.vox.com/",
"https://www.nbcnews.com/",
}
for _, url := range urls {
go MakeRequest(url, ch)
}
for range urls {
fmt.Println(<-ch)
}
fmt.Printf("Was made %v requests\n", len(urls))
fmt.Printf("%.2fs elapsed\n", time.Since(start).Seconds())
}