Skip to content

Instantly share code, notes, and snippets.

@darkhelmet
Created June 18, 2011 03:06
Show Gist options
  • Save darkhelmet/1032762 to your computer and use it in GitHub Desktop.
Save darkhelmet/1032762 to your computer and use it in GitHub Desktop.
func Download(out chan string, urls... string) {
for url := range(urls) {
go func() {
out <- HttpGet(url)
}()
}
}
func Process(in chan string) {
select {
case html := <- in:
// Do something with html
case <- time.After(10 * 1e9)
// Timed out after 10 seconds!
}
}
func DoStuff() {
// Make a channel
pipe := make(chan string, 10)
// Kick off goroutines to download the URLs
Download(pipe, "http://blog.darkhax.com/2011/05/06/simple-ruby-pipes", "http://blog.darkhax.com/2011/03/28/rubber-duck-debugging")
// Process them, with a timeout
Process(pipe)
}
@mjs
Copy link

mjs commented Jun 23, 2014

Thanks for the blog article that goes with this. Very clear.

I fixed a missing colon and made the example slightly more readable here: https://gist.github.com/mjs/38a2c7cba406792a7143

The changes are for line 13. Feel free to integrate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment