doukong6031 2016-11-15 01:09
浏览 41
已采纳

在http.Response中使用空白标识符是否足以防止Golang中的内存泄漏?

For example, I am making a post request and I don't need the response object at all.

_, err := http.Post(url, "", &buf)

Am I safe from memory leak in that case? Will the response.Body be drained for proper connection reuse? Or I need to do the usual defer resp.Body.Close() thing?

  • 写回答

1条回答 默认 最新

  • doubi9255 2016-11-15 05:19
    关注

    The application must close the response body to reclaim the resources used by the underlying network connection. To enable reuse of the underlying connection, the application must read the response body before closing.

    Assigning the response to the blank identifier will not close the connection.

    There are no side effects for assignments, including assignments to the blank identifier.

    playground example

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错
  • ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
  • ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出
  • ¥100 matlab2009 32位一直初始化
  • ¥15 Expected type 'str | PathLike[str]…… bytes' instead
  • ¥15 三极管电路求解,已知电阻电压和三级关放大倍数