Consider these 2 XML documents
<a>
<b nil="true"></b>
</a>
and
<a>
<b type="integer">1</b>
</a>
How can I unmarshal this XML properly in Go to a b
struct field of type int
, without producing a strconv.ParseInt: parsing "": invalid syntax
error in the first case?
omitempty
doesn't seem to work in this case.
Example: http://play.golang.org/p/fbhVJ4zUbl
The omitempty tag is just respected with Marshal, not Unmarshal.
Unmarshal errors if the int value is not an actual int.
Instead, change B to a string. Then, convert B to an int with the strconv package. If it errors, set it to 0.
Try this snippet: http://play.golang.org/p/1zqmlmIQDB
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With