Skip to content

Instantly share code, notes, and snippets.

@maniankara
Created February 19, 2017 13:51
Show Gist options
  • Save maniankara/e46060bb13b1e03607d3888843e64f10 to your computer and use it in GitHub Desktop.
Save maniankara/e46060bb13b1e03607d3888843e64f10 to your computer and use it in GitHub Desktop.
Executing go routines inside for loop with Boolean channels
package fragments
func downloadUrls(urls []string) {
done := make(chan bool, len(urls))
for _, url := range urls {
go func(url string) {
// perform download here
done <- true
}(url)
}
// ensure all the routines are done
for i:=0; i<len(urls); i++ { <- done }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment