I'm new to go and trying to get the following piece of code to work with no luck. Looks like I don't have the struct of structs part correctly coded. Help!
package main
import (
"encoding/xml"
"fmt"
"os"
)
func main() {
type Person struct {
Email string `xml:"email"`
Phone string `xml:"phone"`
}
type Host struct {
Hostname string `xml:"hostname"`
Address string `xml:"address"`
}
type Asset struct {
person Person
host Host
}
p := &Person{Email: "person@a.com", Phone: "1111"}
h := &Host{Hostname: "boxen", Address: "1 Place St"}
a := &Asset{person: *p, host: *h}
enc := xml.NewEncoder(os.Stdout)
enc.Indent(" ", " ")
if err := enc.Encode(p); err != nil {
fmt.Printf("error: %v
", err)
}
if err := enc.Encode(h); err != nil {
fmt.Printf("error: %v
", err)
}
if err := enc.Encode(a); err != nil {
fmt.Printf("error: %v
", err)
}
}
Expected output. What I currently get is an empty Asset element.
<Asset>
<Person>
<email>person@a.com</email>
<phone>1111</phone>
</Person>
<Host>
<hostname>boxen</hostname>
<address>1 Place St</address>
</Host>
</Asset>