Skip to content

Instantly share code, notes, and snippets.

@lujjjh
Created August 8, 2021 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lujjjh/8f9c4b257654465ed2585dcd4d193a29 to your computer and use it in GitHub Desktop.
Save lujjjh/8f9c4b257654465ed2585dcd4d193a29 to your computer and use it in GitHub Desktop.
Go timeout
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