Time
time.Time, time.Now, Duration arithmetic, time.Since and time.Until, Add and Sub, and comparisons with Before, After, and Equal.
The time package represents a moment with time.Time and a length of time with time.Duration. Use Year, Month and Day to read calendar parts. Durations are stored in nanoseconds, but constants such as time.Second make them easier to read.
Use Add to move an instant by a duration, Sub to measure the duration between instants, and Before, After, or Equal to compare instants.
package main
import (
"fmt"
"time"
)
func main() {
start := time.Now()
deadline := start.Add(5 * time.Second)
fmt.Println(time.Until(deadline) > 0)
time.Sleep(10 * time.Millisecond)
fmt.Println("elapsed:", time.Since(start))
fmt.Println(start.Before(deadline))
}In production
Use time.Since or time.Until to measure elapsed time. Go uses a monotonic clock when available, a clock that only moves forward, so system clock changes do not affect the measurement. Use Time.Equal to compare moments because two values can describe the same moment while storing different location details.