{"id":41984,"date":"2024-12-01T08:46:06","date_gmt":"2024-12-01T00:46:06","guid":{"rendered":"https:\/\/fwq.ai\/blog\/41984\/"},"modified":"2024-12-01T08:46:06","modified_gmt":"2024-12-01T00:46:06","slug":"%e6%a3%80%e6%9f%a5%e6%96%87%e4%bb%b6%e6%98%af%e5%90%a6%e9%a9%bb%e7%95%99%e5%9c%a8%e5%9f%ba%e6%9c%ac%e7%9b%ae%e5%bd%95%e4%b8%ad%e7%9a%84%e6%9c%80%e5%ae%89%e5%85%a8%e6%96%b9%e6%b3%95%e6%98%af%e4%bb%80","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/41984\/","title":{"rendered":"\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u9a7b\u7559\u5728\u57fa\u672c\u76ee\u5f55\u4e2d\u7684\u6700\u5b89\u5168\u65b9\u6cd5\u662f\u4ec0\u4e48\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>\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u9a7b\u7559\u5728\u57fa\u672c\u76ee\u5f55\u4e2d\u7684\u6700\u5b89\u5168\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/span><\/p>\n<h1>\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u9a7b\u7559\u5728\u57fa\u672c\u76ee\u5f55\u4e2d\u7684\u6700\u5b89\u5168\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 09:12:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300a\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u9a7b\u7559\u5728\u57fa\u672c\u76ee\u5f55\u4e2d\u7684\u6700\u5b89\u5168\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>Go \u4e2d\u5728\u4efb\u4f55\u5e73\u53f0\u4e0a\u9a8c\u8bc1\u7ed9\u5b9a\u6587\u4ef6\u8def\u5f84\u662f\u5426\u4f4d\u4e8e\u57fa\u672c\u8def\u5f84\u5185\u7684\u6700\u5b89\u5168\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/p>\n<p>\u8def\u5f84\u6700\u521d\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u63d0\u4f9b\uff0c\u5e76\u4f7f\u7528\u201c\/\u201d\u4f5c\u4e3a\u5206\u9694\u7b26\uff0c\u4f46\u5b83\u4eec\u662f\u7528\u6237\u63d0\u4f9b\u7684\uff0c\u6211\u9700\u8981\u5047\u8bbe<em>\u5927\u91cf\u6076\u610f\u8f93\u5165<\/em>\u3002\u6211\u5e94\u8be5\u6267\u884c\u54ea\u79cd\u8def\u5f84\u6807\u51c6\u5316\u4ee5\u786e\u4fdd\u4f8b\u5982\u50cf\u201c..\u201d\u8fd9\u6837\u7684\u5e8f\u5217\u4f1a\u88ab\u8bc4\u4f30\uff0c\u6240\u4ee5\u6211\u53ef\u4ee5\u5b89\u5168\u5730\u68c0\u67e5\u57fa\u672c\u8def\u5f84\uff1f\u5404\u79cd\u6587\u4ef6\u7cfb\u7edf\u548c\u5e73\u53f0\u4e0a\u6709\u54ea\u4e9b\u9700\u8981\u6ce8\u610f\u7684\u4f8b\u5916\u60c5\u51b5\uff1f\u54ea\u4e9b Go \u5e93\u5728\u8fd9\u65b9\u9762\u5e94\u8be5\u662f\u5b89\u5168\u7684\uff1f<\/p>\n<p>\u7ed3\u679c\u5c06\u88ab\u63d0\u4f9b\u7ed9 <code>os.Create<\/code> \u548c <code>sqlite3.Open<\/code> \u7b49\u5916\u90e8\u51fd\u6570\uff0c\u4efb\u4f55\u65e0\u6cd5\u8bc6\u522b\u7559\u4e0b\u7684\u57fa\u672c\u8def\u5f84\u7684\u884c\u4e3a\u90fd\u5c06\u6784\u6210\u5b89\u5168\u8fdd\u89c4\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u6211\u76f8\u4fe1\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>filepath.rel<\/code> \uff08\u5e76\u68c0\u67e5\u5b83\u662f\u5426\u8fd4\u56de\u4e0d\u4ee5 <code>..<\/code> \u5f00\u5934\u7684\u503c\uff09\u3002<\/p>\n<p>rel \u8fd4\u56de\u4e00\u4e2a\u5728\u8bcd\u6cd5\u4e0a\u7b49\u540c\u4e8e targpath \u7684\u76f8\u5bf9\u8def\u5f84 \u5f53\u4f7f\u7528\u4e2d\u95f4\u5206\u9694\u7b26\u8fde\u63a5\u5230\u57fa\u672c\u8def\u5f84\u65f6\u3002\u90a3\u662f\uff0c join(basepath, rel(basepath, targpath)) \u76f8\u5f53\u4e8e targpath \u672c\u8eab\u3002\u6210\u529f\u65f6\uff0c\u8fd4\u56de\u7684\u8def\u5f84\u5c06\u59cb\u7ec8\u76f8\u5bf9\u4e8e \u57fa\u672c\u8def\u5f84\uff0c\u5373\u4f7f\u57fa\u672c\u8def\u5f84\u548c\u76ee\u6807\u8def\u5f84\u4e0d\u5171\u4eab\u4efb\u4f55\u5143\u7d20\u3002\u4e00\u4e2a\u9519\u8bef\u662f \u5982\u679c\u65e0\u6cd5\u4f7f targpath \u76f8\u5bf9\u4e8e\u57fa\u672c\u8def\u5f84\u6216\u8005\u77e5\u9053\u7684\u8bdd\u5219\u8fd4\u56de \u8ba1\u7b97\u5b83\u9700\u8981\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002\u76f8\u5bf9\u503c \u5bf9\u7ed3\u679c\u8c03\u7528 clean\u3002<\/p>\n<p><code>filepath.rel<\/code> \u8fd8\u5728\u5176\u8f93\u5165\u8def\u5f84\u4e0a\u8c03\u7528 <code>filepath.clean<\/code>\uff0c\u89e3\u6790\u4efb\u4f55 <code>.<\/code>s \u548c <code>..<\/code>s\u3002<\/p>\n<p>clean \u8fd4\u56de\u4e0e\u7eaf\u7cb9\u7684\u8def\u5f84\u7b49\u6548\u7684\u6700\u77ed\u8def\u5f84\u540d \u8bcd\u6c47\u5904\u7406\u3002\u5b83\u8fed\u4ee3\u5730\u5e94\u7528\u4ee5\u4e0b\u89c4\u5219\uff0c\u76f4\u5230 \u65e0\u6cd5\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\uff1a<\/p>\n<ol>\n<li>\u5c06\u591a\u4e2a\u5206\u9694\u7b26\u5143\u7d20\u66ff\u6362\u4e3a\u5355\u4e2a\u5206\u9694\u7b26\u5143\u7d20\u3002<\/li>\n<li>\u6d88\u9664\u6bcf\u4e2a .\u8def\u5f84\u540d\u5143\u7d20\uff08\u5f53\u524d\u76ee\u5f55\uff09\u3002<\/li>\n<li>\u6d88\u9664\u6bcf\u4e2a\u5185\u90e8 .. \u8def\u5f84\u540d\u5143\u7d20\uff08\u7236\u76ee\u5f55\uff09\u53ca\u5176\u524d\u9762\u7684\u975e .. \u5143\u7d20\u3002<\/li>\n<li>\u6d88\u9664\u5f00\u59cb\u6839\u8def\u5f84\u7684 .. \u5143\u7d20\uff1a\u5373\u5728\u8def\u5f84\u5f00\u5934\u7528\u201c\/\u201d\u66ff\u6362\u201c\/..\u201d\uff0c\u5047\u8bbe\u5206\u9694\u7b26\u4e3a\u201c\/\u201d\u3002<\/li>\n<\/ol>\n<p>\u60a8\u8fd8\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 <code>filepath.clean<\/code> \u5e76\u5728\u5b8c\u6210\u540e\u68c0\u67e5\u524d\u7f00\u3002\u4ee5\u4e0b\u662f <code>filepath.clean<\/code> \u7684\u4e00\u4e9b\u793a\u4f8b\u8f93\u51fa\uff1a<\/p>\n<pre>ps := []string{\n    \"\/a\/..\/b\",\n    \"\/a\/b\/.\/c\/..\/..\/d\",\n    \"\/b\",\n}\nfor _, p := range ps {\n    fmt.println(p, filepath.clean(p))\n}<\/pre>\n<p>\u6253\u5370\uff1a<\/p>\n<pre>\/a\/..\/b \/b\n\/a\/b\/.\/c\/..\/..\/d \/a\/d\n\/b \/b<\/pre>\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c<strong>\u8def\u5f84\u64cd\u4f5c\u4e0d\u5e94\u8be5\u662f\u60a8\u90e8\u7f72\u7684\u552f\u4e00\u5b89\u5168\u673a\u5236<\/strong>\u3002\u5982\u679c\u60a8\u786e\u5b9e\u62c5\u5fc3\u88ab\u5229\u7528\uff0c\u8bf7\u901a\u8fc7\u6c99\u7bb1\u3001\u521b\u5efa\u865a\u62df\u6587\u4ef6\u7cfb\u7edf\/\u5bb9\u5668\u7b49\u8fdb\u884c\u6df1\u5ea6\u9632\u5fa1\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\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u68c0\u67e5\u6587\u4ef6\u662f\u5426\u9a7b\u7559\u5728\u57fa\u672c\u76ee\u5f55\u4e2d\u7684\u6700\u5b89\u5168\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f \u68c0\u67e5\u6587\u4ef6\u662f\u5426\u9a7b\u7559\u5728\u57fa\u672c\u76ee\u5f55\u4e2d\u7684\u6700\u5b89\u5168\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f \u6765\u6e90\uff1astackoverflow 2024-04-22 09:12:33 0\u6d4f\u89c8 \u6536\u85cf \u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300a\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u9a7b\u7559\u5728\u57fa\u672c\u76ee\u5f55\u4e2d\u7684\u6700\u5b89\u5168\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002 \u95ee\u9898\u5185\u5bb9 Go \u4e2d\u5728\u4efb\u4f55\u5e73\u53f0\u4e0a\u9a8c\u8bc1\u7ed9\u5b9a\u6587\u4ef6\u8def\u5f84\u662f\u5426\u4f4d\u4e8e\u57fa\u672c\u8def\u5f84\u5185\u7684\u6700\u5b89\u5168\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f \u8def\u5f84\u6700\u521d\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u63d0\u4f9b\uff0c\u5e76\u4f7f\u7528\u201c\/\u201d\u4f5c\u4e3a\u5206\u9694\u7b26\uff0c\u4f46\u5b83\u4eec\u662f\u7528\u6237\u63d0\u4f9b\u7684\uff0c\u6211\u9700\u8981\u5047\u8bbe\u5927\u91cf\u6076\u610f\u8f93\u5165\u3002\u6211\u5e94\u8be5\u6267\u884c\u54ea\u79cd\u8def\u5f84\u6807\u51c6\u5316\u4ee5\u786e\u4fdd\u4f8b\u5982\u50cf\u201c..\u201d\u8fd9\u6837\u7684\u5e8f\u5217\u4f1a\u88ab\u8bc4\u4f30\uff0c\u6240\u4ee5\u6211\u53ef\u4ee5\u5b89\u5168\u5730\u68c0\u67e5\u57fa\u672c\u8def\u5f84\uff1f\u5404\u79cd\u6587\u4ef6\u7cfb\u7edf\u548c\u5e73\u53f0\u4e0a\u6709\u54ea\u4e9b\u9700\u8981\u6ce8\u610f\u7684\u4f8b\u5916\u60c5\u51b5\uff1f\u54ea\u4e9b Go \u5e93\u5728\u8fd9\u65b9\u9762\u5e94\u8be5\u662f\u5b89\u5168\u7684\uff1f \u7ed3\u679c\u5c06\u88ab\u63d0\u4f9b\u7ed9 os.Create \u548c sqlite3.Open \u7b49\u5916\u90e8\u51fd\u6570\uff0c\u4efb\u4f55\u65e0\u6cd5\u8bc6\u522b\u7559\u4e0b\u7684\u57fa\u672c\u8def\u5f84\u7684\u884c\u4e3a\u90fd\u5c06\u6784\u6210\u5b89\u5168\u8fdd\u89c4\u3002 \u89e3\u51b3\u65b9\u6848 \u6211\u76f8\u4fe1\u60a8\u53ef\u4ee5\u4f7f\u7528 filepath.rel \uff08\u5e76\u68c0\u67e5\u5b83\u662f\u5426\u8fd4\u56de\u4e0d\u4ee5 .. \u5f00\u5934\u7684\u503c\uff09\u3002 rel \u8fd4\u56de\u4e00\u4e2a\u5728\u8bcd\u6cd5\u4e0a\u7b49\u540c\u4e8e targpath \u7684\u76f8\u5bf9\u8def\u5f84 \u5f53\u4f7f\u7528\u4e2d\u95f4\u5206\u9694\u7b26\u8fde\u63a5\u5230\u57fa\u672c\u8def\u5f84\u65f6\u3002\u90a3\u662f\uff0c join(basepath, rel(basepath, targpath)) \u76f8\u5f53\u4e8e targpath \u672c\u8eab\u3002\u6210\u529f\u65f6\uff0c\u8fd4\u56de\u7684\u8def\u5f84\u5c06\u59cb\u7ec8\u76f8\u5bf9\u4e8e \u57fa\u672c\u8def\u5f84\uff0c\u5373\u4f7f\u57fa\u672c\u8def\u5f84\u548c\u76ee\u6807\u8def\u5f84\u4e0d\u5171\u4eab\u4efb\u4f55\u5143\u7d20\u3002\u4e00\u4e2a\u9519\u8bef\u662f \u5982\u679c\u65e0\u6cd5\u4f7f targpath \u76f8\u5bf9\u4e8e\u57fa\u672c\u8def\u5f84\u6216\u8005\u77e5\u9053\u7684\u8bdd\u5219\u8fd4\u56de \u8ba1\u7b97\u5b83\u9700\u8981\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002\u76f8\u5bf9\u503c \u5bf9\u7ed3\u679c\u8c03\u7528 clean\u3002 filepath.rel \u8fd8\u5728\u5176\u8f93\u5165\u8def\u5f84\u4e0a\u8c03\u7528 filepath.clean\uff0c\u89e3\u6790\u4efb\u4f55 .s \u548c ..s\u3002 clean [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-41984","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41984","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=41984"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41984\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=41984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=41984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=41984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}