{"id":244465,"date":"2022-06-20T09:34:26","date_gmt":"2022-06-20T01:34:26","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=244465"},"modified":"2022-06-04T03:36:36","modified_gmt":"2022-06-03T19:36:36","slug":"go-copy-ways","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/go-copy-ways.html","title":{"rendered":"\u5728 Go \u4e2d\u590d\u5236\u6587\u4ef6\u7684\u4e09\u79cd\u65b9\u6cd5"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u672c\u6587\u5c06\u4ecb\u7ecd\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u00a0Go \u7f16\u7a0b\u8bed\u8a00<\/a>[1]<\/span>\u00a0\u6765\u590d\u5236\u6587\u4ef6\u3002\u5728 Go \u4e2d\u590d\u5236\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u6211\u53ea\u4ecb\u7ecd\u4e09\u79cd\u6700\u5e38\u89c1\u7684\uff1a\u4f7f\u7528 Go \u5e93\u4e2d\u7684\u00a0io.Copy()<\/code>\u00a0\u51fd\u6570\u8c03\u7528\u3001\u4e00\u6b21\u8bfb\u53d6\u8f93\u5165\u6587\u4ef6\u5e76\u5c06\u5176\u5199\u5165\u53e6\u4e00\u4e2a\u6587\u4ef6\uff0c\u4ee5\u53ca\u4f7f\u7528\u7f13\u51b2\u533a\u4e00\u5757\u5757\u5730\u590d\u5236\u6587\u4ef6\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u65b9\u6cd5\u4e00\uff1a\u4f7f\u7528 io.Copy()<\/strong><\/div>\n

\u7b2c\u4e00\u79cd\u65b9\u6cd5\u5c31\u662f\u4f7f\u7528 Go \u6807\u51c6\u5e93\u7684\u00a0io.Copy()<\/code>\u00a0\u51fd\u6570\u3002\u4f60\u53ef\u4ee5\u5728\u00a0copy()<\/code>\u00a0\u51fd\u6570\u7684\u4ee3\u7801\u4e2d\u627e\u5230\u5b83\u7684\u5b9e\u73b0\u903b\u8f91\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

func copy(src, dst string) (int64, error) {\r\n  sourceFileStat, err := os.Stat(src)\r\n  if err != nil {\r\n    return 0, err\r\n  }\r\n\r\n  if !sourceFileStat.Mode().IsRegular() {\r\n    return 0, fmt.Errorf(\"%s is not a regular file\", src)\r\n  }\r\n\r\n  source, err := os.Open(src)\r\n  if err != nil {\r\n    return 0, err\r\n  }\r\n  defer source.Close()\r\n\r\n  destination, err := os.Create(dst)\r\n  if err != nil {\r\n    return 0, err\r\n  }\r\n  defer destination.Close()\r\n  nBytes, err := io.Copy(destination, source)\r\n    return nBytes, err\r\n  }\r\n<\/code><\/pre>\n

\u9996\u5148\uff0c\u4e0a\u8ff0\u4ee3\u7801\u505a\u4e86\u4e24\u4e2a\u5224\u65ad\uff0c\u4ee5\u4fbf\u786e\u5b9a\u5b83\u53ef\u4ee5\u88ab\u6253\u5f00\u8bfb\u53d6\uff1a\u4e00\u662f\u5224\u65ad\u5c06\u8981\u590d\u5236\u7684\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff08os.Stat(src)<\/code>\uff09\uff0c\u4e8c\u662f\u5224\u65ad\u5b83\u662f\u5426\u4e3a\u5e38\u89c4\u6587\u4ef6\uff08sourceFileStat.Mode().IsRegular()<\/code>\uff09\u3002\u5269\u4e0b\u7684\u6240\u6709\u5de5\u4f5c\u90fd\u7531\u00a0io.Copy(destination, source)<\/code>\u00a0\u8fd9\u884c\u4ee3\u7801\u6765\u5b8c\u6210\u3002io.Copy()<\/code>\u00a0\u51fd\u6570\u6267\u884c\u7ed3\u675f\u540e\uff0c\u4f1a\u8fd4\u56de\u590d\u5236\u7684\u5b57\u8282\u6570\u548c\u590d\u5236\u8fc7\u7a0b\u4e2d\u53d1\u751f\u7684\u7b2c\u4e00\u6761\u9519\u8bef\u6d88\u606f\u3002\u5728 Go \u4e2d\uff0c\u5982\u679c\u6ca1\u6709\u9519\u8bef\u6d88\u606f\uff0c\u9519\u8bef\u53d8\u91cf\u7684\u503c\u5c31\u4e3a\u00a0nil<\/code>\u3002<\/p>\n

\u4f60\u53ef\u4ee5\u5728\u00a0io \u5305<\/a>[2]<\/span>\u00a0\u7684\u6587\u6863\u9875\u9762\u4e86\u89e3\u6709\u5173\u00a0io.Copy()<\/code>\u00a0\u51fd\u6570\u7684\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n

\u8fd0\u884c\u00a0cp1.go<\/code>\u00a0\u5c06\u4ea7\u751f\u4ee5\u4e0b\u8f93\u51fa\uff1a<\/p>\n

$ go run cp1.go\r\nPlease provide two command line arguments!\r\n$ go run cp1.go fileCP.txt \/tmp\/fileCPCOPY\r\nCopied 3826 bytes!\r\n$ diff fileCP.txt \/tmp\/fileCPCOPY\r\n<\/code><\/pre>\n

\u8fd9\u4e2a\u65b9\u6cd5\u5df2\u7ecf\u975e\u5e38\u7b80\u5355\u4e86\uff0c\u4e0d\u8fc7\u5b83\u6ca1\u6709\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u7075\u6d3b\u6027\u3002\u8fd9\u5e76\u4e0d\u603b\u662f\u4e00\u4ef6\u574f\u4e8b\uff0c\u4f46\u662f\uff0c\u6709\u4e9b\u65f6\u5019\uff0c\u5f00\u53d1\u8005\u53ef\u80fd\u4f1a\u9700\u8981\/\u60f3\u8981\u544a\u8bc9\u7a0b\u5e8f\u8be5\u5982\u4f55\u8bfb\u53d6\u6587\u4ef6\u3002<\/p>\n

\u65b9\u6cd5\u4e8c\uff1a\u4f7f\u7528 ioutil.WriteFile() \u548c ioutil.ReadFile()<\/strong><\/div>\n

\u590d\u5236\u6587\u4ef6\u7684\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u00a0ioutil.ReadFile()<\/code>\u00a0\u548c\u00a0ioutil.WriteFile()<\/code>\u00a0\u51fd\u6570\u3002\u7b2c\u4e00\u4e2a\u51fd\u6570\u7528\u4e8e\u5c06\u6574\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u4e00\u6b21\u6027\u5730\u8bfb\u5165\u5230\u67d0\u4e2a\u5185\u5b58\u4e2d\u7684\u5b57\u8282\u5207\u7247\u91cc\uff1b\u7b2c\u4e8c\u4e2a\u51fd\u6570\u5219\u7528\u4e8e\u5c06\u5b57\u8282\u5207\u7247\u7684\u5185\u5bb9\u5199\u5165\u5230\u4e00\u4e2a\u78c1\u76d8\u6587\u4ef6\u4e2d\u3002<\/p>\n

\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

input, err := ioutil.ReadFile(sourceFile)\r\nif err != nil {\r\n  fmt.Println(err)\r\n  return\r\n}\r\n\r\nerr = ioutil.WriteFile(destinationFile, input, 0644)\r\nif err != nil {\r\n  fmt.Println(\"Error creating\", destinationFile)\r\n  fmt.Println(err)\r\n  return\r\n}\r\n<\/code><\/pre>\n

\u4e0a\u8ff0\u4ee3\u7801\u5305\u62ec\u4e86\u4e24\u4e2a\u00a0if<\/code>\u00a0\u4ee3\u7801\u5757\uff08\u55ef\uff0c\u7528 Go \u5199\u7a0b\u5e8f\u5c31\u662f\u8fd9\u6837\u7684\uff09\uff0c\u7a0b\u5e8f\u7684\u5b9e\u9645\u529f\u80fd\u5176\u5b9e\u4f53\u73b0\u5728\u00a0ioutil.ReadFile()<\/code>\u00a0\u548c\u00a0ioutil.WriteFile()<\/code>\u00a0\u8fd9\u4e24\u884c\u4ee3\u7801\u4e2d\u3002<\/p>\n

\u8fd0\u884c\u00a0cp2.go<\/code>\uff0c\u4f60\u4f1a\u5f97\u5230\u4e0b\u9762\u7684\u8f93\u51fa\uff1a<\/p>\n

$ go run cp2.go\r\nPlease provide two command line arguments!\r\n$ go run cp2.go fileCP.txt \/tmp\/copyFileCP\r\n$ diff fileCP.txt \/tmp\/copyFileCP\r\n<\/code><\/pre>\n

\u8bf7\u6ce8\u610f\uff0c\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u5b9e\u73b0\u6587\u4ef6\u590d\u5236\uff0c\u4f46\u5b83\u5728\u590d\u5236\u5927\u6587\u4ef6\u65f6\u7684\u6548\u7387\u53ef\u80fd\u4e0d\u9ad8\u3002\u8fd9\u662f\u56e0\u4e3a\u5f53\u6587\u4ef6\u5f88\u5927\u65f6\uff0cioutil.ReadFile()<\/code>\u00a0\u8fd4\u56de\u7684\u5b57\u8282\u5207\u7247\u4f1a\u5f88\u5927\u3002<\/p>\n

\u65b9\u6cd5\u4e09\uff1a\u4f7f\u7528 os.Read() \u548c os.Write()<\/strong><\/div>\n

\u5728 Go \u4e2d\u590d\u5236\u6587\u4ef6\u7684\u7b2c\u4e09\u79cd\u65b9\u6cd5\u5c31\u662f\u4e0b\u9762\u8981\u4ecb\u7ecd\u7684\u00a0cp3.go<\/code>\u3002\u5b83\u63a5\u53d7\u4e09\u4e2a\u53c2\u6570\uff1a\u8f93\u5165\u6587\u4ef6\u540d\u3001\u8f93\u51fa\u6587\u4ef6\u540d\u548c\u7f13\u51b2\u533a\u5927\u5c0f\u3002<\/p>\n

cp3.go<\/code>\u00a0\u6700\u91cd\u8981\u7684\u90e8\u5206\u4f4d\u4e8e\u4ee5\u4e0b\u00a0for<\/code>\u00a0\u5faa\u73af\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5728\u00a0copy()<\/code>\u00a0\u51fd\u6570\u4e2d\u627e\u5230\u5b83\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

buf := make([]byte, BUFFERSIZE)\r\nfor {\r\n  n, err := source.Read(buf)\r\n  if err != nil && err != io.EOF {\r\n    return err\r\n  }\r\n  if n == 0 {\r\n    break\r\n  }\r\n\r\n  if _, err := destination.Write(buf[:n]); err != nil {\r\n    return err\r\n  }\r\n}\r\n<\/code><\/pre>\n

\u8be5\u65b9\u6cd5\u4f7f\u7528\u00a0os.Read()<\/code>\u00a0\u5c06\u8f93\u5165\u6587\u4ef6\u7684\u4e00\u5c0f\u90e8\u5206\u8bfb\u5165\u540d\u4e3a\u00a0buf<\/code>\u00a0\u7684\u7f13\u51b2\u533a\uff0c\u7136\u540e\u4f7f\u7528\u00a0os.Write()<\/code>\u00a0\u5c06\u8be5\u7f13\u51b2\u533a\u7684\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u3002\u5f53\u8bfb\u53d6\u51fa\u9519\u6216\u5230\u8fbe\u6587\u4ef6\u672b\u5c3e\uff08io.EOF<\/code>\uff09\u65f6\uff0c\u590d\u5236\u8fc7\u7a0b\u5c06\u505c\u6b62\u3002<\/p>\n

\u8fd0\u884c\u00a0cp3.go<\/code>\uff0c\u4f60\u4f1a\u5f97\u5230\u4e0b\u9762\u7684\u8f93\u51fa\uff1a<\/p>\n

$ go run cp3.go\r\nusage: cp3 source destination BUFFERSIZE\r\n$ go run cp3.go fileCP.txt \/tmp\/buf10 10\r\nCopying fileCP.txt to \/tmp\/buf10\r\n$ go run cp3.go fileCP.txt \/tmp\/buf20 20\r\nCopying fileCP.txt to \/tmp\/buf20\r\n<\/code><\/pre>\n

\u5728\u63a5\u4e0b\u6765\u7684\u57fa\u51c6\u6d4b\u8bd5\u4e2d\uff0c\u4f60\u4f1a\u53d1\u73b0\uff0c\u7f13\u51b2\u533a\u7684\u5927\u5c0f\u6781\u5927\u5730\u5f71\u54cd\u4e86\u00a0cp3.go<\/code>\u00a0\u7684\u6027\u80fd\u3002<\/p>\n

\u8fd0\u884c\u57fa\u51c6\u6d4b\u8bd5<\/strong><\/div>\n

\u5728\u672c\u6587\u7684\u6700\u540e\u4e00\u90e8\u5206\uff0c\u6211\u5c06\u5c1d\u8bd5\u6bd4\u8f83\u8fd9\u4e09\u4e2a\u7a0b\u5e8f\u4ee5\u53ca\u00a0cp3.go<\/code>\u00a0\u5728\u4e0d\u540c\u7f13\u51b2\u533a\u5927\u5c0f\u4e0b\u7684\u6027\u80fd\uff08\u4f7f\u7528\u00a0time(1)<\/code>\u00a0\u547d\u4ee4\u884c\u5de5\u5177\uff09\u3002<\/p>\n

\u4ee5\u4e0b\u8f93\u51fa\u663e\u793a\u4e86\u590d\u5236 500MB \u5927\u5c0f\u7684\u6587\u4ef6\u65f6\uff0ccp1.go<\/code>\u3001cp2.go<\/code>\u00a0\u548c\u00a0cp3.go<\/code>\u00a0\u7684\u6027\u80fd\u5bf9\u6bd4\uff1a<\/p>\n

$ ls -l INPUT\r\n-rw-r--r-- \u00a01 mtsouk \u00a0staff \u00a0512000000 Jun \u00a05 09:39 INPUT\r\n$ time go run cp1.go INPUT \/tmp\/cp1\r\nCopied 512000000 bytes!\r\n\r\nreal \u00a0 \u00a00m0.980s\r\nuser \u00a0 \u00a00m0.219s\r\nsys \u00a0 \u00a0 0m0.719s\r\n$ time go run cp2.go INPUT \/tmp\/cp2\r\n\r\nreal \u00a0 \u00a00m1.139s\r\nuser \u00a0 \u00a00m0.196s\r\nsys \u00a0 \u00a0 0m0.654s\r\n$ time go run cp3.go INPUT \/tmp\/cp3 1000000\r\nCopying INPUT to \/tmp\/cp3\r\n\r\nreal \u00a0 \u00a00m1.025s\r\nuser \u00a0 \u00a00m0.195s\r\nsys \u00a0 \u00a0 0m0.486s\r\n<\/code><\/pre>\n

\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\uff0c\u8fd9\u4e09\u4e2a\u7a0b\u5e8f\u7684\u6027\u80fd\u975e\u5e38\u63a5\u8fd1\uff0c\u8fd9\u610f\u5473\u7740 Go \u6807\u51c6\u5e93\u51fd\u6570\u7684\u5b9e\u73b0\u975e\u5e38\u806a\u660e\u3001\u7ecf\u8fc7\u4e86\u5145\u5206\u4f18\u5316\u3002<\/p>\n

\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u6d4b\u8bd5\u4e00\u4e0b\u7f13\u51b2\u533a\u5927\u5c0f\u5bf9\u00a0cp3.go<\/code>\u00a0\u7684\u6027\u80fd\u6709\u4ec0\u4e48\u5f71\u54cd\u5427\uff01\u6267\u884c\u00a0cp3.go<\/code>\uff0c\u5e76\u5206\u522b\u6307\u5b9a\u7f13\u51b2\u533a\u5927\u5c0f\u4e3a 10\u300120 \u548c 1000 \u5b57\u8282\uff0c\u5728\u4e00\u53f0\u8fd0\u884c\u5f88\u5feb\u7684\u673a\u5668\u4e0a\u590d\u5236 500MB \u6587\u4ef6\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n

$ ls -l INPUT\r\n-rw-r--r-- \u00a01 mtsouk \u00a0staff \u00a0512000000 Jun \u00a05 09:39 INPUT\r\n$ time go run cp3.go INPUT \/tmp\/buf10 10\r\nCopying INPUT to \/tmp\/buf10\r\n\r\nreal \u00a0 \u00a06m39.721s\r\nuser \u00a0 \u00a01m18.457s\r\nsys 5m19.186s\r\n$ time go run cp3.go INPUT \/tmp\/buf20 20\r\nCopying INPUT to \/tmp\/buf20\r\n\r\nreal \u00a0 \u00a03m20.819s\r\nuser \u00a0 \u00a00m39.444s\r\nsys 2m40.380s\r\n$ time go run cp3.go INPUT \/tmp\/buf1000 1000\r\nCopying INPUT to \/tmp\/buf1000\r\n\r\nreal \u00a0 \u00a00m4.916s\r\nuser \u00a0 \u00a00m1.001s\r\nsys \u00a0 \u00a0 0m3.986s\r\n<\/code><\/pre>\n

\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0\uff0c\u7f13\u51b2\u533a\u8d8a\u5927\uff0ccp3.go<\/code>\u00a0\u8fd0\u884c\u5f97\u5c31\u8d8a\u5feb\uff0c\u8fd9\u6216\u591a\u6216\u5c11\u662f\u7b26\u5408\u9884\u671f\u7684\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u5c0f\u4e8e 20 \u5b57\u8282\u7684\u7f13\u51b2\u533a\u6765\u590d\u5236\u5927\u6587\u4ef6\u4f1a\u975e\u5e38\u7f13\u6162\uff0c\u5e94\u8be5\u907f\u514d\u3002<\/p>\n

\u4f60\u53ef\u4ee5\u5728\u00a0GitHub<\/a>[3]<\/span>\u00a0\u627e\u5230\u00a0cp1.go<\/code>\u3001cp2.go<\/code>\u00a0\u548c\u00a0cp3.go<\/code>\u00a0\u7684 Go \u4ee3\u7801\u3002<\/p>\n

\u5982\u679c\u4f60\u6709\u4efb\u4f55\u95ee\u9898\u6216\u53cd\u9988\uff0c\u8bf7\u5728\uff08\u539f\u6587\uff09\u4e0b\u65b9\u53d1\u8868\u8bc4\u8bba\u6216\u5728\u00a0Twitter<\/a>[4]<\/span>\u00a0\u4e0a\u4e0e\u6211\uff08\u539f\u4f5c\u8005\uff09\u8054\u7cfb\u3002<\/p>\n


\n

via:\u00a0https:\/\/opensource.com\/article\/18\/6\/copying-files-go<\/a><\/p>\n

\u4f5c\u8005\uff1aMihalis Tsoukalos<\/a>[5]<\/span>\u00a0\u9009\u9898\uff1alkxed<\/a>[6]<\/span>\u00a0\u8bd1\u8005\uff1alkxed<\/a>[7]<\/span>\u00a0\u6821\u5bf9\uff1awxy<\/a>[8]<\/span><\/p>\n

\u672c\u6587\u7531\u00a0LCTT<\/a>[9]<\/span>\u00a0\u539f\u521b\u7f16\u8bd1\uff0cLinux\u4e2d\u56fd<\/a>[10]<\/span>\u00a0\u8363\u8a89\u63a8\u51fa<\/p>\n","protected":false},"excerpt":{"rendered":"

\u7b2c\u4e00\u79cd\u65b9\u6cd5\u5c31\u662f\u4f7f\u7528 Go \u6807\u51c6\u5e93\u7684\u00a0io.Copy()\u00a0\u51fd\u6570\u3002\u4f60\u53ef\u4ee5\u5728\u00a0copy()\u00a0\u51fd\u6570\u7684\u4ee3\u7801\u4e2d\u627e\u5230\u5b83\u7684\u5b9e\u73b0 […]<\/p>\n","protected":false},"author":309,"featured_media":244508,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-244465","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\/244465","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\/309"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=244465"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/244465\/revisions"}],"predecessor-version":[{"id":244476,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/244465\/revisions\/244476"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/244508"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=244465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=244465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=244465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}