跳转到主内容
极星编程网:以代码为星,赴技术山海!

Go如何处理HTTP响应数据_HTTP响应读取方式

Go处理HTTP响应需先检查StatusCode≥300再读Body,及时Close()防止连接耗尽;小JSON用io.ReadAll,大或流式用json.NewDecoder;Content-Type决定编码处理,流式响应须用bufio按行读取并及时解码。 Go 处理 HTTP 响应数据,核心不是“怎么读”,而是“什么时候读、读多少、读完怎么用”。 状态码 没检查就急着
io.ReadAll
,Body 没
Close()
就返回,JSON 解析前不看
Content-Type
—— 这些都会让程序在压测时连接耗尽、线上突然 OOM、或解析出空结构体却查不出原因。 先 check StatusCode,再碰 Body 很多 bug 都源于把 404/500 当成成功响应处理。Go 的
http.Response
不会自动拒绝非 2xx 响应,它只管传数据。 必须显式判断:
if resp.StatusCode = 300
(比只判
!= http.StatusOK
更稳妥,覆盖 201/204 等合法非 200 场景) 非成功状态也得读并关掉
resp.Body
,否则连接无法复用,高并发下
net/http: timeout awaiting response headers
或
too many open files
就来了 错误提示建议带原始响应体(限长):
body, _ := io.ReadAll(io.LimitReader(resp.Body, 1024))
,再
defer resp.Body.Close()
读 Body:io.ReadAll 还是 js on.NewDecoder? 取决于你对内存和格式的信任程度。小 JSON 响应两者差别不大;大响应或流式接口,选错就容易 OOM 或解析失败。
io.ReadAll(resp.Body)
:简单直接,但整块进内存 —— 下载 500MB 文件时会瞬间吃光 1GB RAM
json.NewDecoder(resp.Body).Decode(&v)
:边读边解,支持 UTF-8 BOM 自动跳过,且不占额外内存;适合单个 JSON 对象 如果是 JSON 数组流(如 NDJSON),用
decoder := json.NewDecoder(resp.Body); for decoder.More() { decoder.Decode(&item) }
别忘了:无论哪种方式,
resp.Body
仍需
defer resp.Body.Close()
Content-Type 和 编码 不能靠猜 HTTP 响应头里的
Content-Type
是唯一可信线索,比如
application/json; charset=gbk
—— Go 标准库 的
json.Unmarshal
会直接 panic。 php版微信js-sdk支付接口类 php版微信js-sdk支付接口类 下载 先打印调试:
log.Printf("Content-Type: %s", resp.Header.Get("Content-Type"))
若含
charset=gbk
或
charset=gb2312
,用
golang.org/x/text/encoding
转 UTF-8 再解码 纯文本或 HTML 响应,别用
string(bodyBytes)
硬转 —— BOM、换行符、\u0000 都可能让字符串截断或解析异常 HTML 解析务必用
golang.org/x/net/html
,正则提取
</code></div> 类操作在真实页面中基本不可靠 流式响应(SSE / JSON Stream)必须用 bufio 服务端用 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">text/event-stream</code></div> 或逐行推送 JSON, <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">io.ReadAll</code></div> 会一直阻塞到连接关闭 —— 而流式连接本就不关。 用 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">reader := bufio.NewReader(resp.Body)</code></div> 包装响应体 按行读: <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">line, err := reader.ReadBytes('\n')</code></div> ;按分隔符读: <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">reader.ReadBytes('\x00')</code></div> 循环中检测 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">err == io.EOF</code></div> 表示流结束; <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">err != nil && err != io.EOF</code></div> 才是真错误 每行处理完记得 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">json.Unmarshal(line, &event)</code></div> ,别 accumulate 到切片里等最后处理 —— 否则又变内存黑洞 最常被跳过的其实是 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">resp.Body.Close()</code></div> 的位置 —— 它必须在所有 error 分支之后、函数退出前执行,哪怕你只读了前 10 字节 。Go 不会替你记着这个连接,它只等你关。 </div> <!-- 文章导航 --> <nav class="jxgpc-article-nav" aria-label="文章导航"> <a href="http://www.jxgpc.com/pc/28153.html" class="jxgpc-article-nav-link jxgpc-article-nav-prev"> <span>‹</span> <div> <div class="jxgpc-article-nav-label">上一篇</div> <div class="jxgpc-article-nav-title">C++实现字符串中单词的顺序反转 _ 结合stringstream与reverse【详解】</div> </div> </a> <a href="http://www.jxgpc.com/pc/28161.html" class="jxgpc-article-nav-link jxgpc-article-nav-next"> <div> <div class="jxgpc-article-nav-label">下一篇</div> <div class="jxgpc-article-nav-title">php二维数组怎么转换为字符串</div> </div> <span>›</span> </a> </nav> </div> </article> <!-- 相关文章列表 --> <section class="jxgpc-content-section" style="margin-top: 20px;"> <h2 class="jxgpc-widget-title" style="margin-bottom: 20px;">相关文章</h2> <ul class="jxgpc-article-list"> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/28153.html" class="jxgpc-article-link">C++实现字符串中单词的顺序反转 _ 结合stringstream与reverse【详解】</a> <div class="jxgpc-article-meta">2026-10-21</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/28145.html" class="jxgpc-article-link">网站当前的在线人数</a> <div class="jxgpc-article-meta">2026-10-21</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/28142.html" class="jxgpc-article-link">代币化美股成热度飙升,“加密券商”颠覆传统券商?</a> <div class="jxgpc-article-meta">2026-10-21</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/28139.html" class="jxgpc-article-link">netbeans for php 最惯用快捷键整理</a> <div class="jxgpc-article-meta">2026-10-21</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/28137.html" class="jxgpc-article-link">php获取复选框中值的例子</a> <div class="jxgpc-article-meta">2026-10-21</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/28125.html" class="jxgpc-article-link">如何在PHP中调用其他文件中的方法?</a> <div class="jxgpc-article-meta">2026-10-21</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/28121.html" class="jxgpc-article-link">简单的利用php谢了一个分页的功能模块,php分页功能模块_PHP教程</a> <div class="jxgpc-article-meta">2026-10-21</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/28119.html" class="jxgpc-article-link">修复PHP Dompdf中文乱码的有效途径</a> <div class="jxgpc-article-meta">2026-10-21</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/28103.html" class="jxgpc-article-link">C++中using的三种用法举例详解</a> <div class="jxgpc-article-meta">2026-10-21</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/28161.html" class="jxgpc-article-link">php二维数组怎么转换为字符串</a> <div class="jxgpc-article-meta">2026-10-21</div> </li> </ul> </section> </div> <!-- 右侧边栏 --> <aside class="jxgpc-sidebar jxgpc-sidebar-right" aria-label="侧边栏"> <!-- 推荐阅读 --> <div class="jxgpc-widget"> <h3 class="jxgpc-widget-title">推荐阅读</h3> <div class="jxgpc-recommend-grid"> <a href="http://www.jxgpc.com/pc/28161.html" class="jxgpc-recommend-item"> <div class="jxgpc-recommend-thumb"> <img src="http://www.jxgpc.com/static/v3/images/article-6.jpg" alt="php二维数组怎么转换为字符串"> </div> <div class="jxgpc-recommend-title">php二维数组怎么转换为字符串</div> </a> <a href="http://www.jxgpc.com/pc/28160.html" class="jxgpc-recommend-item"> <div class="jxgpc-recommend-thumb"> <img src="http://www.jxgpc.com/static/v3/images/article-6.jpg" alt="Go如何处理HTTP响应数据_HTTP响应读取方式"> </div> <div class="jxgpc-recommend-title">Go如何处理HTTP响应数据_HTTP响应读取方式</div> </a> <a href="http://www.jxgpc.com/pc/28153.html" class="jxgpc-recommend-item"> <div class="jxgpc-recommend-thumb"> <img src="http://www.jxgpc.com/static/v3/images/article-6.jpg" alt="C++实现字符串中单词的顺序反转 _ 结合stringstream与reverse【详解】"> </div> <div class="jxgpc-recommend-title">C++实现字符串中单词的顺序反转 _ 结合stringstream与reverse【详解】</div> </a> <a href="http://www.jxgpc.com/pc/28145.html" class="jxgpc-recommend-item"> <div class="jxgpc-recommend-thumb"> <img src="http://www.jxgpc.com/static/v3/images/article-6.jpg" alt="网站当前的在线人数"> </div> <div class="jxgpc-recommend-title">网站当前的在线人数</div> </a> </div> </div> <!-- 最新文章 --> <div class="jxgpc-widget"> <h3 class="jxgpc-widget-title">最新文章</h3> <ul class="jxgpc-article-list"> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/28161.html" class="jxgpc-article-link">php二维数组怎么转换为字符串</a> <div class="jxgpc-article-meta">2026-10-21</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/28160.html" class="jxgpc-article-link">Go如何处理HTTP响应数据_HTTP响应读取方式</a> <div class="jxgpc-article-meta">2026-10-21</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/28153.html" class="jxgpc-article-link">C++实现字符串中单词的顺序反转 _ 结合stringstream与reverse【详解】</a> <div class="jxgpc-article-meta">2026-10-21</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/28145.html" class="jxgpc-article-link">网站当前的在线人数</a> <div class="jxgpc-article-meta">2026-10-21</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/28142.html" class="jxgpc-article-link">代币化美股成热度飙升,“加密券商”颠覆传统券商?</a> <div class="jxgpc-article-meta">2026-10-21</div> </li> <li class="jxgpc-article-item"> <a href="http://www.jxgpc.com/pc/28139.html" class="jxgpc-article-link">netbeans for php 最惯用快捷键整理</a> <div class="jxgpc-article-meta">2026-10-21</div> </li> </ul> </div> <!-- 热门标签 --> <div class="jxgpc-widget"> <h3 class="jxgpc-widget-title">热门标签</h3> <div class="jxgpc-article-tag-list"> <a href="http://www.jxgpc.com/tag/3264" class="jxgpc-article-tag-item">状态码</a> <a href="http://www.jxgpc.com/tag/2683" class="jxgpc-article-tag-item">标准库</a> <a href="http://www.jxgpc.com/tag/1804" class="jxgpc-article-tag-item">json</a> <a href="http://www.jxgpc.com/tag/1803" class="jxgpc-article-tag-item">js</a> <a href="http://www.jxgpc.com/tag/1728" class="jxgpc-article-tag-item">stream</a> <a href="http://www.jxgpc.com/tag/1685" class="jxgpc-article-tag-item">字节</a> <a href="http://www.jxgpc.com/tag/1684" class="jxgpc-article-tag-item">编码</a> <a href="http://www.jxgpc.com/tag/1666" class="jxgpc-article-tag-item">app</a> <a href="http://www.jxgpc.com/tag/1651" class="jxgpc-article-tag-item">golang</a> <a href="http://www.jxgpc.com/tag/1596" class="jxgpc-article-tag-item">html</a> <a href="http://www.jxgpc.com/tag/1590" class="jxgpc-article-tag-item">ai</a> <a href="http://www.jxgpc.com/tag/340" class="jxgpc-article-tag-item">go</a> </div> </div> </aside> </div> </div> </main> <!-- 页脚 --> <footer class="jxgpc-footer"> <div class="jxgpc-container"> <div class="jxgpc-footer-links" style="justify-content: center;"> <a href="http://www.jxgpc.com/22.html" class="jxgpc-footer-link">免责声明</a> <a href="http://www.jxgpc.com/23.html" class="jxgpc-footer-link">关于我们</a> <a href="http://www.jxgpc.com/24.html" class="jxgpc-footer-link">联系我们</a> <a href="http://www.jxgpc.com/25.html" class="jxgpc-footer-link">投诉删除</a> <a href="http://www.jxgpc.com/26.html" class="jxgpc-footer-link">投稿须知</a> </div> <div class="jxgpc-footer-copyright" style="text-align: center; margin-top: 20px;"> Copyright 2026 极星编程网 版权所有 <a href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank" style="color: inherit;">黔ICP备2025063994号</a> </div> <div style="text-align: center; margin-top: 10px; font-size: 12px; color: #999;">免责声明:本站信息来源于网络,仅供学习使用,版权归原作者所有,如有不愿意被转载的情况,请联系QQ:182111886,通知我们删除已转载的信息。</div> </div> </footer> <!-- 返回顶部按钮 --> <button class="jxgpc-back-to-top" id="backToTop" aria-label="返回顶部">⇧</button> <!-- 公共脚本 --> <script src="http://www.jxgpc.com/static/v3/js/common.js"></script> <div class="hide"> </div> </body> </html>