i\'m learning to create XML in Go. Here\'s my code:
type Request struct {
XMLName xml.Name `xml:\"request\"`
Action string
Several things are wrong in your code. When working with encoding packages in Go, all the fields you want to marshal/unmarshal have to be exported. Note that the structs themselves do not have to be exported.
So, first step is to change the point struct to export the fields:
type point struct {
Geo string `xml:"point"`
Radius int `xml:"radius,attr"`
}
Now, if you want to display the Geo field inside a point, you have to add ,cdata to the xml tag. Finally, there is no need to add an omitempty keyword to a slice.
type Request struct {
XMLName xml.Name `xml:"request"`
Action string `xml:"action,attr"`
Point []point `xml:"point"`
}
type point struct {
Geo string `xml:",chardata"`
Radius int `xml:"radius,attr"`
}
Go playground