我在这里举个例子: https://www.socketloop.com/tutorials/golang-create-new-xml-file
如果在工作人员之后更深入怎么办?
type Staff struct {
XMLName xml.Name `xml:"staff"`
ID int `xml:"id"`
FirstName string `xml:"firstname"`
LastName string `xml:"lastname"`
UserName string `xml:"username"`
Thing Thing `xml:"thing"`
}
我如何追加?我尝试过这样的事情:
v.Staffs = append(v.Staffs, Staff{ID: 108, FirstName: "Jennifer", LastName: "Loh", UserName: "jenniferloh", Thing.tape: "scotch"})
但是只有两个循环迭代的语句才能起作用并且难看:
v.Staffs = append(v.Staffs, Staff{ID: 108, FirstName: "Jennifer", LastName: "Loh", UserName: "jenniferloh"})
v.Staffs.Thing[i] = append(v.Staffs.Thing, Thing{scotch: "tape"})
有更好的方法吗?感谢。
答案 0 :(得分:0)
type Thing struct {
stuff string `xml:"stuff"`
}
type Staff struct {
XMLName xml.Name `xml:"staff"`
ID int `xml:"id"`
FirstName string `xml:"firstname"`
LastName string `xml:"lastname"`
UserName string `xml:"username"`
Thing Thing `xml:"thing"`
}
stuff := Thing{Stuff: "120"}
v.Staffs = append(v.Staffs, Staff{ID: 108, FirstName: "Jennifer", LastName: "Loh", UserName: "jenniferloh", Thing: stuff})