package main
import (
"encoding/xml"
"fmt"
"net/http"
)
type Personi struct {
Emri string
Telefonat []string `xml:"Telefonat>Telefoni"`
}
func main() {
http.HandleFunc("/xml", shfaqXML)
err := http.ListenAndServe(":8000", nil)
if err != nil {
fmt.Println(err.Error())
return
}
}
func shfaqXML(w http.ResponseWriter, r *http.Request) {
personi := Personi{"Alban", []string{"044123456", "049234567"}}
x, err := xml.MarshalIndent(personi, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/xml")
w.Write(x)
}
https://play.golang.org/p/dWlReYOqbhE
Rezultati:
<Personi>
<Emri>Alban</Emri>
<Telefonat>
<Telefoni>044123456</Telefoni>
<Telefoni>049234567</Telefoni>
</Telefonat>
</Personi>