{"id":503,"date":"2020-04-07T15:39:28","date_gmt":"2020-04-07T07:39:28","guid":{"rendered":"http:\/\/124.244.86.40:8080\/wordpress\/?p=503"},"modified":"2020-04-07T15:39:48","modified_gmt":"2020-04-07T07:39:48","slug":"golang-%e5%9c%a8-filepath-glob-%e8%a3%a1%e4%bd%bf%e7%94%a8%e6%96%b9%e6%8b%ac%e8%99%9f%ef%bc%88-%e8%b7%9f-%ef%bc%89","status":"publish","type":"post","link":"https:\/\/imuslab.com\/wordpress\/2020\/04\/07\/golang-%e5%9c%a8-filepath-glob-%e8%a3%a1%e4%bd%bf%e7%94%a8%e6%96%b9%e6%8b%ac%e8%99%9f%ef%bc%88-%e8%b7%9f-%ef%bc%89\/","title":{"rendered":"Golang \u5728 filepath.Glob \u88e1\u4f7f\u7528\u542b\u65b9\u62ec\u865f\uff08[ \u8ddf ]\uff09\u7684\u8def\u5f91"},"content":{"rendered":"\n<p>\u7531\u65bc Golang \u8a9e\u8a00\u9650\u5236\uff0cfilepath.Glob \u88e1\u9762\u6709\u4e00\u4e9b\u5b57\u5143\u662f\u4e0d\u80fd\u7528\u7684\uff0c\u4f8b\u5982\u8aaa [ \u8ddf ] \u5c31\u662f\u5176\u4e2d\u4e4b\u4e00\uff08\u9084\u6709 * \u865f\uff0c\u4f46\u662f\u5927\u90e8\u5206\u4f5c\u696d\u7cfb\u7d71\u90fd\u4e0d\u6703\u8b93\u4f60\u628a * \u865f\u7576\u4f5c\u8cc7\u6599\u593e\u540d\u7a31\uff0c\u6240\u4ee5\u5728\u9019\u88e1\u5c31\u4e0d\u4f5c\u8655\u7406\u4e86\u3002\u4e00\u822c\u4f86\u8aaa\uff0c\u5982\u679c\u4f60\u8981\u6253\u958b\u4e00\u500b\u5305\u542b [ ] \u7684\u8def\u5f91\uff0c<code> filepath.Glob<\/code> \u6703\u56de\u50b3\u7a7a\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>path := \"mymusic[mp3]\/*\"\nfiles, err := filepath.Glob(path);\n\/\/ files \u6703\u56de\u50b3\u7a7a\u503c<\/code><\/pre>\n\n\n\n<p>\u6240\u4ee5\uff0c\u5f88\u591a\u5de5\u7a0b\u5e2b\u5c31\u5e79\u8106\u4e0d\u5728\u8cc7\u6599\u593e\u540d\u7a31\u88e1\u9762\u4f7f\u7528 [ \u8ddf ]\uff0c\u4f46\u662f\u4f5c\u70ba\u958b\u767c\u7db2\u9801\u7248\u7684\u6a94\u6848\u7ba1\u7406\u5668\uff0c\u7531\u65bc\u4f5c\u70ba\u5de5\u7a0b\u5e2b\u662f\u7121\u6cd5\u9810\u8a08\u7528\u6236\u7684\u4f7f\u7528\u65b9\u5f0f\uff0c\u6211\u5011\u5fc5\u5c07\u8981\u5728 Glob \u88e1\u9762\u652f\u63f4 [ \u8ddf ]\uff0c\u800c\u65b9\u6cd5\u53ef\u80fd\u6bd4\u4f60\u60f3\u50cf\u4e2d\u7684\u7c21\u55ae\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>realpath := \"mymusic[mp3]\"\nfiles, _ := filepath.Glob(realpath + \"*\")\n\/\/\u9019\u88e1\u7684 files \u6703\u662f\u7a7a\u503c\uff0c\u5beb\u5728\u9019\u88e1\u662f\u70ba\u4e86\u5728\u4e0b\u9762\u7701\u4e0b\u4e00\u500b else \u7684 case\nif (strings.Contains(realpath, \"[\") == true || strings.Contains(realpath, \"]\") == true){\n\tif (len(files) == 0){\n\t\t\/\/\u7279\u5225\u8655\u7406\u6a21\u5f0f\uff0c\u4ee5 * \u865f\u53d6\u4ee3 [ \u8ddf ] \n\t\tnewSearchPath := strings.ReplaceAll(realpath, \"[\",\"*\")\n\t\tnewSearchPath = strings.ReplaceAll(newSearchPath, \"]\",\"*\")\n\t\t\/\/\u6383\u63cf\u8207\u8f38\u5165\u8def\u5f91\u76f8\u8fd1\u7684\u8def\u5f91\u540d\u7a31\n\t\ttmpFilelist, _ := filepath.Glob(newSearchPath + \"*\")\n                \/\/\u5728\u6bcf\u500b\u76f8\u8fd1\u8def\u5f91\u4e2d\uff0c\u627e\u51fa\u64c1\u6709\u6b63\u78ba\u8def\u5f91\u7684\u6a94\u6848\n\t\tfor _, file := range tmpFilelist{\n\t\t\tfile = filepath.ToSlash(file)\n\t\t\tif strings.Contains(file, realpath){\n\t\t\t\tfiles = append(files, file)\n\t\t\t}\n\t\t}\n\t}\n}\n\/\/\u6b63\u78ba\u56de\u50b3\nlog.Println(files)<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7531\u65bc Golang \u8a9e\u8a00\u9650\u5236\uff0cfilepath.Glob \u88e1\u9762\u6709\u4e00\u4e9b\u5b57\u5143\u662f\u4e0d\u80fd\u7528\u7684\uff0c\u4f8b\u5982\u8aaa [ \u8ddf ] \u5c31\u662f\u5176\u4e2d\u4e4b\u4e00\uff08\u9084\u6709 * \u865f\uff0c\u4f46\u662f\u5927\u90e8\u5206\u4f5c\u696d\u7cfb\u7d71\u90fd\u4e0d\u6703\u8b93\u4f60\u628a * \u865f\u7576\u4f5c\u8cc7\u6599\u593e\u540d\u7a31\uff0c\u6240\u4ee5\u5728\u9019\u88e1\u5c31\u4e0d\u4f5c\u8655\u7406\u4e86\u3002\u4e00\u822c\u4f86\u8aaa\uff0c\u5982\u679c\u4f60\u8981\u6253\u958b\u4e00\u500b\u5305\u542b [ ] \u7684\u8def\u5f91\uff0c filepath.Glob \u6703\u56de\u50b3\u7a7a\u503c\u3002 \u6240\u4ee5\uff0c\u5f88\u591a\u5de5\u7a0b\u5e2b\u5c31\u5e79\u8106\u4e0d\u5728\u8cc7\u6599\u593e\u540d\u7a31\u88e1\u9762\u4f7f\u7528 [ \u8ddf ]\uff0c\u4f46\u662f\u4f5c\u70ba\u958b\u767c\u7db2\u9801\u7248\u7684\u6a94\u6848\u7ba1\u7406\u5668\uff0c\u7531\u65bc\u4f5c\u70ba\u5de5\u7a0b\u5e2b\u662f\u7121\u6cd5\u9810\u8a08\u7528\u6236\u7684\u4f7f\u7528\u65b9\u5f0f\uff0c\u6211\u5011\u5fc5\u5c07\u8981\u5728 Glob \u88e1\u9762\u652f\u63f4 [ \u8ddf ]\uff0c\u800c\u65b9\u6cd5\u53ef\u80fd\u6bd4\u4f60\u60f3\u50cf\u4e2d\u7684\u7c21\u55ae\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[94,95,65,89],"class_list":["post-503","post","type-post","status-publish","format-standard","hentry","category-instructions-and-information","tag-94","tag-95","tag-glob","tag-golang"],"_links":{"self":[{"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/posts\/503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/comments?post=503"}],"version-history":[{"count":2,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/posts\/503\/revisions"}],"predecessor-version":[{"id":505,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/posts\/503\/revisions\/505"}],"wp:attachment":[{"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/media?parent=503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/categories?post=503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/tags?post=503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}