[英]Iterating nested structs in golang on a template
我有以下代碼,並希望遍歷模板中的主題,但是對於我來說,我似乎無法擺脫它是一個嵌套容器的事實。
type ThemeList struct {
XMLName xml.Name `xml:"Themes"`
Themes []Theme `xml:"Theme"`
}
type Theme struct {
XMLName xml.Name `xml:"Theme"`
Name string `xml:"Name,attr"`
Page string `xml:"Page,attr"`
Tag string `xml:"Tag,attr"`
Day string `xml:"Day,attr"`
}
// Fetch the current XML document and return the Themelist[]
func openXML(filename string) ThemeList {
xmlFile, _ := os.Open(filename)
defer xmlFile.Close()
XMLdata, _ := ioutil.ReadAll(xmlFile)
var t ThemeList
xml.Unmarshal(XMLdata, &t)
return t
}
如何將它們輸出到{{range}}中,其中每個主題都是單個列表項的一部分? 當我查看它們時,輸出將在模板中使用.Name .Tag等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.