Mahdi.Kh
September 21, 2026
We look at the time standard package and get familiar with the parts of it that are used most often.
The time package in Go provides capabilities for measuring and displaying time. Calendar calculations are always done using the Gregorian calendar, and no leap seconds are taken into account.
Operating systems usually provide two kinds of clocks:
The wall clock shows the real-world time and is what we normally see on an operating system or a mobile phone. This kind of clock can change due to time synchronization — for example, being moved an hour forward or back for winter and summer time, or because we change it ourselves.
The monotonic clock acts like an internal stopwatch for the operating system: it increases steadily and only ever moves forward. It can be used to calculate how much time has elapsed.
In Go, if we get the time from the standard package, the value we receive will contain the time according to both the wall clock and the monotonic clock:
The output of the code above will look something like this:
The m=+0.000123456 value is the monotonic clock reading. The monotonic clock is usually used to calculate the difference between times.
When will the returned value not contain a monotonic clock reading?
Date, Parse, or Unix functions, the returned value will not contain a monotonic clock reading.UTC function), the monotonic clock reading is stripped from the returned value.Take a look at the example below:
In the output of the code above, the m= value — which represents the monotonic clock — is shown only on the first line; it won't be present for the other values.
When the monotonic clock reading isn't present in a given value, Go falls back to the wall clock to calculate the time difference. In that case, if the operating system's clock changes, that change will affect the calculated value.
For example, if we move the operating system's clock two hours forward or back, the calculated time difference will be two hours more or less.
There are two ways to compare times for equality in Go:
==, if the monotonic clock mattersEqual, if only the wall clock matters.In Go, two times (time.Time) are equal to each other if the following conditions hold:
If at least one of them has a monotonic clock reading, their monotonic clock readings must also be equal. (If neither has a monotonic clock reading, the monotonic clock comparison is skipped.)
In Go, for comparing whether a time is greater or less than another — similar to the equality comparison (==) — you can use one of the following approaches:
>= and <=.Before and After methods.In Go, the time.Duration type is used to represent time spans, and the result of calculating the difference between two times will be a time.Duration.
To calculate the time difference between two time.Time variables, you can use the Sub method:
If we want to calculate the time elapsed between now and a time in the past, we can use the Since method as follows:
The time package has several different ways to do scheduled execution. Each one is designed for a specific scenario.
time.SleepThe simplest way to delay code execution is to use time.Sleep.
time.AfterOne way to send a scheduled signal on channels is to use the time.After function. This function returns a channel onto which a value is written once the specified duration (time.Duration) has elapsed.
A timer can be used for controlled signal delivery. A timer lets us change how much time has passed or remains before the signal is sent, or even prevent the signal from being sent at all. You can create a timer using the time.NewTimer function.
You can stop a timer using the Stop method, and reset its value using the Reset method. In the example above, we first set the timer to 1 second, and then, after 200 milliseconds, reset it to 2 seconds.
time.AfterFuncYou can use time.AfterFunc to run a function after a specified duration.
time.TickUsing time.Tick, we can periodically send a value on a channel at a fixed interval.
Note: Tick cannot be stopped, and it's suited to cases where you need to do something repeatedly for the entire lifetime of the program.
In the example above, we used break to exit the loop, but the Ticker held in the tick variable will keep putting values on its internal channel and won't stop!
time.NewTicker to Create a Controllable Recurring ScheduleYou can create a controllable Ticker using the time.NewTicker function. A Ticker is simply a struct with a C field of channel type, plus Stop and Reset methods.
The Stop method stops the Ticker, and the Reset method can be used to change the Ticker's schedule.
Previous part: Unit Testing | Golang basics
Next part: The sync Standard Package | Golang basics