Skip to content

Instantly share code, notes, and snippets.

@haibin
Created April 11, 2017 15:47
Show Gist options
  • Save haibin/f46499a47f5addddfa8ce49d8842ddd8 to your computer and use it in GitHub Desktop.
Save haibin/f46499a47f5addddfa8ce49d8842ddd8 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
"sync"
)
func main() {
var wg sync.WaitGroup
var urls = []string{
"http://www.golang.org/",
"http://www.google.com/",
"http://www.somestupidname.com/",
}
for _, url := range urls {
// Increment the WaitGroup counter.
wg.Add(1)
// Launch a goroutine to fetch the URL.
go func(url string) {
// Decrement the counter when the goroutine completes.
defer wg.Done()
// Fetch the URL.
resp, err := http.Get(url)
if err != nil {
fmt.Println(err.Error())
return
}
defer resp.Body.Close()
fmt.Println(url, resp.StatusCode)
}(url)
}
// Wait for all HTTP fetches to complete.
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment