#time.Time vs null

5 messages · Page 1 of 1 (latest)

flint fjord
#

Assume I have a struct with updatedAt time.Time, which has zero value until actual update is in place. How is this usually solved in Go?

Saving this to a database doesn't cause problems, but reading does - I managed to not use sql.NullTime by not making the field nullable in the db but save 0000-00-00 00:00:00 instead.
Another gimmick appears when marshaling (encoding) to JSON - by default it will return 0001-01-01 something (time.Time's zero value equivalent). Is there any tag that inserts null on zero value, or I do have to create my own type or marshal method and handle that manually?

narrow pewter
flint fjord
#

I wonder if my main struct should have *time.Time that can be nil instead of zero value, or time.Time and have consumer check for zero value

narrow pewter
#

you can also use the pointer variant

you can easily be getting into nil pointer exceptions though if you dont check against it everytime you try to access it

flint fjord
#

So, is there preferred way?