{"id":42970,"date":"2024-12-01T08:04:58","date_gmt":"2024-12-01T00:04:58","guid":{"rendered":"https:\/\/fwq.ai\/blog\/42970\/"},"modified":"2024-12-01T08:04:58","modified_gmt":"2024-12-01T00:04:58","slug":"go-%e4%b8%ad%e7%9b%b8%e5%af%b9-url-%e4%b8%8e%e7%bb%9d%e5%af%b9-url-%e7%9a%84%e8%bd%ac%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/42970\/","title":{"rendered":"Go \u4e2d\u76f8\u5bf9 URL \u4e0e\u7edd\u5bf9 URL \u7684\u8f6c\u6362"},"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>Go \u4e2d\u76f8\u5bf9 URL \u4e0e\u7edd\u5bf9 URL \u7684\u8f6c\u6362<\/span><\/p>\n<h1>Go \u4e2d\u76f8\u5bf9 URL \u4e0e\u7edd\u5bf9 URL \u7684\u8f6c\u6362<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-27 22:36:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728Golang\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300aGo \u4e2d\u76f8\u5bf9 URL \u4e0e\u7edd\u5bf9 URL \u7684\u8f6c\u6362\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u7f16\u5199\u4e00\u4e2a\u5c0f\u578b\u7f51\u7edc\u722c\u866b\uff0c\u6211\u6b63\u5728\u722c\u884c\u7684\u7f51\u7ad9\u4e0a\u7684\u8bb8\u591a\u94fe\u63a5\u90fd\u662f\u76f8\u5bf9\u7684\uff08\u4f8b\u5982\uff0c\u5b83\u4eec\u662f <code>\/robots.txt<\/code>\uff09\u3002\u5982\u4f55\u5c06\u8fd9\u4e9b\u76f8\u5bf9\u7f51\u5740\u8f6c\u6362\u4e3a\u7edd\u5bf9\u7f51\u5740\uff08\u4f8b\u5982 <code>\/robots.txt<\/code> =&gt; <code>http:\/\/google.com\/robots.txt<\/code>\uff09\uff1f Go \u6709\u5185\u7f6e\u7684\u65b9\u6cd5\u6765\u505a\u5230\u8fd9\u4e00\u70b9\u5417\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u662f\u7684\uff0c\u6807\u51c6\u5e93\u53ef\u4ee5\u4f7f\u7528 <code>net\/url<\/code> \u5305\u6765\u5b8c\u6210\u6b64\u64cd\u4f5c\u3002\u793a\u4f8b\uff08\u6765\u81ea\u6807\u51c6\u5e93\uff09\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"log\"\n    \"net\/url\"\n)\n\nfunc main() {\n    u, err := url.parse(\"..\/..\/..\/\/search?q=dotnet\")\n    if err != nil {\n        log.fatal(err)\n    }\n    base, err := url.parse(\"http:\/\/example.com\/directory\/\")\n    if err != nil {\n        log.fatal(err)\n    }\n    fmt.println(base.resolvereference(u))\n}<\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0c\u60a8\u53ea\u9700\u89e3\u6790\u7edd\u5bf9 url \u4e00\u6b21\uff0c\u7136\u540e\u5c31\u53ef\u4ee5\u591a\u6b21\u91cd\u590d\u4f7f\u7528\u5b83\u3002 <\/p>\n<p>\u57fa\u4e8e@not_a_golfer \u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p>\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528 <code>base<\/code> url \u7684 <code>parse<\/code> \u65b9\u6cd5\u6765\u63d0\u4f9b\u76f8\u5bf9\u6216\u7edd\u5bf9 url\u3002<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"log\"\n    \"net\/url\"\n)\n\nfunc main() {\n    \/\/ parse only base url\n    base, err := url.Parse(\"http:\/\/example.com\/directory\/\")\n    if err != nil {\n        log.Fatal(err)\n    }\n\n    \/\/ and then use it to parse relative URLs\n    u, err := base.Parse(\"..\/..\/..\/\/search?q=dotnet\")\n    if err != nil {\n        log.Fatal(err)\n    }\n\n    fmt.Println(u.String())\n}\n<\/pre>\n<p>\u62e8\u6253\u8bd5\u8bd5\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aGo \u4e2d\u76f8\u5bf9 URL \u4e0e\u7edd\u5bf9 URL \u7684\u8f6c\u6362\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; Go \u4e2d\u76f8\u5bf9 URL \u4e0e\u7edd\u5bf9 URL \u7684\u8f6c\u6362 Go \u4e2d\u76f8\u5bf9 URL \u4e0e\u7edd\u5bf9 URL \u7684\u8f6c\u6362 \u6765\u6e90\uff1astackoverflow 2024-04-27 22:36:35 0\u6d4f\u89c8 \u6536\u85cf \u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728Golang\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300aGo \u4e2d\u76f8\u5bf9 URL \u4e0e\u7edd\u5bf9 URL \u7684\u8f6c\u6362\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~ \u95ee\u9898\u5185\u5bb9 \u6211\u6b63\u5728\u7f16\u5199\u4e00\u4e2a\u5c0f\u578b\u7f51\u7edc\u722c\u866b\uff0c\u6211\u6b63\u5728\u722c\u884c\u7684\u7f51\u7ad9\u4e0a\u7684\u8bb8\u591a\u94fe\u63a5\u90fd\u662f\u76f8\u5bf9\u7684\uff08\u4f8b\u5982\uff0c\u5b83\u4eec\u662f \/robots.txt\uff09\u3002\u5982\u4f55\u5c06\u8fd9\u4e9b\u76f8\u5bf9\u7f51\u5740\u8f6c\u6362\u4e3a\u7edd\u5bf9\u7f51\u5740\uff08\u4f8b\u5982 \/robots.txt =&gt; http:\/\/google.com\/robots.txt\uff09\uff1f Go \u6709\u5185\u7f6e\u7684\u65b9\u6cd5\u6765\u505a\u5230\u8fd9\u4e00\u70b9\u5417\uff1f \u89e3\u51b3\u65b9\u6848 \u662f\u7684\uff0c\u6807\u51c6\u5e93\u53ef\u4ee5\u4f7f\u7528 net\/url \u5305\u6765\u5b8c\u6210\u6b64\u64cd\u4f5c\u3002\u793a\u4f8b\uff08\u6765\u81ea\u6807\u51c6\u5e93\uff09\uff1a package main import ( &#8220;fmt&#8221; &#8220;log&#8221; &#8220;net\/url&#8221; ) func main() { u, err := url.parse(&#8220;..\/..\/..\/\/search?q=dotnet&#8221;) [&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-42970","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42970","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=42970"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42970\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=42970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=42970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=42970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}