我对 go 语言还比较陌生。
我在尝试解组 soap 消息时遇到问题。我的尝试是抽象 body 元素的内容并避免静态定义 xml 结构,因为它会根据请求的操作而变化。 不幸的是我找不到正确的方法。在示例中,getcontent 函数应接收指向包含内容的结构的指针,并将其动态添加到 body 中,以便进行填充。但结果并不是预期的。
package main import ( "encoding/xml" "fmt" ) type Message interface{} type EnvelopeResponse struct { XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"` Body Message `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"` } type Body struct { XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"` Fault *Fault `xml:",omitempty"` Content Message `xml:",omitempty"` SOAPBodyContentType string `xml:"-"` } type Fault struct { XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Fault"` Code string `xml:"faultcode,omitempty"` String string `xml:"faultstring,omitempty"` Actor string `xml:"faultactor,omitempty"` Detail string `xml:"detail,omitempty"` } type GetHostNumberOfEntriesResponse struct { XMLName xml.Name `xml:"urn:dslforum-org:service:Hosts:1 GetHostNumberOfEntriesResponse"` NewHostNumberOfEntries int64 `xml:"NewHostNumberOfEntries"` } func GetContent(rawXml []byte, content interface{}) { envelope := EnvelopeResponse{Body: Body{Content: content}} xml.Unmarshal(rawXml, &envelope) } func main() { b := []byte(` <?xml version="1.0"?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <s:Body> <u:GetHostNumberOfEntriesResponse xmlns:u="urn:dslforum-org:service:Hosts:1"> <NewHostNumberOfEntries>47</NewHostNumberOfEntries> </u:GetHostNumberOfEntriesResponse> </s:Body> </s:Envelope> `) content := &GetHostNumberOfEntriesResponse{} GetContent(b, content) fmt.Println(*content) }
这是操场上的示例:
https://go.dev/play/p/bbr4vexipbc
正确答案
我找到的解决方案是使用泛型来表示主体的变量和参数化内容。
此代码按我的预期工作:
package main import ( "encoding/xml" "fmt" ) type EnvelopeResponse[T any] struct { XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"` Body Body[T] `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"` } type Body[T any] struct { XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"` Fault *Fault `xml:",omitempty"` Content T `xml:",omitempty"` SOAPBodyContentType string `xml:"-"` } type Fault struct { XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Fault"` Code string `xml:"faultcode,omitempty"` String string `xml:"faultstring,omitempty"` Actor string `xml:"faultactor,omitempty"` Detail string `xml:"detail,omitempty"` } type GetHostNumberOfEntriesResponse struct { XMLName xml.Name `xml:"urn:dslforum-org:service:Hosts:1 GetHostNumberOfEntriesResponse"` NewHostNumberOfEntries int64 `xml:"NewHostNumberOfEntries"` } func GetContent[T any](rawXml []byte, content T) { envelope := EnvelopeResponse[T]{Body: Body[T]{Content: content}} xml.Unmarshal(rawXml, &envelope) } func main() { b := []byte(` <?xml version="1.0"?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <s:Body> <u:GetHostNumberOfEntriesResponse xmlns:u="urn:dslforum-org:service:Hosts:1"> <NewHostNumberOfEntries>47</NewHostNumberOfEntries> </u:GetHostNumberOfEntriesResponse> </s:Body> </s:Envelope> `) content := &GetHostNumberOfEntriesResponse{} GetContent(b, content) fmt.Println(*content) }
以上是使用 Go 解组 SOAP 消息的详细内容。更多信息请关注PHP中文网其他相关文章!

Golang在实际应用中表现出色,以简洁、高效和并发性着称。 1)通过Goroutines和Channels实现并发编程,2)利用接口和多态编写灵活代码,3)使用net/http包简化网络编程,4)构建高效并发爬虫,5)通过工具和最佳实践进行调试和优化。

Go语言的核心特性包括垃圾回收、静态链接和并发支持。1.Go语言的并发模型通过goroutine和channel实现高效并发编程。2.接口和多态性通过实现接口方法,使得不同类型可以统一处理。3.基本用法展示了函数定义和调用的高效性。4.高级用法中,切片提供了动态调整大小的强大功能。5.常见错误如竞态条件可以通过gotest-race检测并解决。6.性能优化通过sync.Pool重用对象,减少垃圾回收压力。

Go语言在构建高效且可扩展的系统中表现出色,其优势包括:1.高性能:编译成机器码,运行速度快;2.并发编程:通过goroutines和channels简化多任务处理;3.简洁性:语法简洁,降低学习和维护成本;4.跨平台:支持跨平台编译,方便部署。

关于SQL查询结果排序的疑惑学习SQL的过程中,常常会遇到一些令人困惑的问题。最近,笔者在阅读《MICK-SQL基础�...

golang ...

Go语言中如何对比并处理三个结构体在Go语言编程中,有时需要对比两个结构体的差异,并将这些差异应用到第�...


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

SublimeText3汉化版
中文版,非常好用

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能