gobyexample/xml/xml.go

27 lines
552 B
Go

package xml
import (
"encoding/xml"
"fmt"
)
type Player struct {
XMLName xml.Name `xml:"player"`
Id int `xml:"id,attr"`
Name string `xml:"name"`
Weapons []string `xml:"weapons>weapon"`
}
func (p *Player) String() string {
return fmt.Sprintf("Player id=%v name=%s weapons=%v", p.Id, p.Name, p.Weapons)
}
func Xml() {
geralt := &Player{Id: 1, Name: "Geralt", Weapons: []string{"Silver Sword", "Steel Sword"}}
out, _ := xml.MarshalIndent(geralt, "", " ")
fmt.Println(string(out))
fmt.Println(xml.Header + string(out))
}