Question

Simplified struct:

type User struct {
    ResetToken     string        `bson:"resettoken,omitempty" json:"resettoken"`
    ResetSent      time.Time     `bson:"resetsent,omitempty" json:"resetsent"`
}

Now on successful (password) reset it should set ResetToken = "" and set ResetSent to "uninitialized" aka 0 or initial value or empty, you name it.

In the case of string it's done with "" and ",omitempty" but how do I do with with time.Time?

Was it helpful?

Solution

The time zero is time.Time{}, and you can check that it's zero with time.IsZero(t). So, something like:

user.ResetSent = time.Time{}

If you need it to really omitempty, you could use a *time.Time, so it will leave it empty if nil.

Update: Gustavo's comment is correct, omitempty works as intended for zero-valued time, without a pointer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top