I'm trying to use a time.Time
structure in a structure that will be encoded/decoded with JSON. The time.Time
attributes should not be included if they aren't set (omitempty tag), so to be able to do so I will have to use a pointer to the time.Time
object.
I have defined a type for the time.Time
structure so I easily can create receiver functions format the time when the JSON is encoded and decoded etc.
See the code here: https://play.golang.org/p/e81xzA-dzz
So in my main structure (the structure that actually will be encoded) I will do something like this:
type EncodeThis struct {
Str string `json:"str,omitempty"`
Date *jWDate `json:"date,omitempty"`
}
The problem is that the pointer may be nil, when trying to decode the value, so if you look at my code at the Go playground, you can see that I'm trying to (using double pointers) to set the address of the receiver if its nil. See method "Set" and "swap".
But, this doesnt seem to work. The program doesn't fail or anything, but the "EncodeThis" struct will not contain a reference to this new address. Any idea for a fix for this?