Created
April 11, 2017 15:47
-
-
Save haibin/f46499a47f5addddfa8ce49d8842ddd8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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