This question already has an answer here:
According to my requirements, I created one struct as -
type MyRule struct {
CreatedAt time.Time `json:"createdAt" datastore:"createdAt,noindex"`
UpdatedAt *time.Time `json:"updatedAt" datastore:"updatedAt,noindex"`
}
for createdAt field, I am able to store current time as-
MyRule.CreatedAt = time.Now()
However, the same thing does not work to store current time in updatedAt
field of MyRule
struct as it's type is *time.Time
and not time.Time
. Here, I can't change field type of updatedAt
because *time.Time
allows me to accept nil as updatedAt
value when I create any rule.
If I try to do this as-
MyRule.UpdatedAt = time.Now()
It gives me below error-
cannot use time.Now()(type time.Time) as type *time.Time in assignment
How can I store current time value in updatedAt field of type *time.Time and not time.Time
</div>
Note: one cannot get the address of a return value, so something like this will NOT work:
MyRule.UpdatedAt = &time.Now() // compile fail
To get the address of a value, it must be in an addressable item. So assign the value to a variable, like so:
t := time.Now()
MyRule.UpdatedAt = &t