FAQ
Is it possible with encoding/xml to marshal empty non-string elements into
an empty XML element?

Something like:

type Test struct {
   Number int
}

t := &Test{}

Marshaling t results in:

<Test>
   <Number>0</Number>
</Test>

But if the field is empty (int initialized to 0), I'd like:

<Test>
   <Number></Number>
</Test>

I've tried with pointers, ",omitempty", and the output is either nothing at
all or the XML element containing zero.
Any ideas?

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Ken GoNoob at Dec 17, 2013 at 4:13 pm
    Seems to me that an "int initialized to 0" is NOT empty. 0 is a valid
    numeric value.
    On Tuesday, December 17, 2013 6:55:09 AM UTC-5, Vedran Vekic wrote:

    Is it possible with encoding/xml to marshal empty non-string elements into
    an empty XML element?

    Something like:

    type Test struct {
    Number int
    }

    t := &Test{}

    Marshaling t results in:

    <Test>
    <Number>0</Number>
    </Test>

    But if the field is empty (int initialized to 0), I'd like:

    <Test>
    <Number></Number>
    </Test>

    I've tried with pointers, ",omitempty", and the output is either nothing
    at all or the XML element containing zero.
    Any ideas?
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Egon at Dec 17, 2013 at 4:32 pm
    Probably the easiest solution would be to implement a custom int type with
    custom marshaling.

    + egon
    On Tuesday, December 17, 2013 1:55:09 PM UTC+2, Vedran Vekic wrote:

    Is it possible with encoding/xml to marshal empty non-string elements into
    an empty XML element?

    Something like:

    type Test struct {
    Number int
    }

    t := &Test{}

    Marshaling t results in:

    <Test>
    <Number>0</Number>
    </Test>

    But if the field is empty (int initialized to 0), I'd like:

    <Test>
    <Number></Number>
    </Test>

    I've tried with pointers, ",omitempty", and the output is either nothing
    at all or the XML element containing zero.
    Any ideas?
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 17, '13 at 11:55a
activeDec 17, '13 at 4:32p
posts3
users3
websitegolang.org

3 users in discussion

Ken GoNoob: 1 post Egon: 1 post Vedran Vekic: 1 post

People

Translate

site design / logo © 2021 Grokbase