{"id":243312,"date":"2022-06-01T08:26:04","date_gmt":"2022-06-01T00:26:04","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=243312"},"modified":"2022-05-23T14:27:18","modified_gmt":"2022-05-23T06:27:18","slug":"golang-copy-readall","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/golang-copy-readall.html","title":{"rendered":"Golang copy\u4ee3\u66ffReadAll"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u4f7f\u7528 Golang \u5b9e\u73b0\uff0c\u4f7f\u7528 io.Copy \u4ee3\u66ff ioutil.ReadAll\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u9898\u76ee<\/strong><\/div>\n

\u51fd\u6570 \u8c03\u7528 io.Copy(dst, src) \u4f1a\u4ece src \u4e2d\u8bfb\u53d6\u5185\u5bb9\uff0c\u5e76\u5c06\u8bfb\u5230\u7684\u7ed3\u679c\u5199\u5165\u5230 dst \u4e2d\uff0c\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u66ff\u4ee3\u6389\u4f8b\u5b50\u4e2d\u7684 ioutil.ReadAll \u6765\u62f7\u8d1d\u54cd\u5e94 \u7ed3\u6784\u4f53 \u5230 os.Stdout\uff0c\u907f\u514d\u7533\u8bf7\u4e00\u4e2a\u7f13\u51b2\u533a\uff08\u4f8b\u5b50\u4e2d\u7684 b\uff09\u6765\u5b58\u50a8\u3002\u8bb0\u5f97\u5904\u7406 io.Copy \u8fd4\u56de\u7ed3\u679c\u4e2d\u7684\u9519\u8bef\u3002<\/p>\n

\u9898\u76ee\u89e3\u51b3\u601d\u8def<\/strong><\/div>\n

\u6211\u4eec\u53ea\u9700\u8981\u5c06 ioutil.ReadAll \u66ff\u6362\u4e3a io.Copy \u51fd\u6570\u3002<\/p>\n

\u4ee3\u7801\u5177\u4f53\u5b9e\u73b0<\/strong><\/div>\n
\r\npackage main\r\nimport (\r\n\t\"fmt\"\r\n\t\"io\"\r\n\t\"net\/http\"\r\n\t\"os\"\r\n)\r\nfunc main() {\r\n\tfmt.Println(\"\u55e8\u5ba2\u7f51(www.haicoder.net)\")\r\n\tfor _, url := range os.Args[1:] {\r\n\t\tresp, err := http.Get(url)\r\n\t\tif err != nil {\r\n\t\t\tfmt.Fprintf(os.Stderr, \"fetch: %v\\n\", err)\r\n\t\t\tos.Exit(1)\r\n\t\t}\r\n\t\tb, err := io.Copy(os.Stdout, resp.Body)\r\n\t\tresp.Body.Close()\r\n\t\tif err != nil {\r\n\t\t\tfmt.Fprintf(os.Stderr, \"fetch: %v\\n\", err)\r\n\t\t\tos.Exit(1)\r\n\t\t}\r\n\t\tfmt.Printf(\"%s\", b)\r\n\t}\r\n}\r\n<\/pre>\n

\u6211\u4eec\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u884c\uff0c\u5f00\u59cb\u8fd0\u884c\u7a0b\u5e8f\uff1a<\/p>\n

\r\ngo run haicoder.go http:\/\/www.haicoder.net\r\n<\/pre>\n

\u7a0b\u5e8f\u8fd0\u884c\u540e\uff0c\u63a7\u5236\u53f0\u8f93\u51fa\u5982\u4e0b\uff1a
\n\"\"<\/p>\n","protected":false},"excerpt":{"rendered":"

\u51fd\u6570 \u8c03\u7528 io.Copy(dst, src) \u4f1a\u4ece src \u4e2d\u8bfb\u53d6\u5185\u5bb9\uff0c\u5e76\u5c06\u8bfb\u5230\u7684\u7ed3\u679c\u5199\u5165\u5230 dst \u4e2d\uff0c\u4f7f […]<\/p>\n","protected":false},"author":1903,"featured_media":243308,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-243312","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/243312","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=243312"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/243312\/revisions"}],"predecessor-version":[{"id":243317,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/243312\/revisions\/243317"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/243308"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=243312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=243312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=243312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}