


为什么在 `ioutil.ReadAll(resp1.Body)` 读取响应正文后,`xml.NewDecoder(resp.Body).Decode` 返回 \'EOF\' 错误?
xml.NewDecoder(resp.Body).Decode 中出现 EOF 错误
问题:
尝试使用 xml.NewDecoder(resp.Body).Decode 从 HTTP 响应中解码 XML 时,尽管已使用 xml.Unmarshal 成功解码响应正文,但仍会出现错误“EOF”。
Code :
<code class="go">req1, err := http.NewRequest("GET", concat([]string{domain, defects_link}), nil) error_handler(err) req1.Close = true resp1, err := client.Do(req1) error_handler(err) fmt.Printf("\n %s \n", resp1.Status) defer resp1.Body.Close() conts1, err := ioutil.ReadAll(resp1.Body) error_handler(err) fmt.Println("Response Body is Here :", string(conts1))//Contents are Printed Here if err := xml.NewDecoder(resp1.Body).Decode(&v); err != nil { fmt.Printf("error is : %v", err) return }</code>
说明:
发生错误“EOF”(文件结束),因为响应正文已被 ioutil 消耗前面几行中的.ReadAll(resp1.Body)。此函数读取正文中的所有数据,直到 EOF,不留下任何内容供 xml.NewDecoder 解码。
解决方案:
要解决此问题,正文内容应该在使用 xml.NewDecoder 解码之前将其复制到新的 io.Reader 中。这允许多个阅读器访问正文的内容。
一种方法是使用 bytes.NewReader 从正文内容创建新的阅读器:
<code class="go">bodyBuffer := bytes.NewReader(conts1) decoder := xml.NewDecoder(bodyBuffer) err := decoder.Decode(&v) if err != nil { fmt.Printf("error is : %v", err) }</code>
或者, resp1.Body 可以是使用 resp1.Body.Seek(0, 0)“回退”到其初始状态:
<code class="go">_, err = resp1.Body.Seek(0, 0) if err != nil { fmt.Printf("error is : %v", err) } decoder := xml.NewDecoder(resp1.Body) err = decoder.Decode(&v) if err != nil { fmt.Printf("error is : %v", err) }</code>
以上是为什么在 `ioutil.ReadAll(resp1.Body)` 读取响应正文后,`xml.NewDecoder(resp.Body).Decode` 返回 \'EOF\' 错误?的详细内容。更多信息请关注PHP中文网其他相关文章!

whentestinggocodewithinitfunctions,useexplicitseTupfunctionsorseParateTestFileSteSteTepteTementDippedDependendendencyOnInItfunctionsIdeFunctionSideFunctionsEffect.1)useexplicitsetupfunctionStocontrolglobalvaribalization.2)createSepEpontrolglobalvarialization

go'serrorhandlingurturnserrorsasvalues,与Javaandpythonwhichuseexceptions.1)go'smethodensursexplitirorhanderling,propertingrobustcodebutincreasingverbosity.2)

AnefactiveInterfaceoisminimal,clear and promotesloosecoupling.1)minimizeTheInterfaceForflexibility andeaseofimplementation.2)useInterInterfaceForeabStractionTosWapImplementations withCallingCallingCode.3)

集中式错误处理在Go语言中可以提升代码的可读性和可维护性。其实现方式和优势包括:1.将错误处理逻辑从业务逻辑中分离,简化代码。2.通过集中处理错误,确保错误处理的一致性。3.使用defer和recover来捕获和处理panic,增强程序健壮性。

Ingo,替代词Inivuntionsionializatializatializationfunctionsandsingletons.1)customInitializationfunctions hallowexpliticpliticpliticconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconcontirization curssementializatizatupsetups.2)单次固定元素限制ininconinconcurrent

Gohandlesinterfacesandtypeassertionseffectively,enhancingcodeflexibilityandrobustness.1)Typeassertionsallowruntimetypechecking,asseenwiththeShapeinterfaceandCircletype.2)Typeswitcheshandlemultipletypesefficiently,usefulforvariousshapesimplementingthe

Go语言的错误处理通过errors.Is和errors.As函数变得更加灵活和可读。1.errors.Is用于检查错误是否与指定错误相同,适用于错误链的处理。2.errors.As不仅能检查错误类型,还能将错误转换为具体类型,方便提取错误信息。使用这些函数可以简化错误处理逻辑,但需注意错误链的正确传递和避免过度依赖以防代码复杂化。

tomakegoapplicationsRunfasterandMorefly,useProflingTools,leverageConCurrency,andManageMoryfectily.1)usepprofforcpuorforcpuandmemoryproflingtoidentifybottlenecks.2)upitizegorizegoroutizegoroutinesandchannelstoparalletaparelalyizetasksandimproverperformance.3)


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

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

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

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

WebStorm Mac版
好用的JavaScript开发工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。