How to Check If a Time Value is the Zero Value in Golang

To check if a time value is the zero value in Go, you can use the IsZero() of the time.Time type.

package main

import (
  "fmt"
  "time"
)

func main() {
  var zeroTime time.Time
  fmt.Println("Is zero time:", zeroTime.IsZero())

  currentTime := time.Now()
  fmt.Println("Is current time zero:", currentTime.IsZero())
}

Output

Is zero time: true
Is current time zero: false

We declared a time.Time variable called zeroTime without initializing it, which results in the zero value for the time.Time type.

Then, we used the IsZero() method to check if both zeroTime and currentTime (the current time obtained using time.Now() are the zero time.

That’s it.