* Henrik Johansson <dahankz...@gmail.com> [171128 07:43]:
> But wouldn't unmarshal just overwrite in case of more trivial keys?
> 
> So pointer receivers on the marshaling methods is better.
> I think I tried it but something else blew up.

While MarshalText can use a value or pointer receiver, it makes no sense
to use a value receiver with UnmarshalText.  The UnmarshalText method
must be able to change the caller's copy of the variable that is to hold
the unmarshalled data.

As for time.Time, if you read its documentation, it says that it holds
both a wall clock time and a monotonic time.  time.Now() returns a
structure that has both values, but some operations (e.g. time.Parse and
time.Time.UnmarshalJSON) return a time.Time that only has a wall clock
time.  t.Round(0) is the canonical way to strip the monotonic time from
a time.Time value t.

So after t := time.Now(); t2 := t.Round(0), t and t2 represent the same
wall clock time, but compare as different, because the t has both wall
clock and monotonic times, whereas t2 only has wall clock time.

So your map with key time.Now() has a key with both times.  When you
marshal it, the marshalled value only has wall clock time.  When you
unmarshal it back to the same map, the unmarshalled time is different
(but represents the same wall clock time) from the existing map key, so
it creates an additional map element with the new key.

If you create your map with keys that only have wall clock times with
UTC location, as in https://play.golang.org/p/BCB3TAZADB, the
unmarshalled key will match the existing key and overwrite it.  If you
remove either .UTC() or .Round(0) or both from that code, you will
notice that the map keys are different, and you end up with two values
in the map after unmarshalling.

...Marvin

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to