xml.NewDecoder(resp.Body).Decode 在Go 中給出EOF 錯誤
當嘗試使用以下命令從HTTP 回應正文中解碼XML 時xml.NewDecoder,您可能會遇到「EOF」錯誤。這種情況通常發生在您之前使用過回應正文,導致後續嘗試解碼 XML 時無法使用該正文。
以下是程式碼細分:
<code class="go">conts1, err := ioutil.ReadAll(resp1.Body)</code>
此程式碼讀取正文使用 ioutil.ReadAll,有效地消耗整個回應。
<code class="go">if err := xml.NewDecoder(resp1.Body).Decode(&v); err != nil { fmt.Printf("error is : %v", err)</code>
使用 ioutil.ReadAll 讀取正文後,嘗試從同一正文 (resp1.Body) 解碼 XML 將導致 EOF 錯誤,因為內容已被消耗。
解決方案:
要解決此問題,請在使用 ioutil.ReadAll 消耗響應正文之前將其儲存到變數中。這允許您從緩衝回應中解碼 XML。
<code class="go">resp1Bytes, err := ioutil.ReadAll(resp1.Body)</code>
然後,使用此緩衝響應進行解碼:
<code class="go">if err := xml.NewDecoder(bytes.NewReader(resp1Bytes)).Decode(&v); err != nil { fmt.Printf("error is : %v", err) }</code>
以上是在 Go 中從 HTTP 回應正文解碼 XML 時,為什麼會收到 EOF 錯誤?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Interfacesand -polymormormormormormingingoenhancecodereusanity和Maintainability.1)defineInterfaceSattherightabStractractionLevel.2)useInterInterFacesFordEffordExpentIndention.3)ProfileCodeTomeAgePerformancemacts。

initiTfunctioningOrunSautomation beforeTheMainFunctionToInitializePackages andSetUptheNvironment.it'susefulforsettingupglobalvariables,資源和performingOne-timesEtepaskSarpaskSacraskSacrastAscacrAssanyPackage.here'shere'shere'shere'shere'shodshowitworks:1)Itcanbebeusedinanananainapthecate,NotjustAckAckAptocakeo

接口組合在Go編程中通過將功能分解為小型、專注的接口來構建複雜抽象。 1)定義Reader、Writer和Closer接口。 2)通過組合這些接口創建如File和NetworkStream的複雜類型。 3)使用ProcessData函數展示如何處理這些組合接口。這種方法增強了代碼的靈活性、可測試性和可重用性,但需注意避免過度碎片化和組合複雜性。

initfunctionsingoareAutomationalCalledBeLedBeForeTheMainFunctionandAreuseFulforSetupButcomeWithChallenges.1)executiondorder:totiernitFunctionSrunIndIndefinitionorder,cancancapationSifsUsiseSiftheyDepplothother.2)測試:sterfunctionsmunctionsmunctionsMayInterfionsMayInterferfereWithTests,b

本文討論了GO中的數組和切片之間的差異,重點是尺寸,內存分配,功能傳遞和用法方案。陣列是固定尺寸的,分配的堆棧,而切片是動態的,通常是堆積的,並且更靈活。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

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

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具