我是新手,尝试让下面的代码顺利运行。看起来我没有正确编码structs的struct部分。救命!
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: "[email protected]", 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\n", err)
}
if err := enc.Encode(h); err != nil {
fmt.Printf("error: %v\n", err)
}
if err := enc.Encode(a); err != nil {
fmt.Printf("error: %v\n", err)
}
}
预期的输出。我目前得到的是一个空的Asset元素。
<Asset>
<Person>
<email>[email protected]</email>
<phone>1111</phone>
</Person>
<Host>
<hostname>boxen</hostname>
<address>1 Place St</address>
</Host>
</Asset>
您必须通过使“资产”类型的属性名称以大写字母开头来导出它们:
type Asset struct {
Person Person
Host Host
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句