Avatar ·

What is the zero value for time.Time in Go and how to check if a time variable is empty?

Hello everyone! I just started learning Go and encountered an unclear situation when working with the time package.

\n\n

I have a struct where one of the fields is a date and time. Sometimes this field may not be set (null from other languages). In PHP or JavaScript I would just check for null, but Go doesn't have that luxury. I tried using nil, but the compiler gave an error.

\n\n

Here is an example of my code:

\n\n
package main\n\nimport (\n    "fmt"\n    "time"\n)\n\ntype Event struct {\n    Name      string\n    CreatedAt time.Time\n    UpdatedAt time.Time // may not be set\n}\n\nfunc main() {\n    e := Event{\n        Name: "Test event",\n        // UpdatedAt not set\n    }\n    \n    // Trying to check if UpdatedAt is set