I'm trying to create an XML implementing the MarshalXML output. But currently i'm facing several issues.

The structure i'm using for storing the data is:

type Edition struct {
    Launch         string             `xml:"launch" json:"launch"`
    Code           string             `xml:"code" json:"code"`
    Names          []NameNode         `xml:"names>name"`
    Cards          CardsComposition   `xml:"cards" json:"cards,omitempty"`
    Preconstructed PreconstructedInfo `xml:"preconstructed" json:"preconstructed,omitempty"`
    Vault          *struct{}          `xml:"vault" json:"vault"`
    Online         *struct{}          `xml:"online" json:"online"`
}

So what i did is:

func (preconstructed PreconstructedInfo) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
    if (PreconstructedInfo{} == preconstructed) {
        return nil
    }
    return e.EncodeElement(preconstructed, start)
}

And it apparently works, if I use it for encoding a single Edition entity. But if I try to encode an array of Edition entities, I get the following error:

runtime: goroutine stack exceeds 1000000000-byte limit
fatal error: stack overflow

(the array is ~200 entries)

So what I don't understand is:

  • Why the stack overflow issue happens only when I try to customize the xml, that in this case is also trying to remove empty tags, so "saving space"
  • What is the best way to do it? And someone can explain me how to implement a custom XML Marshaler for go? I found plenty for JSON marshal, but nearly nothing for XML)