Time
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:
time.Ticker
:Example:
time.NewTimer()
:Example:
Using with select
:
Example:
Last updated