{"id":873,"date":"2024-11-07T15:09:45","date_gmt":"2024-11-07T07:09:45","guid":{"rendered":"https:\/\/fwq.ai\/blog\/873\/"},"modified":"2024-11-07T15:09:45","modified_gmt":"2024-11-07T07:09:45","slug":"go-%e4%b8%ad%e5%bc%95%e5%85%a5%e8%87%aa%e5%ae%9a%e4%b9%89%e5%8c%85%e5%a4%b1%e8%b4%a5%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88%e6%89%be%e4%b8%8d%e5%88%b0%e5%8c%85%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/873\/","title":{"rendered":"Go \u4e2d\u5f15\u5165\u81ea\u5b9a\u4e49\u5305\u5931\u8d25\uff1a\u4e3a\u4ec0\u4e48\u627e\u4e0d\u5230\u5305\uff1f"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1032\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173028227485832.jpg\" width=\"800\" height=\"320\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173028227485832.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173028227485832-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173028227485832-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173028227485832-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"Go \u4e2d\u5f15\u5165\u81ea\u5b9a\u4e49\u5305\u5931\u8d25\uff1a\u4e3a\u4ec0\u4e48\u627e\u4e0d\u5230\u5305\uff1f\u63d2\u56fe\" alt=\"Go \u4e2d\u5f15\u5165\u81ea\u5b9a\u4e49\u5305\u5931\u8d25\uff1a\u4e3a\u4ec0\u4e48\u627e\u4e0d\u5230\u5305\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5f15\u5165\u81ea\u5b9a\u4e49\u5305\u5931\u8d25\uff1a\u5305\u627e\u4e0d\u5230\u7684\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728 go \u4e2d\u5bfc\u5165\u81ea\u5b9a\u4e49\u5305\u65f6 encountered &#8220;package not found&#8221; \u9519\u8bef\u3002\u5177\u4f53\u9519\u8bef\u4fe1\u606f\u5982\u4e0b\uff1a<\/p>\n<pre>e:codegoproject&gt;go build main.go\nbuild command-line-arguments: cannot find module for path _\/e_\/code\/goproject\/learn1<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0b\u662f\u5bfc\u81f4\u6b64\u95ee\u9898\u7684\u53ef\u80fd\u539f\u56e0\u4ee5\u53ca\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u8def\u5f84\u9519\u8bef<\/strong><\/p>\n<p>\u5c1d\u8bd5\u5f15\u5165\u7684\u5305\u7684\u8def\u5f84\u4e0d\u6b63\u786e\u3002\u786e\u4fdd\u8def\u5f84\u4e0e\u5305\u7684\u5b9e\u9645\u4f4d\u7f6e\u76f8\u5339\u914d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6b63\u786e\u7684\u8def\u5f84\u5e94\u8be5\u662f\uff1a<\/p>\n<pre>import \"learn1\"\nimport \"show2\"<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. gopath \u672a\u8bbe\u7f6e<\/strong><\/p>\n<p>gopath environment variable \u7528\u4e8e\u6307\u793a\u7f16\u8bd1\u5668\u5728\u54ea\u91cc\u67e5\u627e\u5305\u3002\u786e\u4fdd\u5df2\u6b63\u786e\u8bbe\u7f6e gopath \u5e76\u6307\u5411\u5305\u542b\u81ea\u5b9a\u4e49\u5305\u7684\u76ee\u5f55\u3002<\/p>\n<p><strong>3. go modules \u5c1a\u672a\u521d\u59cb\u5316<\/strong><\/p>\n<p>\u5982\u679c\u4f7f\u7528 go \u7248\u672c 1.11 \u6216\u66f4\u9ad8\u7248\u672c\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 go modules \u6765\u7ba1\u7406\u5305\u4f9d\u8d56\u5173\u7cfb\u3002\u786e\u4fdd\u4e3a\u9879\u76ee\u521d\u59cb\u5316\u4e86\u6a21\u5757\uff1a<\/p>\n<pre>go mod init my_module_name<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>4. \u5bfc\u5165\u8def\u5f84\u4e0d\u6b63\u786e<\/strong><\/p>\n<p>\u4ece go 1.11 \u5f00\u59cb\uff0c\u4f7f\u7528 modules\uff0c\u5bfc\u5165\u8def\u5f84\u5fc5\u987b\u4f7f\u7528\u5176\u5b8c\u6574 url\u3002\u5c06\u5176\u66f4\u6539\u4e3a\uff1a<\/p>\n<pre>import \"github.com\/your_username\/project_name\/learn1\"\nimport \"github.com\/your_username\/project_name\/show2\"<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>5. ide \u914d\u7f6e\u9519\u8bef<\/strong><\/p>\n<p>ide \u53ef\u80fd\u4f1a\u5f71\u54cd\u5305\u5bfc\u5165\u3002\u68c0\u67e5 ide \u4e2d\u7684\u9879\u76ee\u8bbe\u7f6e\u4ee5\u786e\u4fdd\u5b83\u5df2\u6b63\u786e\u914d\u7f6e\u4e3a\u67e5\u627e\u81ea\u5b9a\u4e49\u5305\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fGo \u4e2d\u5f15\u5165\u81ea\u5b9a\u4e49\u5305\u5931\u8d25\uff1a\u4e3a\u4ec0\u4e48\u627e\u4e0d\u5230\u5305\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u5165\u81ea\u5b9a\u4e49\u5305\u5931\u8d25\uff1a\u5305\u627e\u4e0d\u5230\u7684\u89e3\u51b3\u65b9\u6cd5 \u5728 go \u4e2d\u5bfc\u5165\u81ea\u5b9a\u4e49\u5305\u65f6 encountered &#8220;package not found&#8221; \u9519\u8bef\u3002\u5177\u4f53\u9519\u8bef\u4fe1\u606f\u5982\u4e0b\uff1a e:codegoproject&gt;go build main.go build command-line-arguments: cannot find module for path _\/e_\/code\/goproject\/learn1 \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0b\u662f\u5bfc\u81f4\u6b64\u95ee\u9898\u7684\u53ef\u80fd\u539f\u56e0\u4ee5\u53ca\u89e3\u51b3\u65b9\u6cd5\uff1a 1. \u8def\u5f84\u9519\u8bef \u5c1d\u8bd5\u5f15\u5165\u7684\u5305\u7684\u8def\u5f84\u4e0d\u6b63\u786e\u3002\u786e\u4fdd\u8def\u5f84\u4e0e\u5305\u7684\u5b9e\u9645\u4f4d\u7f6e\u76f8\u5339\u914d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6b63\u786e\u7684\u8def\u5f84\u5e94\u8be5\u662f\uff1a import &#8220;learn1&#8221; import &#8220;show2&#8221; \u767b\u5f55\u540e\u590d\u5236 2. gopath \u672a\u8bbe\u7f6e gopath environment variable \u7528\u4e8e\u6307\u793a\u7f16\u8bd1\u5668\u5728\u54ea\u91cc\u67e5\u627e\u5305\u3002\u786e\u4fdd\u5df2\u6b63\u786e\u8bbe\u7f6e gopath \u5e76\u6307\u5411\u5305\u542b\u81ea\u5b9a\u4e49\u5305\u7684\u76ee\u5f55\u3002 3. go modules \u5c1a\u672a\u521d\u59cb\u5316 \u5982\u679c\u4f7f\u7528 go \u7248\u672c 1.11 \u6216\u66f4\u9ad8\u7248\u672c\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 go modules \u6765\u7ba1\u7406\u5305\u4f9d\u8d56\u5173\u7cfb\u3002\u786e\u4fdd\u4e3a\u9879\u76ee\u521d\u59cb\u5316\u4e86\u6a21\u5757\uff1a go mod init my_module_name [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-873","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/873","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=873"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/873\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}