Created
May 15, 2014 15:50
-
-
Save zgiber/ee5eeeea07a63d230aaa to your computer and use it in GitHub Desktop.
Golang timeout goroutine with channels
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
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