I'm learning Go by writing a program that generates files in Collada format, which describes geometry using XML.
You annotate your structs and almost everything works as you'd expect, except I can't figure out how to marshal arrays into one XML element - I always end up generating N elements.
In other words, I'd like
<input>
<p>0 1 2</p>
</input>
instead of
<input>
<p>0</p>
<p>1</p>
<p>2</p>
</input>
The code is as follows
package main
import (
"encoding/xml"
"os"
)
func main() {
type Vert struct {
XMLName xml.Name `xml:"input"`
Indices []int `xml:"p"`
}
v := &Vert{Indices:[]int{0, 1, 2}}
output, err := xml.MarshalIndent(v, "", " ")
if err == nil {
os.Stdout.Write(output)
}
}
Various comments (and code) from encoding/xml/marshal.go seem to imply that I'm out of luck:
// Marshal handles an array or slice by marshalling each of the elements.
// Slices and arrays iterate over the elements. They do not have an enclosing tag.
Strangely, if I change my array type to uint8, the array is not marshalled at all.
If I am out of luck, I'll probably use the xml:",innerxml" annotation to substitute the array myself.