知识卡片

操作参考卡 · 4545.g.8.n

HTTP响应体需要及时关闭

理解等级:中级

HTTP响应包含状态、头和一个可流式读取并关闭的Body。请求成功返回后应检查业务需要的状态,读取所需数据并关闭响应体,释放关联资源;一次性ReadAll只适合已限制大小的内容。网络请求返回无错误不代表业务状态一定成功。

典型次序:resp, err := client.Get(url),确认成功取得响应后defer resp.Body.Close(),再按状态和大小限制读取。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Loose Coupling · 查看原书依据

      There are some useful things in there! Of particular interest is the Body field, which provides access to the HTTP response body. It’s a ReadCloser interface, which tells us two things: that the response body is streamed on demand as it’s read, and that it has a Close method that we’re expected to call. Below

    参考来源

    - 位置:《Cloud Native Go》第08章《Loose Coupling》(来源项目book-sources;源文件:extracted/Cloud Native Go (Matthew A. Titmus)/_epub-src/OEBPS/ch08.xhtml) - 结论依据:HTTP响应包含状态、头和一个可流式读取并关闭的Body。原文相应定义、示例或限制为此结论提供依据;补充纠错与版本边界在正文就近标明。 - 原始内容:There are some useful things in there! Of particular interest is the Body field, which provides access to the HTTP response body. It’s a ReadCloser interface, which tells us two things: that the response body is streamed on demand as it’s read, and that it has a Close method that we’re expected to call. Below