Menu Close

Go – time package in go

In this tutorial, We are going to learn about time package in go. We will also see how to use time package in program with example.

It provides functionality for measuring and displaying time.

How to import time package in program:

import “time”

A computer has two different clocks:

  • Wall clock
  • Monotonic clock

Monotonic and Wall clock

Let’s talk about wall clock first. Everyone knows about the wall clock. It is used to get the current time of the day.

This clock time may have variations, reason being Suppose If it is synchronised with NTP (Network Time Protocol) then after synchronisation, the local clock of our server can jump either backward or forward in time. Hence, measuring a duration from the wall clock can be biased.

In monotonic clock, time always moves forward. if we have to measure durations, we must use the monotonic-clock.

That’s why as a software developer, to measure the duration (time interval) of an execution of code, we use monotonic clock.

monotonic clock reading has no meaning outside the current process.

time.Now() returns monotonic clock reading in go.


start_time := time.Now()

… operation that takes around 10 milliseconds …

end_time := time.Now()

elapsed := end_time.Sub(start)


package main

import (



func main() {

  start_time := time.Now()


  end_time := time.Now()

  elapsed := end_time.Sub(start_time)

  fmt.Println("Elapsed time:  ", elapsed)


Elapsed time: 18.813µs

time package has many functions which are given below :

To learn more about golang, Please refer given below link.

Posted in golang, packages, time

Leave a Reply

Your email address will not be published. Required fields are marked *