{"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":"
\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 |