2
我在Go中使用編碼/ xml包和Encoder example code。如何將XML屬性添加到Go中的元素?
雖然我能夠生成可行的XML,但我無法添加所需的所有屬性。
作爲一個例子,讓我們使用溫度報告的概念。我需要的是這樣的:
<environment>
<temperature type="float" units="c">-11.3</temperature>
</environment>
我的結構是這樣的:
type climate struct {
XMLName xml.Name `xml:"environment"`
Temperature string `xml:"temperature"`
Type string `xml:"type,attr"`
Units string `xml:"unit,attr"`
}
我到底是什麼了這個樣子的:
<environment type="float" unit="c">
<temperature>-11.3</temperature>
</environment>
My example code in the Go Playground
如何我可以格式化結構標籤以將屬性放入正確的元素中嗎?