Add xml
This commit is contained in:
parent
f042da5479
commit
b1fe04a282
6
main.go
6
main.go
@ -26,7 +26,8 @@ package main
|
|||||||
// import "git.sangeeth.dev/gobyexample/formatting"
|
// import "git.sangeeth.dev/gobyexample/formatting"
|
||||||
// import "git.sangeeth.dev/gobyexample/templates"
|
// import "git.sangeeth.dev/gobyexample/templates"
|
||||||
// import "git.sangeeth.dev/gobyexample/regex"
|
// import "git.sangeeth.dev/gobyexample/regex"
|
||||||
import "git.sangeeth.dev/gobyexample/json"
|
// import "git.sangeeth.dev/gobyexample/json"
|
||||||
|
import "git.sangeeth.dev/gobyexample/xml"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// runes.Runes()
|
// runes.Runes()
|
||||||
@ -55,5 +56,6 @@ func main() {
|
|||||||
// formatting.Formatting()
|
// formatting.Formatting()
|
||||||
// templates.Templates()
|
// templates.Templates()
|
||||||
// regex.Regex()
|
// regex.Regex()
|
||||||
json.Json()
|
// json.Json()
|
||||||
|
xml.Xml()
|
||||||
}
|
}
|
||||||
|
26
xml/xml.go
Normal file
26
xml/xml.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
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))
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user