Overview:
- The
time
package in Go provides functionality for working with time-related operations, such as getting the current time, sleeping, timers, and tickers. - It offers structs like
Time
,Duration
, and functions likeNow()
,Sleep()
,After()
,Ticker()
, andNewTimer()
for various time-related tasks.
Usage of time.After()
, time.Ticker
, and time.NewTimer()
:
-
time.After()
:time.After(duration)
returns a channel that receives the current time after the specified duration.- It's commonly used to create a timeout mechanism or to schedule an action to occur after a specific duration.
- It's a non-blocking operation.
Example:
select { case <-time.After(1 * time.Second): fmt.Println("Timeout!") }
-
time.Ticker
:Example:
ticker := time.NewTicker(1 * time.Second) defer ticker.Stop() for { select { case <-ticker.C: fmt.Println("Tick") } }
-
time.NewTimer()
:Example:
timer := time.NewTimer(2 * time.Second) defer timer.Stop() select { case <-timer.C: fmt.Println("Timer expired") }
Using with select
:
Example:
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
timer := time.NewTimer(5 * time.Second)
defer timer.Stop()
for {
select {
case <-ticker.C:
fmt.Println("Tick")
case <-timer.C:
fmt.Println("Timer expired")
return
}
}