Initial code for XML sample
Covers marshal/unmarshal of a simple un-nested type. No .sh file yet
This commit is contained in:
parent
78e1fd61ec
commit
62bfb159ac
32
examples/xml/xml.go
Normal file
32
examples/xml/xml.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Plant struct {
|
||||||
|
XMLName xml.Name `xml:"fruit"`
|
||||||
|
Id int `xml:"id,attr"`
|
||||||
|
Name string `xml:"name"`
|
||||||
|
Origin []string `xml:"origin"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Plant) String() string {
|
||||||
|
return fmt.Sprintf("Fruit id=%v, name=%v, origin=%v",
|
||||||
|
p.Id, p.Name, p.Origin)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
coffee := &Plant{Id: 27, Name: "Coffee"}
|
||||||
|
coffee.Origin = []string{"Ethiopia", "Brazil"}
|
||||||
|
|
||||||
|
out, _ := xml.MarshalIndent(coffee, " ", " ")
|
||||||
|
fmt.Println(string(out))
|
||||||
|
|
||||||
|
var p Plant
|
||||||
|
if err := xml.Unmarshal(out, &p); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
fmt.Println(p)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user