gobyexample/xml/xml.go

27 lines
552 B
Go
Raw Normal View History

2024-05-02 07:42:12 +00:00
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))
}