亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Golang 嵌套、重命名的 XML 屬性

Golang 嵌套、重命名的 XML 屬性

Go
海綿寶寶撒 2022-01-04 10:06:07
給定以下結構: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


查看完整回答
反對 回復 2022-01-04
  • 1 回答
  • 0 關注
  • 220 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號