package main import ( "encoding/xml" "fmt" "net/http" "strconv" ) type Tag struct { Key string `xml:"name,attr,omitempty"` Value string `xml:",chardata"` L string `xml:"l,attr,omitempty"` } type wrt100Result struct { XMLName struct{} `xml:"root"` Id int `xml:"id"` Unit string `xml:"unit"` Access []string `xml:"access>item"` Tags []Tag `xml:"tags>tag"` } func main() { http.HandleFunc("/wrt100", func(wr http.ResponseWriter, req *http.Request) { res := &wrt100Result{ Id:123, Unit: "EugeneX", Access: []string{"member", "admin"}, Tags: []Tag{ {Key: "most", Value: "wanted", L: "13"}, {Key: "lost", Value: "everything", L: "25"}, }, } for i, s := range []string{"Hw", "Rules", "Warp"} { res.Tags = append(res.Tags, Tag{ L: strconv.Itoa(i + 1), Value: s, }) } b, _ := xml.Marshal(res) wr.WriteHeader(200) fmt.Fprintln(wr, `<?xml version="1.0" encoding="UTF-8" ?>`) wr.Write(b) }) http.ListenAndServe(":7000", nil) }
Выводит вот такой вот XML
<?xml version="1.0" encoding="UTF-8" ?> <root> <id>123</id> <unit>EugeneX</unit> <access> <item>member</item> <item>admin</item> </access> <tags> <tag name="most" l="13">wanted</tag> <tag name="lost" l="25">everything</tag> <tag l="1">Hw</tag> <tag l="2">Rules</tag> <tag l="3">Warp</tag> </tags> </root>