-
-
Save lujjjh/8f9c4b257654465ed2585dcd4d193a29 to your computer and use it in GitHub Desktop.
Go timeout
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 "time" | |
func main() { | |
timeout := time.Second | |
t := time.NewTimer(timeout) | |
for { | |
select { | |
case <-t.C: // <- A | |
// timed out | |
case <-doSomethingElse(): | |
if !t.Stop() { // <- B | |
<-t.C // <- C | |
} | |
} | |
t.Reset(timeout) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment