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

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

使用指針時,omitempty 在編碼/xml 中不起作用?

使用指針時,omitempty 在編碼/xml 中不起作用?

Go
忽然笑 2021-06-22 21:22:07
我正在使用一個返回 XML 的 REST API,我正在嘗試解組 XML,但遇到了似乎omitempty無法正常工作的問題。這是一個工作 XML 文件的示例:<?xml version='1.0' encoding='UTF-8'?><customer uri="/api/customers/339/" id="339">    <name>        <first>Firstname</first>        <last>Lastname</last>    </name>    <email>[email protected]</email>    <billing>        <address>            <address1>123 Main St.</address123>            <address2></address2>            <city>Nowhere</city>            <state>IA</state>            <country>USA</country>            <zip>12345</zip>        </address>    </billing></customer>這是一個“壞”記錄的例子<?xml version='1.0' encoding='UTF-8'?><customer uri="/api/customers/6848/" id="6848">    <name>        <first>Firstname</first>        <last>Lastname</last>    </name>    <email/>    <billing/></customer>現在我的結構設置如下: type Customer struct {     ID      int      `xml:"id,attr"`     Name    *Name    `xml:"name,omitempty"`     Billing *Billing `xml:"billing,omitempty"` } type Billing struct {     Address *Address `xml:"address,omitempty"` } type Address struct {     address_1 string `xml:",omitempty"`     address_2 string `xml:",omitempty"`     city      string `xml:",omitempty"`     postal    string `xml:",omitempty"`     country   string `xml:",omitempty"` } type Name struct {     first, last string }當 XML 遵循第一個示例的模式時,它可以讀取所有記錄,<billing></billing>但是當它遇到類似的記錄時<billing/>會引發以下錯誤:panic: runtime error: invalid memory address or nil pointer dereference有人可以幫我弄清楚發生了什么以及如何解決嗎?
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 216 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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