Skip to content

Instantly share code, notes, and snippets.

@zgiber
Created May 15, 2014 15:50
Show Gist options
  • Save zgiber/ee5eeeea07a63d230aaa to your computer and use it in GitHub Desktop.
Save zgiber/ee5eeeea07a63d230aaa to your computer and use it in GitHub Desktop.
Golang timeout goroutine with channels
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println("Hello, playground")
receiverTimeout := time.Duration(5 * time.Second)
timeoutExpired := make(chan bool)
resetTimeout := make(chan bool)
startTimeout := func() {
for {
select {
case <-resetTimeout:
continue
case <-time.After(receiverTimeout):
timeoutExpired <- true
}
}
}
go startTimeout()
select {
case <-timeoutExpired:
fmt.Println("timeout")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment