我對 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 無盡。

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

記事本++7.3.1
好用且免費的程式碼編輯器

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境