{"id":4139,"date":"2018-10-27T00:02:47","date_gmt":"2018-10-26T16:02:47","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4139"},"modified":"2018-10-27T00:02:47","modified_gmt":"2018-10-26T16:02:47","slug":"go%e8%af%ad%e8%a8%80%e5%ad%a6%e4%b9%a07-%e5%a6%82%e4%bd%95%e8%bf%9b%e8%a1%8c%e7%9b%ae%e5%bd%95%e9%81%8d%e5%8e%86","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4139.html","title":{"rendered":"Go\u8bed\u8a00\u5b66\u4e60#7-\u5982\u4f55\u8fdb\u884c\u76ee\u5f55\u904d\u5386"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u521a\u597d\u6700\u8fd1\u5728\u5b66\u4e60Go\u8bed\u8a00\uff0c\u6240\u4ee5\u60f3\u7740\u770b\u80fd\u4e0d\u80fd\u5b66\u4e60\u4e00\u4e0b\u5982\u4f55\u7528Go\u8bed\u8a00\u8fdb\u884c\u5b9e\u73b0\u2014\u2014\u76ee\u5f55\u904d\u5386\u6709\u54ea\u4e9b\u65b9\u6cd5\uff0c\u4ee5\u53ca\u76f8\u5bf9\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u65b9\u4fbf\u4ee5\u540e\u5feb\u901f\u53c2\u8003\u6539\u5199\u3002<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<p>Go \u8bed\u8a00\u4e2d\u8fdb\u884c\u76ee\u5f55\u904d\u5386\u7684\u539f\u751f\u65b9\u6cd5\u4e3b\u8981\u662f\u4ee5\u4e0b3\u79cd\uff1a<br \/>\nfilepath.Walk()<br \/>\nioutil.ReadDir()<br \/>\nos.File.Readdir()<\/p>\n<p>\u6027\u80fd\u662f\u8d8a\u5e95\u5c42\u8d8a\u9ad8\uff08\u4e0a\u5c42\u5176\u5b9e\u662f\u5bf9\u5e95\u5c42API\u7684\u5c01\u88c5\uff09\u3002<\/p>\n<hr \/>\n<p><span style=\"color: #ff0000;\">filepath.Walk()\u904d\u5386\u6839\u76ee\u5f55(root)\u4e0b\u7684\u6587\u4ef6\u6811\uff0c\u4e3a\u6811\u4e2d\u7684\u6bcf\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55(\u5305\u62ec\u6839\u76ee\u5f55)\u8c03\u7528walkFn\u3002<\/span>\u6240\u6709\u5728\u8bbf\u95ee\u6587\u4ef6\u548c\u76ee\u5f55\u65f6\u51fa\u73b0\u7684\u9519\u8bef\u90fd\u7531walkFn\u8fc7\u6ee4\u3002\u904d\u5386\u6309\u8bcd\u6cd5\u987a\u5e8f\u8fdb\u884c\uff0c\u8fd9\u4f7f\u5f97\u8f93\u51fa\u662f\u786e\u5b9a\u7684\uff0c\u4f46\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u76ee\u5f55\u6765\u8bf4\uff0c\u904d\u5386\u53ef\u80fd\u662f\u4f4e\u6548\u7684\u3002filepath.Walk()\u4e0d\u4f1a\u8ddf\u8fdb\u7b26\u53f7\u94fe\u63a5\u3002<\/p>\n<pre class=\"lang:default decode:true \">package main\r\n\r\nimport (\r\n    \"flag\"\r\n    \"fmt\"\r\n    \"os\"\r\n    \"path\/filepath\"\r\n)\r\n\r\n\/\/ https:\/\/rosettacode.org\/wiki\/Walk_a_directory\/Recursively#Go\r\n\r\nconst (\r\n    layout = \"2006-01-02 15:04:05\"\r\n)\r\n\r\nfunc VisitFile(fp string, fi os.FileInfo, err error) error {\r\n    if err != nil {\r\n        fmt.Println(err) \/\/ can't walk here,\r\n        return nil       \/\/ but continue walking elsewhere\r\n    }\r\n    if fi.IsDir() {\r\n        return nil \/\/ not a file.  ignore.\r\n    }\r\n    \/\/ \u8fc7\u6ee4\u8f93\u51fa\u5185\u5bb9\r\n    matched, err := filepath.Match(\"*.txt\", fi.Name())\r\n    if err != nil {\r\n        fmt.Println(err) \/\/ malformed pattern\r\n        return err       \/\/ this is fatal.\r\n    }\r\n    if matched {\r\n        \/\/ fmt.Println(fp)\r\n        fmt.Printf(\"Name: %s, ModifyTime: %s, Size: %v\\n\", fp, fi.ModTime().Format(layout), fi.Size())\r\n    }\r\n    return nil\r\n}\r\n\r\nfunc main() {\r\n    var path = flag.String(\"path\", \".\", \"The path to traverse.\")\r\n    flag.Parse()\r\n\r\n    filepath.Walk(*path, VisitFile)\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\"><strong>filepath.Walk()\u4f1a\u81ea\u52a8\u904d\u5386\u5b50\u76ee\u5f55<\/strong><\/span>\uff0c\u4f46\u6709\u4e9b\u65f6\u5019\u6211\u4eec\u4e0d\u5e0c\u671b\u8fd9\u6837\uff0c<span style=\"color: #ff0000;\">\u5982\u679c\u53ea\u60f3\u770b\u5f53\u524d\u76ee\u5f55\uff0c\u6216\u624b\u52a8\u6307\u5b9a\u67d0\u51e0\u7ea7\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\uff0c\u8fd9\u4e2a\u65f6\u5019\uff0c\u53ef\u4ee5\u4f7f\u7528 ioutil.ReadDir \u8fdb\u884c\u66ff\u4ee3\u3002<\/span><\/p>\n<pre class=\"lang:default decode:true\">package main\r\n\r\nimport (\r\n    \"flag\"\r\n    \"fmt\"\r\n    \"io\/ioutil\"\r\n    \"log\"\r\n)\r\n\r\nfunc main() {\r\n    var path = flag.String(\"path\", \".\", \"The path to traverse.\")\r\n    flag.Parse()\r\n\r\n    files, err := ioutil.ReadDir(*path)\r\n    if err != nil {\r\n        log.Fatal(err)\r\n    }\r\n\r\n    for _, file := range files {\r\n        fmt.Println(file.Name())\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u51e0\u4e2a\u65b9\u6cd5\u5c01\u88c5\u7684\u4e00\u4e2a\u6f14\u793a\u548c\u5bf9\u6bd4\uff1a<\/p>\n<pre class=\"lang:default decode:true\">package main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"io\/ioutil\"\r\n    \"os\"\r\n    \"path\/filepath\"\r\n)\r\n\r\n\/\/ https:\/\/stackoverflow.com\/questions\/14668850\/list-directory-in-go\/49196644#49196644\r\n\r\nfunc main() {\r\n    var (\r\n        root  string\r\n        files []string\r\n        err   error\r\n    )\r\n\r\n    \/\/ root = \"\/home\/manigandan\/Desktop\/Manigandan\/sample\"\r\n    root = \".\"\r\n    \/\/ filepath.Walk\r\n    files, err = FilePathWalkDir(root)\r\n    if err != nil {\r\n        panic(err)\r\n    }\r\n    \/\/ ioutil.ReadDir\r\n    files, err = IOReadDir(root)\r\n    if err != nil {\r\n        panic(err)\r\n    }\r\n    \/\/os.File.Readdir\r\n    files, err = OSReadDir(root)\r\n    if err != nil {\r\n        panic(err)\r\n    }\r\n\r\n    for _, file := range files {\r\n        fmt.Println(file)\r\n    }\r\n}\r\n\r\nfunc FilePathWalkDir(root string) ([]string, error) {\r\n    var files []string\r\n    err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {\r\n        if !info.IsDir() {\r\n            files = append(files, path)\r\n        }\r\n        return nil\r\n    })\r\n    return files, err\r\n}\r\n\r\nfunc IOReadDir(root string) ([]string, error) {\r\n    var files []string\r\n    fileInfo, err := ioutil.ReadDir(root)\r\n    if err != nil {\r\n        return files, err\r\n    }\r\n\r\n    for _, file := range fileInfo {\r\n        files = append(files, file.Name())\r\n    }\r\n    return files, nil\r\n}\r\n\r\nfunc OSReadDir(root string) ([]string, error) {\r\n    var files []string\r\n    f, err := os.Open(root)\r\n    if err != nil {\r\n        return files, err\r\n    }\r\n    fileInfo, err := f.Readdir(-1)\r\n    f.Close()\r\n    if err != nil {\r\n        return files, err\r\n    }\r\n\r\n    for _, file := range fileInfo {\r\n        files = append(files, file.Name())\r\n    }\r\n    return files, nil\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"https:\/\/rosettacode.org\/wiki\/Walk_a_directory\/Recursively#Go\">https:\/\/rosettacode.org\/wiki\/Walk_a_directory\/Recursively#Go<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/14668850\/list-directory-in-go\/49196644#49196644\">https:\/\/stackoverflow.com\/questions\/14668850\/list-directory-in-go\/49196644#49196644<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u521a\u597d\u6700\u8fd1\u5728\u5b66\u4e60Go\u8bed\u8a00\uff0c\u6240\u4ee5\u60f3\u7740\u770b\u80fd\u4e0d\u80fd\u5b66\u4e60\u4e00\u4e0b\u5982\u4f55\u7528Go\u8bed\u8a00\u8fdb\u884c\u5b9e\u73b0\u2014\u2014\u76ee\u5f55\u904d\u5386\u6709\u54ea [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[1238,564,1234,187],"class_list":["post-4139","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-filepath-walk","tag-golang","tag-ioutil","tag-readdir"],"views":5847,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4139"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4139\/revisions"}],"predecessor-version":[{"id":4140,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4139\/revisions\/4140"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}