{"id":207412,"date":"2025-07-08T12:11:25","date_gmt":"2025-07-08T04:11:25","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207412\/"},"modified":"2025-07-08T12:11:25","modified_gmt":"2025-07-08T04:11:25","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8golang%e4%b8%ad%e4%b8%ba%e5%8c%85%e5%90%ab%e9%80%9a%e9%81%93%e3%80%81filewalk%e5%92%8capi%e8%b0%83%e7%94%a8%e7%9a%84%e7%a8%8b%e5%ba%8f%e7%bc%96%e5%86%99%e5%8d%95%e5%85%83","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207412\/","title":{"rendered":"\u5982\u4f55\u5728golang\u4e2d\u4e3a\u5305\u542b\u901a\u9053\u3001filewalk\u548capi\u8c03\u7528\u7684\u7a0b\u5e8f\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\uff1f"},"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>\u5982\u4f55\u5728golang\u4e2d\u4e3a\u5305\u542b\u901a\u9053\u3001filewalk\u548capi\u8c03\u7528\u7684\u7a0b\u5e8f\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 13:12:41<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5728golang\u4e2d\u4e3a\u5305\u542b\u901a\u9053\u3001filewalk\u548capi\u8c03\u7528\u7684\u7a0b\u5e8f\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\uff1f\u300b<\/span>\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u6216\u8005\u662f\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u7684\u7a0b\u5e8f\u6574\u4f53\u5982\u4e0b\u3002<\/p>\n<pre>func main() {\n\n    flag.Parse()\n\n    if *token == \"\" {\n        log.Fatal(Red + \"please provide a client token =&gt; -token={$token}\")\n    }\n\n    tokenSource := oauth2.StaticTokenSource(&amp;oauth2.Token{AccessToken: *token})\n    oauthClient := oauth2.NewClient(context.TODO(), tokenSource)\n    client := putio.NewClient(oauthClient)\n\n    \/\/paths := make(chan string)\n    var wg = new(sync.WaitGroup)\n    for i := 0; i &lt; 50; i++ {\n        wg.Add(1)\n        go worker(paths, wg, client)\n    }\n    WalkFilePath()\n    \/\/if err := filepath.Walk(*rootpath, func(path string, info os.FileInfo, err error) error {\n    \/\/  if err != nil {\n    \/\/      return fmt.Errorf(\"Failed to walk directory: %T %w\", err, err)\n    \/\/  }\n    \/\/  if !info.IsDir() {\n    \/\/      paths &lt;- path\n    \/\/  }\n    \/\/  return nil\n    \/\/}); err != nil {\n    \/\/  panic(fmt.Errorf(\"failed Walk: %w\", err))\n    \/\/}\n    close(paths)\n    wg.Wait()\n}\n\n\/\/ walks the file path and sends paths to channel\nfunc WalkFilePath() {\n    if err := filepath.Walk(*rootpath, func(path string, info os.FileInfo, err error) error {\n        if err != nil {\n            return fmt.Errorf(\"Failed to walk directory: %T %w\", err, err)\n        }\n        if !info.IsDir() {\n            paths &lt;- path\n        }\n        return nil\n    }); err != nil {\n        panic(fmt.Errorf(\"failed Walk: %w\", err))\n    }\n}\n\nfunc worker(paths &lt;-chan string, wg *sync.WaitGroup, client *putio.Client) {\n    defer wg.Done()\n    for path := range paths {\n        f, err := os.Open(path)\n        if err != nil {\n            log.Printf(Red + \"Failed to open file %v for reading\" + Reset, f.Name())\n        }\n        upload, err := client.Files.Upload(context.TODO(), f, path, 0)\n        if err != nil {\n            log.Printf(Red + \"Failed to upload file %v\" + Reset, upload.File.Name)\n        }\n        log.Printf(Green+ \"File %v has been uploaded succesfully\" + Reset, upload.File.Name)\n    }\n}<\/pre>\n<p>\u4ee3\u7801\u662f\u6211\u5199\u7684\u3002\u8fd9\u662f\u6211\u80fd\u505a\u7684\u6700\u5e72\u51c0\u7684\u4e8b\u60c5\uff0c\u6211\u88ab\u544a\u77e5\u4e3a\u8be5\u7a0b\u5e8f\u7f16\u5199\u4e00\u4e2a\u5355\u5143\u6d4b\u8bd5\u3002\u6211\u5f88\u56f0\u60d1\u3002\u4f8b\u5982\uff0c\u8003\u8651 <code>walkfilepath<\/code> \u51fd\u6570\u3002\u6211\u5e94\u8be5\u63d0\u4f9b\u4ec0\u4e48\u4ee5\u53ca\u6211\u5e94\u8be5\u671f\u671b\u6d4b\u8bd5\u8be5\u529f\u80fd\u5f97\u5230\u4ec0\u4e48\u6837\u7684\u7ed3\u679c\u3002\u56e0\u4e3a\u5b83\u5305\u542b\u4e86<code>channel<\/code>\u901a\u8baf\u610f\u4e49<code>goroutines<\/code>\u3002\u6709\u6ca1\u6709\u529e\u6cd5\u6e05\u695a\u5730\u4e3a\u8fd9\u4e2a\u7a0b\u5e8f\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\uff1f\u6216\u8005\u6211\u5e94\u8be5\u66f4\u6539\u4ee3\u7801\u7ed3\u6784\uff0c\u8fd9\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5bf9\u6211\u6765\u8bf4\u4e0d\u597d\u3002\u987a\u4fbf\u8bf4\u4e00\u53e5\uff0c\u7a0b\u5e8f\u8fd0\u884c\u6b63\u5e38\u3002<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u50cf\u5927\u591a\u6570\u4e8b\u60c5\u4e00\u6837\uff0cgo \u5bf9\u5982\u4f55\u6d4b\u8bd5\u975e\u5e38\u6709\u81ea\u5df1\u7684\u770b\u6cd5\u3002\u8bf7\u52a1\u5fc5\u9605\u8bfb<\/p>\n<p><code>walkfilepath<\/code> \u7684\u8f93\u5165<em>\u5e94\u8be5<\/em>\u662f <code>paths<\/code> \u548c <code>rootpath<\/code>\u3002\u60a8\u7684 <code>walkfilepath<\/code> \u65e0\u6cd5\u4ece\u4efb\u4f55\u5730\u65b9\u83b7\u53d6 <code>paths<\/code> \u6216 <code>rootpath<\/code>\uff0c\u56e0\u6b64\u6b64\u4ee3\u7801\u4e0d\u4f1a\u6309\u539f\u6837\u7f16\u8bd1\uff08\u6d4b\u8bd5\u5f53\u7136\u6709\u52a9\u4e8e\u6355\u83b7\u8fd9\u4e9b\u5185\u5bb9\uff09\u3002<\/p>\n<p>\u5bf9 <code>walkfilepath<\/code> \u7684\u6d4b\u8bd5\u53ef\u80fd\u4f1a\u50cf\u8fd9\u6837\u8fdb\u884c\uff1a<\/p>\n<ol>\n<li>\n<p>\u5728\u9879\u76ee\u4e2d\u7684 <code>testdata\/<\/code>\uff08\u76ee\u5f55 \uff09\u4e0b\u521b\u5efa\u6587\u4ef6\u7cfb\u7edf\u7ed3\u6784\u3002\u521b\u5efa\u5b50\u76ee\u5f55\u548c\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u53ef\u80fd\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>testdata\/\n   walktest\/\n      dir1\/\n         file1.txt\n      dir2\/\n          file2.txt\n          dir3\/\n             file3.txt<\/pre>\n<\/li>\n<li>\n<p>\u73b0\u5728\u60a8\u53ef\u4ee5\u5b9a\u4e49\u5c06\u4ece\u9891\u9053\u4e2d\u83b7\u53d6\u7684\u9884\u671f\u6570\u636e\u3002<\/p>\n<pre>expected_paths := []string{\n   \"testdata\/walktest\/dir1\/file1.txt\",\n   \"testdata\/walktest\/dir2\/file2.txt\",\n   \"testdata\/walktest\/dir3\/file3.txt\"\n}<\/pre>\n<\/li>\n<li>\n<p>\u73b0\u5728\u60a8\u9700\u8981\u66f4\u6539 <code>walkfilepath<\/code> \u4ee5\u83b7\u53d6 <code>rootpath<\/code> \u548c <code>paths<\/code> \u7684\u53c2\u6570\u3002<\/p>\n<pre>func walkfilepath(rootdir string, paths chan&lt;- string) {<\/pre>\n<\/li>\n<li>\n<p>\u73b0\u5728\u60a8\u5df2\u51c6\u5907\u597d\u7f16\u5199\u6d4b\u8bd5\u3002<\/p>\n<pre>func TestWalkFilePath(t *testing.T(\n   paths := make(chan string)\n   go WalkFilePath(\"testdata\/walktest\")\n   results := make([]string,0)\n   for path := range paths {\n     results = append(results, path)\n   }\n   exp, res := strings.Join(expected_paths, \"\"), strings.Join(results, \"\")\n   if exp != res {\n     t.Errorf(\"Expected %s got %s\", exp, res)\n   }\n}<\/pre>\n<\/li>\n<\/ol>\n<p>\u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\u4f7f\u7528\u901a\u9053\u548c goroutine \u662f\u5b8c\u5168\u6b63\u5e38\u4e14\u6709\u6548\u7684\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/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-207412","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207412","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=207412"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207412\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}