What is the `zero` value for time.Time in Go?

TimeGoNull

Time Problem Overview


In an error condition, I tried to return nil, which throws the error:

cannot use nil as type time.Time in return argument

What is the zero value for time.Time?

Time Solutions


Solution 1 - Time

You should use the Time.IsZero() function instead:

func (Time) IsZero

or

func (t Time) IsZero() bool

> IsZero reports whether t represents the zero time instant, January 1, year 1, 00:00:00 UTC.

Solution 2 - Time

Invoking an empty time.Time struct literal will return Go's zero date. Thus, for the following print statement:

fmt.Println(time.Time{})

The output is:

0001-01-01 00:00:00 +0000 UTC

For the sake of completeness, the official documentation explicitly states:

> The zero value of type Time is January 1, year 1, 00:00:00.000000000 UTC.

Solution 3 - Time

The zero value for time.Time is 0001-01-01 00:00:00 +0000 UTC See http://play.golang.org/p/vTidOlmb9P

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMingyuView Question on Stackoverflow
Solution 1 - TimegextraView Answer on Stackoverflow
Solution 2 - TimezeantsoiView Answer on Stackoverflow
Solution 3 - Timedethtron5000View Answer on Stackoverflow