https://play.golang.org/p/O8eNLkLA6spackage main
import (
"encoding/xml"
"fmt"
)
type Plans struct {
XMLName xml.Name `xml:"Plans"`
Xsd string `xml:"xmlns:xsd,attr"`
Xsi string `xml:"xmlns:xsi,attr"`
Fields Fields
}
type Fields struct {
Field []Field
}
type Field struct {
Id int `xml:"Id,attr"`
Values []string `xml:"Value"`
}
func main() {
v := &Plans{Xsd: "http://www.w3.org/2001/XMLSchema", Xsi: "http://www.w3.org/2001/XMLSchema-instance"}
v.Fields = Fields{
[]Field{
Field{Id: 1, Values: []string{ "One","two"}},
Field{Id: 2, Values: []string{ "Two"}},
},
}
out, err := xml.MarshalIndent(v, "", " ")
if err != nil {
fmt.Println(err)
}
fmt.Println(xml.Header + string(out))
}
<?xml version="1.0" encoding="UTF-8"?>
<Plans xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Fields>
<Field Id="1">
<Value>One</Value>
<Value>two</Value>
</Field>
<Field Id="2">
<Value>Two</Value>
</Field>
</Fields>
</Plans>