I'm programming an xml response API in golang. Following the examples in the xml documentation it's pretty easy to create the xml giving them attributes and more.
The thing is that I need multiple tags under the same name and in different order.
<block>
<tag1>A Value1</tag1>
<tag2>B Value1</tag2>
<tag1>A Value2</tag1>
</block>
By creating a structure like
type Tag1 struct {
Value string `xml:",chardata"`
}
type Tag2 struct {
Value string `xml:",chardata"`
}
type Block struct {
XMLName xml.Name `xml:"block"`
Tags1 []Tag1 `xml:"tag1"`
Tags2 []Tag2 `xml:"tag2"`
}
I achieved this xml:
<block>
<tag1>A Value1</tag1>
<tag1>A Value2</tag1>
<tag2>B Value1</tag2>
</block>
That's due to the array type inside the block
definition. What I want, is a way to define that array type as an interface to inherit the two Tag
types. Like this
type Block struct {
XMLName xml.Name `xml:"block"`
Tags []Tags `xml:"tags"`
}
type (Tags t) Tag1 struct{
Value string `xml:",chardata"`
}
type (Tags t) Tag2 struct{
Value string `xml:",chardata"`
}
This way I could define the order of appearance in xml by the order of appending like
a1 := Tag1{"A Value1"}
b1 := Tag2{"B Value1"}
a2 := Tag1{"A Value2"}
arr := []Tags{}
arr = append(arr,a1)
arr = append(arr,b1)
arr = append(arr,a2)
v.Tags = arr
I've made some reading and I'm trying to avoid to implement the marshaling function used inside the xml package. Any help is appreciated, thanks.