Aller au contenu

XML

Le package encoding/xml fournit le marshaling et unmarshaling XML similaire au JSON. Les tags de structure spécifient les noms d’éléments et les attributs.

Marshaller une structure en XML.

package main
import (
"encoding/xml"
"fmt"
)
type Personne struct {
XMLName xml.Name `xml:"personne"`
Nom string `xml:"nom"`
Age int `xml:"age"`
}
func main() {
p := Personne{Nom: "Alice", Age: 30}
donnees, _ := xml.MarshalIndent(p, "", " ")
fmt.Println(string(donnees))
}
Fenêtre de terminal
<personne>
<nom>Alice</nom>
<age>30</age>
</personne>