Created
August 26, 2016 04:05
-
-
Save kratorado/e20f944708a45c4f1a4fc64a0ab2e94e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
func WaitGroupDemo() { | |
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. | |
http.Get(url) | |
}(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