英文:
how to get data in interface{} while unmarshaling an XML in Golang?
问题
我正在尝试使用interface{}在Golang中进行XML的编组和解组。原因是在调用SOAP服务器时,我的SOAP信封和头部是相同的,但我想通过传递不同的结构体来传递不同的SOAP函数。我在playground上编写了一个示例代码(与SOAP无关)。我能够使用interface{}进行XML编组,但无法解组。
这是链接 Play Ground
请告诉我我做错了什么?
英文:
I am trying to use interface{} to Marshal and UnMarshal my xml in golang. The reason is that as in calling a soap server my soap envelope, header are same but i want to pass the different soap function by passing different structs. I have made a sample code in playground(not related to soap). I am able to marshal the xml with interface{} but unable to unmarshal.
Here is link Play Ground
Please tell me what i am doing wrong ?
答案1
得分: 1
你无法将数据解组为一个空接口,因为空接口没有任何导出字段来映射 XML 的键/值。如果你想共享相同的代码以“动态”地处理不同的消息,你应该将其存储在字符串或字节数组中(在这种情况下推荐使用字节数组)。然后根据你的需求处理数据。
http://play.golang.org/p/sPq0ZfAcU7
英文:
You can't unmarshal to an empty interface since the empty interface doesn't have any exported fields to map the xml keys/values to. If you want share your same code to "dynamically" handle the message differently, you should store it in a string or byte array (a byte array is recommended in this case). You then process the data based on your needs.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论