Introduction
In this post, we are going to explore what is the zero value for time.Time
in Golang. As introduced in some articles before, package time provides functionality for measuring and displaying time. The calendrical calculations always assume a Gregorian calendar, with no leap seconds.
type Time
: A Time represents an instant in time with nanosecond precision. The zero value of type Time is January 1, year 1, 00:00:00.000000000 UTC. As this time is unlikely to come up in practice, the IsZero method gives a simple way of detecting a time that has not been initialized explicitly.
In Golang, variables that are declared without a specific value are assigned a zero value:
-
0
for integer types,0.0
 for floating point numbers,false
 for booleans,
""
 for strings,nil
 for interfaces, slices, channels, maps, pointers, and functions.
Example 1: Zero value for basic data types in Golang
In the example below, we will initialize some variables with their zero values:
package main
import "fmt"
func main() {
// variables f different types
var var1 int
var var2 float64
var var3 bool
var var4 string
var var5 []int
var var6 *int
var var7 map[int]string
// the zero value of the above variables
fmt.Println("The zero value for integer types: ", var1)
fmt.Println("The zero value for float64 types: ", var2)
fmt.Println("The zero value for boolean types: ", var3)
fmt.Println("The zero value for string types: ", var4)
fmt.Println("The zero value for slice types: ", var5)
fmt.Println("The zero value for pointer types: ", var6)
fmt.Println("the zero value for map types: ", var7)
}
Output:
The zero value for integer types: 0
The zero value for float64 types: 0
The zero value for boolean types: false
The zero value for string types:
The zero value for slice types: []
The zero value for pointer types: <nil>
the zero value for map types: map[]
Example 2: Zero value for time.Time in Golang
As per the official documentation of Go: The zero value of type Time is January 1, year 1, 00:00:00.000000000 UTC. To create and print the Zero value for time.Time
we will initialize an empty time.Time
struct literal, it will return Go's Zero date. The code below shows how to get the zero value of the time.Time
in Golang:
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println(time.Time{})
}
Output:
0001-01-01 00:00:00 +0000 UTC
Example 3: Another way to get the zero value for time.Time
In this example, we will create an empty time.Time
variable and print out its value:
package main
import (
"fmt"
"time"
)
func main() {
t := new(time.Time)
fmt.Println("This is the zero value of time.Time:", t)
}
Output:
This is the zero value of time.Time: 0001-01-01 00:00:00 +0000 UTC
Example 4: Check if a time.Time is zero or not
To check if a time.Time
value is zero or not, we can use the isZero()
function. In example 4, we will create 2 time.Time
variables and test if they are zero or not:
func (t Time) IsZero() bool
: IsZero reports whether t represents the zero time instant, January 1, year 1, 00:00:00 UTC.
package main
import (
"fmt"
"time"
)
func main() {
t1 := new(time.Time)
t2 := time.Now()
fmt.Println("t1 is:", t1)
fmt.Println("t2 is:", t2)
fmt.Println("t1 is zero?", t1.IsZero())
fmt.Println("t2 is zero?", t2.IsZero())
}
Output:
t1 is: 0001-01-01 00:00:00 +0000 UTC
t2 is: 2022-12-24 20:25:32.0710816 +0700 +07 m=+0.001072501
t1 is zero? true
t2 is zero? false
Summary
In this tutorial, you may already have stumbled upon the concept of zero values in Go. The zero value of type Time is January 1, year 1, 00:00:00.000000000 UTC. The IsZero()
method returns whether a time has a zero value or not.
References
https://pkg.go.dev/time
What is the `zero` value for time.Time in Go? - Stack Overflow