{"id":207772,"date":"2025-07-08T12:39:17","date_gmt":"2025-07-08T04:39:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207772\/"},"modified":"2025-07-08T12:39:17","modified_gmt":"2025-07-08T04:39:17","slug":"golng-%e8%8e%b7%e5%8f%96-url-%e5%b9%b6-resp-body-close","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207772\/","title":{"rendered":"golng \u83b7\u53d6 url \u5e76 resp.Body.Close()"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>golng \u83b7\u53d6 url \u5e76 resp.Body.Close()<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-25 15:15:38<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u201c\u7eb5\u6709\u75be\u98ce\u6765\uff0c\u4eba\u751f\u4e0d\u8a00\u5f03\u201d\uff0c\u8fd9\u53e5\u8bdd\u9001\u7ed9\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u7684\u670b\u53cb\u4eec\uff0c\u4e5f\u5e0c\u671b\u5728\u9605\u8bfb\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300agolng \u83b7\u53d6 url \u5e76 resp.Body.Close()\u300b<\/span>\u540e\uff0c\u80fd\u591f\u771f\u7684\u5e2e\u52a9\u5230\u5927\u5bb6\u3002\u6211\u4e5f\u4f1a\u5728\u540e\u7eed\u7684\u6587\u7ae0\u4e2d\uff0c\u9646\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u6280\u672f\u6587\u7ae0\uff0c\u6709\u597d\u7684\u5efa\u8bae\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u975e\u5e38\u611f\u8c22\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5bfb\u627e go \u4ee3\u7801\u6765\u83b7\u53d6 url\uff0c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u662f\u7528\u4e8e\u5728 go \u4e0a\u83b7\u53d6 url \u7684\u4ee3\u7801\uff1a<\/p>\n<pre>func main() {\n    for _, url := range os.Args[1:] {\n        resp, err := http.Get(url)\n        if err != nil {\n            fmt.Fprintf(os.Stderr, \"fetch: %v\\n\", err)\n            os.Exit(1)\n        }\n        b, err := ioutil.ReadAll(resp.Body)\n        resp.Body.Close()\n        if err != nil {\n            fmt.Fprintf(os.Stderr, \"fetch: reading %s: %v\\n\", url, err)\n            os.Exit(1)\n        }\n        fmt.Printf(\"%s\", b)\n    }\n}<\/pre>\n<p>\u6211\u7684\u95ee\u9898\u4e3a\u4ec0\u4e48\u9700\u8981\u8fd9\u91cc resp.body.close() \u4ee5\u53ca\u8fd9\u5230\u5e95\u662f\u505a\u4ec0\u4e48\u7684\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u5982\u679c\u60a8\u6df1\u5165\u7814\u7a76 http \u6587\u6863 <\/p>\n<p>\u7528\u4e8e\u751f\u6210\u54cd\u5e94\u7684 get \u65b9\u6cd5\u662f response<\/p>\n<pre>func (c *client) get(url string) (resp *response, err error)<\/pre>\n<p>\u5728\u54cd\u5e94\u6e90\u4e2d\uff1a<\/p>\n<pre>\/\/ Body represents the response body.\n        \/\/\n        \/\/ The response body is streamed on demand as the Body field\n        \/\/ is read. If the network connection fails or the server\n        \/\/ terminates the response, Body.Read calls return an error.\n        \/\/\n        \/\/ The http Client and Transport guarantee that Body is always\n        \/\/ non-nil, even on responses without a body or responses with\n        \/\/ a zero-length body. It is the caller's responsibility to\n        \/\/ close Body. The default HTTP client's Transport may not\n        \/\/ reuse HTTP\/1.x \"keep-alive\" TCP connections if the Body is\n        \/\/ not read to completion and closed.\n        \/\/\n        \/\/ The Body is automatically dechunked if the server replied\n        \/\/ with a \"chunked\" Transfer-Encoding.\n        Body io.ReadCloser<\/pre>\n<p>\u56e0\u6b64 close() \u6574\u7406\u4e86\u7528\u4e8e\u83b7\u53d6 body \u7684\u8d44\u6e90<\/p>\n<p>\u5982\u679c\u4e0d\u8fd9\u6837\u505a\uff0c\u54cd\u5e94\uff08resp\uff09\u5c06\u65e0\u6cd5\u201c\u4fdd\u6301\u6d3b\u52a8\u201d\uff0c\u6211\u731c\u54cd\u5e94\u4e2d\u7684\u67d0\u4e9b\u8d44\u6e90\u6709\u53ef\u80fd\u65e0\u6cd5\u56de\u6536<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300agolng \u83b7\u53d6 url \u5e76 resp.Body.Close()\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207772","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207772","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207772"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207772\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}