Checking for uninitialised dates in Go

In a recent Go project where I’ve used an object-relational mapper (GORM), I needed to whether a time.Time (mapped to a potential NULL database field) had been initialized of not.

Go’s initialisation value for time.Time is 0001-01-01 00:00:00 +0000 UTC (1st January 2001). It turns out time.Time provides a convenient method (IsZero()) to do just this.

import "time"

 var myDate time.Time

if myDate.IsZero() {
    // not initialised
} else {
    // initialised
}