Search code examples
gomgo

Reset time.Time in mgo struct


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?


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.