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 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用
  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚