我正在使用一個返回 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 回答
- 0 關注
- 216 瀏覽
添加回答
舉報
0/150
提交
取消