Skip to content

Instantly share code, notes, and snippets.

@mjs
Forked from darkhelmet/timeout.go
Last active August 29, 2015 14:02
Show Gist options
  • Save mjs/38a2c7cba406792a7143 to your computer and use it in GitHub Desktop.
Save mjs/38a2c7cba406792a7143 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 * time.Second):
// Timed out!
}
}
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