給定以下結構:type book struct { XMLName xml.Name `xml:"DailyAct"` Symbol string `xml:"TradeInstrId,attr"` EntityId string `xml:"EntityId,attr"` AssetClass string `xml:"AssetClass,attr"` Open int `xml:"Open"` High int `xml:"High"` Low int `xml:"Low"` Close int `xml:"Close"` // Type string `` //I'll leave this for another question}我當前的 XML: <DailyAct EntityId="foo" AssetClass="bar" TradeInstrId="Symbol" > <Open>2</Open> <High>3</High> <Low>1</Low> <Close>5</Close> </DailyAct>但是,我需要重新調整結構的某些字段(或以另一種方式生成 xml)來實現:<DailyAct EntityId="foo" AssetClass="bar" TradeInstrId="Symbol"> <Open Price="2" Type="IND"/> <High Price="6" Type="IND"/> <Low Price="1" Type="IND"/> <Close Price="4" Type="IND"/></DailyAct>但是我得到:&errors.errorString{s:"xml: DailyAct>Open chain not valid with Price,attr flag"} (actual)當我嘗試像這樣嵌套字段時:type book struct { //... Open int `xml:"DailyAct>Open,Price,attr>"` //...}編輯: 我發現了這個討論,同時在谷歌上搜索,所以我想要的東西目前可能不可行
1 回答

SMILET
TA貢獻1796條經驗 獲得超4個贊
你是對的,目前這是不可能的。但是您可以使用子結構,例如
type PriceType struct {
Price int `xml:"Price,attr"`
Type string `xml:"Type,attr"`
}
type Book struct {
XMLName xml.Name `xml:"DailyAct"`
Symbol string `xml:"TradeInstrId,attr"`
EntityId string `xml:"EntityId,attr"`
AssetClass string `xml:"AssetClass,attr"`
Open PriceType `xml:"Open"`
High PriceType `xml:"High"`
Low PriceType `xml:"Low"`
Close PriceType `xml:"Close"`
}
這里的例子http://play.golang.org/p/Ekd6Xf3miS
- 1 回答
- 0 關注
- 220 瀏覽
添加回答
舉報
0/150
提交
取消