A common way to use timer in the go language

  • 2020-05-26 09:20:18
  • OfStack

This example summarizes the common ways to use timer in the go language. Share with you for your reference. The specific analysis is as follows:

The following three pieces of code (A,b,C) are the go language code that executes the specified function after 5 minutes:

// (A)
time.AfterFunc(5 * time.Minute, func() {
    fmt.Printf("expired")
}
// (B) create a Timer object
timer := time.NewTimer(5 * time.Minute)
<-timer.C
fmt.Printf("expired")
// (C) time.After() returns timer.C internally
<-time.After(5 * time.Minute)
fmt.Printf("expired")

I hope this article has been helpful to your programming of Go language.


Related articles: