Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

null

time

go

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?

like image 620
Mingyu Avatar asked Apr 14 '14 04:04

Mingyu


People also ask

What is the zero value of time time?

As per the official documentation of Go: The zero value of type Time is January 1, year 1, 00:00:00.000000000 UTC.

How do you get the time in go?

Now() function, we can get the current time in Golang by importing time module. Return: Return current date and time. Example #1: In this example, we can see that by using time. Now() function, we are able to get the current date and time.

What does time duration do in Golang?

Duration has a base type int64. Duration represents the elapsed time between two instants as an int64 nanosecond count”. The maximum possible nanosecond representation is up to 290 years.

What is the initial value zero value for slice and map?

nil for interfaces, slices, channels, maps, pointers and functions.


1 Answers

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

func (Time) IsZero  func (t Time) IsZero() bool IsZero reports whether t represents the zero time instant, January 1, year 1, 00:00:00 UTC. 
like image 168
gextra Avatar answered Sep 17 '22 14:09

gextra