Created
June 18, 2011 03:06
-
-
Save darkhelmet/1032762 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
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.