Skip to content

Instantly share code, notes, and snippets.

@physacco
Created March 13, 2013 01:49
Show Gist options
  • Save physacco/5148770 to your computer and use it in GitHub Desktop.
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.
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