The Go Time package library provides functionality for working with dates and times. One of its methods is Time.After, which returns a channel that will receive the current time after the specified duration has elapsed.
This example creates a new timer with a duration of 2 seconds using time.Second * 2. It then prints a message indicating that it is waiting for 2 seconds. After 2 seconds, the timer will expire and the message Timeout reached! will be printed.
for { select { case <-ticker.C: fmt.Println("Tick") case <-timeout: fmt.Println("Timeout reached!") return } } }
This example creates a new ticker that emits a tick every second using time.NewTicker(time.Second). It then creates a timer that will expire after 5 seconds using time.Second * 5. It then enters an infinite loop with a select statement that listens for either ticks from the ticker or timeout from the timer. When the timeout is reached, the program will exit with the message Timeout reached!.
Both examples use the time package library provided by Go.
Golang Time.After - 30 examples found. These are the top rated real world Golang examples of Time.Time.After extracted from open source projects. You can rate examples to help us improve the quality of examples.