Looks like I have the same issue as Jonathan. Just thinking about this, if Marshal omits &int(0), then shouldn't Unmarshall create a new &int(0) when the tag is omitted rather than nil? (Yes &int(0) isn't legal but I don't have the correct jargon to better describe my thoughts).
Anyway, my problem is that I'm creating an online availability engine (for motels), and I'm using as struct similar to:
type Update struct {
Date time.Time
RoomType string
Availability *int `xml:",omitempty"`
Rate *float32 `xml:",omitempty"`
...lots of other optional fields
}
Now, the tags "Availability" and "Rate" etc are optional. From what Jonathan describes, I either must always set availability (which may not exist in that update - and risk making the room unavailable), OR be unable to make the room unavailable when setting the value to be zero.
I, like Jonathan, thought using an *int instead of an int, would mean the xml package would only check the value, see that the pointer was not nil and display. I didn't expect it to look deeper... I mean, the field value itself isn't empty...
I was just about to suggest I might get around it with the XMLMarshal interface, but that only works with JSON.
I'd really rather not have to add a special unavailable tag, as it's not streamlined and will require more work for anyone interfacing my system.
Thoughts? Thanks.