Created
March 13, 2013 01:49
-
-
Save physacco/5148770 to your computer and use it in GitHub Desktop.
This code shows how to add a timeout to your function in go.
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
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
func work(ch chan int) { | |
time.Sleep(5000 * time.Millisecond) | |
ch <- 12345 | |
} | |
func main() { | |
c := make(chan int, 1) | |
go work(c) | |
select { | |
case res := <-c: | |
fmt.Println("result:", res) | |
case <-time.After(1000 * time.Millisecond): | |
fmt.Println("error: timeout") | |
} | |
} | |
// Refer to: http://code.google.com/p/go-wiki/wiki/Timeouts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment