Я думаю, вы ожидаете что-то подобное, верно?
package main
import (
"encoding/xml"
"net/http"
)
// Response XML struct
type Response struct {
Greeting string
Names []string `xml:"Names>Name"`
}
func hello(w http.ResponseWriter, r *http.Request) {
response := Response{"Hello", []string{"World", "Sarkar"}}
// Wraps the response to Response struct
x, err := xml.MarshalIndent(response, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/xml")
// Write
w.Write(x)
}
func main() {
http.HandleFunc("/hello", hello)
http.ListenAndServe(":9090", nil)
}
Выход:
HTTP/1.1 200 OK
Content-Type: application/xml
Date: Fri, 17 Apr 2020 07:01:46 GMT
Content-Length: 119
<Response>
<Greeting>Hello</Greeting>
<Names>
<Name>World</Name>
<Name>Sarkar</Name>
</Names>
</Response>