{"id":6444,"date":"2023-02-22T21:14:45","date_gmt":"2023-02-22T13:14:45","guid":{"rendered":"https:\/\/i007.vip:15443\/wordpress\/?p=6444"},"modified":"2023-02-22T21:14:45","modified_gmt":"2023-02-22T13:14:45","slug":"go-go-mod%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/6444","title":{"rendered":"Go go.mod\u8be6\u89e3"},"content":{"rendered":"<div class=\"article-header-box\">\n<div class=\"article-header\">\n<div class=\"article-title-box\">\n<p id=\"articleContentId\" class=\"title-article\"><a href=\"https:\/\/blog.csdn.net\/qq_39852676\/article\/details\/121132613\">\u539f\u6587\u5730\u5740<\/a><\/p>\n<\/div>\n<div class=\"article-info-box\">\n<div class=\"article-bar-top\"><img decoding=\"async\" class=\"article-type-img\" src=\"https:\/\/csdnimg.cn\/release\/blogv2\/dist\/pc\/img\/reprint.png\" alt=\"\" \/><\/div>\n<\/div>\n<\/div>\n<\/div>\n<article class=\"baidu_pl\">\n<div id=\"article_content\" class=\"article_content clearfix\">\n<div id=\"content_views\" class=\"markdown_views prism-atom-one-dark\">\n<h2><a name=\"t0\"><\/a><a id=\"gomod_0\"><\/a>\u4ec0\u4e48\u662fgo.mod?<\/h2>\n<p>Go.mod\u662fGolang1.11\u7248\u672c\u65b0\u5f15\u5165\u7684\u5b98\u65b9\u5305\u7ba1\u7406\u5de5\u5177\u7528\u4e8e\u89e3\u51b3\u4e4b\u524d\u6ca1\u6709\u5730\u65b9\u8bb0\u5f55\u4f9d\u8d56\u5305\u5177\u4f53\u7248\u672c\u7684\u95ee\u9898\uff0c\u65b9\u4fbf\u4f9d\u8d56\u5305\u7684\u7ba1\u7406\u3002<\/p>\n<p>Go.mod\u5176\u5b9e\u5c31\u662f\u4e00\u4e2aModules\uff0c\u5173\u4e8eModules\u7684\u5b98\u65b9\u5b9a\u4e49\u4e3a\uff1a<\/p>\n<p>Modules\u662f\u76f8\u5173Go\u5305\u7684\u96c6\u5408\uff0c\u662f\u6e90\u4ee3\u7801\u4ea4\u6362\u548c\u7248\u672c\u63a7\u5236\u7684\u5355\u5143\u3002go\u547d\u4ee4\u76f4\u63a5\u652f\u6301\u4f7f\u7528Modules\uff0c\u5305\u62ec\u8bb0\u5f55\u548c\u89e3\u6790\u5bf9\u5176\u4ed6\u6a21\u5757\u7684\u4f9d\u8d56\u6027\u3002Modules\u66ff\u6362\u65e7\u7684\u57fa\u4e8eGOPATH\u7684\u65b9\u6cd5\uff0c\u6765\u6307\u5b9a\u4f7f\u7528\u54ea\u4e9b\u6e90\u6587\u4ef6\u3002<\/p>\n<p>Modules\u548c\u4f20\u7edf\u7684GOPATH\u4e0d\u540c\uff0c\u4e0d\u9700\u8981\u5305\u542b\u4f8b\u5982src\uff0cbin\u8fd9\u6837\u7684\u5b50\u76ee\u5f55\uff0c\u4e00\u4e2a\u6e90\u4ee3\u7801\u76ee\u5f55\u751a\u81f3\u662f\u7a7a\u76ee\u5f55\u90fd\u53ef\u4ee5\u4f5c\u4e3aModules\uff0c\u53ea\u8981\u5176\u4e2d\u5305\u542b\u6709go.mod\u6587\u4ef6\u3002<\/p>\n<h2><a name=\"t1\"><\/a><a id=\"gomod_11\"><\/a>\u5982\u4f55\u4f7f\u7528go.mod?<\/h2>\n<ol>\n<li>\u9996\u5148\u5c06go\u7684\u7248\u672c\u5347\u7ea7\u4e3a1.11\u4ee5\u4e0a<\/li>\n<li>\u8bbe\u7f6eGO111MODULE<\/li>\n<\/ol>\n<h2><a name=\"t2\"><\/a><a id=\"GO111MODULE_17\"><\/a>GO111MODULE<\/h2>\n<p>GO111MODULE\u6709\u4e09\u4e2a\u503c\uff1aoff, on\u548cauto\uff08\u9ed8\u8ba4\u503c\uff09\u3002<\/p>\n<ul>\n<li>GO111MODULE=off\uff0cgo\u547d\u4ee4\u884c\u5c06\u4e0d\u4f1a\u652f\u6301module\u529f\u80fd\uff0c\u5bfb\u627e\u4f9d\u8d56\u5305\u7684\u65b9\u5f0f\u5c06\u4f1a\u6cbf\u7528\u65e7\u7248\u672c\u90a3\u79cd\u901a\u8fc7vendor\u76ee\u5f55\u6216\u8005GOPATH\u6a21\u5f0f\u6765\u67e5\u627e\u3002<\/li>\n<li>GO111MODULE=on\uff0cgo\u547d\u4ee4\u884c\u4f1a\u4f7f\u7528modules\uff0c\u800c\u4e00\u70b9\u4e5f\u4e0d\u4f1a\u53bbGOPATH\u76ee\u5f55\u4e0b\u67e5\u627e\u3002<\/li>\n<li>GO111MODULE=auto\uff0c\u9ed8\u8ba4\u503c\uff0cgo\u547d\u4ee4\u884c\u5c06\u4f1a\u6839\u636e\u5f53\u524d\u76ee\u5f55\u6765\u51b3\u5b9a\u662f\u5426\u542f\u7528module\u529f\u80fd\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u53ef\u4ee5\u5206\u4e3a\u4e24\u79cd\u60c5\u5f62\uff1a<\/li>\n<\/ul>\n<p>1\u3001\u5f53\u524d\u76ee\u5f55\u5728GOPATH\/src\u4e4b\u5916\u4e14\u8be5\u76ee\u5f55\u5305\u542bgo.mod\u6587\u4ef6<br \/>\n2\u3001\u5f53\u524d\u6587\u4ef6\u5728\u5305\u542bgo.mod\u6587\u4ef6\u7684\u76ee\u5f55\u4e0b\u9762\u3002<\/p>\n<h2><a name=\"t3\"><\/a><a id=\"go_mod_28\"><\/a>go mod\u547d\u4ee4\uff1a<\/h2>\n<p><a class=\"hl hl-1\" href=\"https:\/\/so.csdn.net\/so\/search?q=golang&#038;spm=1001.2101.3001.7020\" target=\"_blank\" rel=\"noopener\" data-tit=\"golang\" data-pretit=\"golang\">golang<\/a>\u00a0\u63d0\u4f9b\u4e86 go mod\u547d\u4ee4\u6765\u7ba1\u7406\u5305\u3002go mod \u6709\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img-blog.csdnimg.cn\/e5b3ec3d25ed4210bd51077183797026.png?x-oss-process=image\/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA57ub5rSe6Iqx5Li75pWP5piO,size_20,color_FFFFFF,t_70,g_se,x_16\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h2><a name=\"t4\"><\/a><a id=\"gomod_34\"><\/a>go.mod\u5982\u4f55\u5728\u9879\u76ee\u4e2d\u4f7f\u7528\uff1f<\/h2>\n<p>1.\u9996\u5148\u6211\u4eec\u8981\u5728GOPATH\/src \u76ee\u5f55\u4e4b\u5916\u65b0\u5efa\u5de5\u7a0b\uff0c\u6216\u5c06\u8001\u5de5\u7a0bcopy\u5230GOPATH\/src \u76ee\u5f55\u4e4b\u5916\u3002<\/p>\n<p>PS\uff1ago.mod\u6587\u4ef6\u4e00\u65e6\u521b\u5efa\u540e\uff0c\u5b83\u7684\u5185\u5bb9\u5c06\u4f1a\u88abgo toolchain\u5168\u9762\u638c\u63a7\u3002go toolchain\u4f1a\u5728\u5404\u7c7b\u547d\u4ee4\u6267\u884c\u65f6\uff0c\u6bd4\u5982go get\u3001go build\u3001go mod\u7b49\u4fee\u6539\u548c\u7ef4\u62a4go.mod\u6587\u4ef6\u3002<\/p>\n<p>go.mod \u6587\u4ef6\u5185\u63d0\u4f9b\u4e86<a class=\"hl hl-1\" href=\"https:\/\/so.csdn.net\/so\/search?q=module&#038;spm=1001.2101.3001.7020\" target=\"_blank\" rel=\"noopener\" data-tit=\"module\" data-pretit=\"module\">module<\/a>, require\u3001replace\u548cexclude\u56db\u4e2a\u5173\u952e\u5b57\uff0c\u8fd9\u91cc\u6ce8\u610f\u533a\u5206\u4e0e\u4e0a\u8868\u4e2d\u547d\u4ee4\u7684\u533a\u522b\uff0c\u4e00\u4e2a\u662f\u7ba1\u7406go mod\u547d\u4ee4\uff0c\u4e00\u4e2a\u662fgo mod\u6587\u4ef6\u5185\u7684\u5173\u952e\u5b57\u3002<\/p>\n<ul>\n<li>module\u8bed\u53e5\u6307\u5b9a\u5305\u7684\u540d\u5b57\uff08\u8def\u5f84\uff09<\/li>\n<li>require\u8bed\u53e5\u6307\u5b9a\u7684\u4f9d\u8d56\u9879\u6a21\u5757<\/li>\n<li>replace\u8bed\u53e5\u53ef\u4ee5\u66ff\u6362\u4f9d\u8d56\u9879\u6a21\u5757<\/li>\n<li>exclude\u8bed\u53e5\u53ef\u4ee5\u5ffd\u7565\u4f9d\u8d56\u9879\u6a21\u5757<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u662f\u6211\u4eec\u5efa\u7acb\u4e86\u4e00\u4e2ahello.go\u7684\u6587\u4ef6<\/p>\n<pre class=\"prettyprint\" data-index=\"0\"><code class=\"has-numbering\">package main\r\n \r\nimport (\r\n\t\"fmt\"\r\n)\r\n \r\nfunc main() {\r\n    fmt.Println(\"Hello, world!\")\r\n}\r\n<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<\/ul>\n<p>2.\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\uff0c\u547d\u4ee4\u884c\u8fd0\u884c go mod init + \u6a21\u5757\u540d\u79f0 \u521d\u59cb\u5316\u6a21\u5757<\/p>\n<p>\u5373<code>go mod init hello<\/code><\/p>\n<p>\u8fd0\u884c\u5b8c\u4e4b\u540e\uff0c\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u751f\u6210\u4e00\u4e2ago.mod\u6587\u4ef6\uff0c\u8fd9\u662f\u4e00\u4e2a\u5173\u952e\u6587\u4ef6\uff0c\u4e4b\u540e\u7684\u5305\u7684\u7ba1\u7406\u90fd\u662f\u901a\u8fc7\u8fd9\u4e2a\u6587\u4ef6\u7ba1\u7406\u3002<\/p>\n<p>\u5b98\u65b9\u8bf4\u660e\uff1a\u9664\u4e86go.mod\u4e4b\u5916\uff0cgo\u547d\u4ee4\u8fd8\u7ef4\u62a4\u4e00\u4e2a\u540d\u4e3ago.sum\u7684\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u7279\u5b9a\u6a21\u5757\u7248\u672c\u5185\u5bb9\u7684\u9884\u671f\u52a0\u5bc6\u54c8\u5e0c go\u547d\u4ee4\u4f7f\u7528go.sum\u6587\u4ef6\u786e\u4fdd\u8fd9\u4e9b\u6a21\u5757\u7684\u672a\u6765\u4e0b\u8f7d\u68c0\u7d22\u4e0e\u7b2c\u4e00\u6b21\u4e0b\u8f7d\u76f8\u540c\u7684\u4f4d\uff0c\u4ee5\u786e\u4fdd\u9879\u76ee\u6240\u4f9d\u8d56\u7684\u6a21\u5757\u4e0d\u4f1a\u51fa\u73b0\u610f\u5916\u66f4\u6539\uff0c\u65e0\u8bba\u662f\u51fa\u4e8e\u6076\u610f\u3001\u610f\u5916\u8fd8\u662f\u5176\u4ed6\u539f\u56e0\u3002 go.mod\u548cgo.sum\u90fd\u5e94\u68c0\u5165\u7248\u672c\u63a7\u5236\u3002<\/p>\n<p>go.sum \u4e0d\u9700\u8981\u624b\u5de5\u7ef4\u62a4\uff0c\u6240\u4ee5\u53ef\u4ee5\u4e0d\u7528\u592a\u5173\u6ce8\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a\u5b50\u76ee\u5f55\u91cc\u662f\u4e0d\u9700\u8981init\u7684\uff0c\u6240\u6709\u7684\u5b50\u76ee\u5f55\u91cc\u7684\u4f9d\u8d56\u90fd\u4f1a\u7ec4\u7ec7\u5728\u6839\u76ee\u5f55\u7684go.mod\u6587\u4ef6\u91cc<\/strong><\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u7684\u9879\u76ee\u4f9d\u7a00\u4e00\u4e0b\u7b2c\u4e09\u65b9\u5305<\/p>\n<p>\u5982\u4fee\u6539hello.go\u6587\u4ef6\u5982\u4e0b\uff0c\u6309\u7167\u8fc7\u53bb\u7684\u505a\u6cd5\uff0c\u8981\u8fd0\u884chello.go\u9700\u8981\u6267\u884cgo get \u547d\u4ee4 \u4e0b\u8f7dgorose\u5305\u5230 $GOPATH\/src<\/p>\n<pre class=\"prettyprint\" data-index=\"1\"><code class=\"has-numbering\">package main\r\n \r\nimport (\r\n\t\"fmt\"\r\n\t\"github.com\/gohouse\/gorose\"\r\n)\r\n \r\nfunc main() {\r\n    fmt.Println(\"Hello, world!\")\r\n}\r\n<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<\/ul>\n<p><strong>\u4f46\u662f\uff0c\u4f7f\u7528\u4e86\u65b0\u7684\u5305\u7ba1\u7406\u5c31\u4e0d\u5728\u9700\u8981\u8fd9\u6837\u505a\u4e86<\/strong><\/p>\n<p>\u76f4\u63a5 go run hello.go<\/p>\n<p>\u7a0d\u7b49\u7247\u523b\u2026 go \u4f1a\u81ea\u52a8\u67e5\u627e\u4ee3\u7801\u4e2d\u7684\u5305\uff0c\u4e0b\u8f7d\u4f9d\u8d56\u5305\uff0c\u5e76\u4e14\u628a\u5177\u4f53\u7684\u4f9d\u8d56\u5173\u7cfb\u548c\u7248\u672c\u5199\u5165\u5230go.mod\u548cgo.sum\u6587\u4ef6\u4e2d\u3002\u67e5\u770bgo.mod\uff0c\u5b83\u4f1a\u53d8\u6210\u8fd9\u6837\uff1a<\/p>\n<pre class=\"prettyprint\" data-index=\"2\"><code class=\"has-numbering\">module test\r\n \r\nrequire (\r\n\tgithub.com\/gohouse\/gorose v1.0.5\r\n)\r\n<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<\/ul>\n<p>require \u5173\u952e\u5b57\u662f\u5f15\u7528\uff0c\u540e\u9762\u662f\u5305\uff0c\u6700\u540ev1.11.1 \u662f\u5f15\u7528\u7684\u7248\u672c\u53f7<\/p>\n<p>\u8fd9\u6837\uff0c\u4e00\u4e2a\u4f7f\u7528Go\u5305\u7ba1\u7406\u65b9\u5f0f\u521b\u5efa\u9879\u76ee\u7684\u5c0f\u4f8b\u5b50\u5c31\u5b8c\u6210\u4e86\u3002<\/p>\n<p><strong>\u95ee\u9898\u4e00\uff1a\u4f9d\u8d56\u7684\u5305\u4e0b\u8f7d\u5230\u54ea\u91cc\u4e86\uff1f\u8fd8\u5728GOPATH\/src\u91cc\u5417\uff1f<\/strong><br \/>\n\u4e0d\u5728\u3002<br \/>\n\u4f7f\u7528Go\u7684\u5305\u7ba1\u7406\u65b9\u5f0f\uff0c\u4f9d\u8d56\u7684\u7b2c\u4e09\u65b9\u5305\u88ab\u4e0b\u8f7d\u5230\u4e86$GOPATH\/pkg\/mod\u8def\u5f84\u4e0b\u3002<\/p>\n<p><strong>\u95ee\u9898\u4e8c\uff1a \u4f9d\u8d56\u5305\u7684\u7248\u672c\u662f\u600e\u4e48\u63a7\u5236\u7684\uff1f<\/strong><\/p>\n<p>\u5728\u4e0a\u4e00\u4e2a\u95ee\u9898\u91cc\uff0c\u53ef\u4ee5\u770b\u5230\u6700\u7ec8\u4e0b\u8f7d\u5728<code>$GOPATH\/pkg\/mod<\/code>\u00a0\u4e0b\u7684\u5305\u4e2d\u6700\u540e\u4f1a\u6709\u4e00\u4e2a\u7248\u672c\u53f7 v1.0.5\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c<code>$GOPATH\/pkg\/mod<\/code>\u91cc\u53ef\u4ee5\u4fdd\u5b58\u76f8\u540c\u5305\u7684\u4e0d\u540c\u7248\u672c\u3002<\/p>\n<p>\u7248\u672c\u662f\u5728go.mod\u4e2d\u6307\u5b9a\u7684\u3002\u5982\u679c\uff0c\u5728go.mod\u4e2d\u6ca1\u6709\u6307\u5b9a\uff0cgo\u547d\u4ee4\u4f1a\u81ea\u52a8\u4e0b\u8f7d\u4ee3\u7801\u4e2d\u7684\u4f9d\u8d56\u7684\u6700\u65b0\u7248\u672c\uff0c\u672c\u4f8b\u5c31\u662f\u81ea\u52a8\u4e0b\u8f7d\u6700\u65b0\u7684\u7248\u672c\u3002\u5982\u679c\uff0c\u5728go.mod\u7528require\u8bed\u53e5\u6307\u5b9a\u5305\u548c\u7248\u672c \uff0cgo\u547d\u4ee4\u4f1a\u6839\u636e\u6307\u5b9a\u7684\u8def\u5f84\u548c\u7248\u672c\u4e0b\u8f7d\u5305\uff0c<br \/>\n\u6307\u5b9a\u7248\u672c\u65f6\u53ef\u4ee5\u7528latest\uff0c\u8fd9\u6837\u5b83\u4f1a\u81ea\u52a8\u4e0b\u8f7d\u6307\u5b9a\u5305\u7684\u6700\u65b0\u7248\u672c\uff1b<\/p>\n<p><strong>\u95ee\u9898\u4e09\uff1a \u53ef\u4ee5\u628a\u9879\u76ee\u653e\u5728$GOPATH\/src\u4e0b\u5417\uff1f<\/strong><\/p>\n<p>\u53ef\u4ee5\u3002\u4f46\u662fgo\u4f1a\u6839\u636eGO111MODULE\u7684\u503c\u800c\u91c7\u53d6\u4e0d\u540c\u7684\u5904\u7406\u65b9\u5f0f\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cGO111MODULE=auto \u81ea\u52a8\u6a21\u5f0f<\/p>\n<p>1.auto \u81ea\u52a8\u6a21\u5f0f\u4e0b\uff0c\u9879\u76ee\u5728<code>$GOPATH\/src<\/code>\u91cc\u4f1a\u4f7f\u7528<code>$GOPATH\/src<\/code>\u7684\u4f9d\u8d56\u5305\uff0c\u5728<code>$GOPATH\/src<\/code>\u5916\uff0c\u5c31\u4f7f\u7528go.mod \u91cc require\u7684\u5305<\/p>\n<p>2.on \u5f00\u542f\u6a21\u5f0f\uff0c1.12\u540e\uff0c\u65e0\u8bba\u5728$GOPATH\/src\u91cc\u8fd8\u662f\u5728\u5916\u9762\uff0c\u90fd\u4f1a\u4f7f\u7528go.mod \u91cc require\u7684\u5305<\/p>\n<p>3.off \u5173\u95ed\u6a21\u5f0f\uff0c\u5c31\u662f\u8001\u89c4\u77e9\u3002<\/p>\n<p><strong>\u95ee\u9898\u4e09\uff1a \u4f9d\u8d56\u5305\u4e2d\u7684\u5730\u5740\u5931\u6548\u4e86\u600e\u4e48\u529e\uff1f\u6bd4\u5982 golang.org\/x\/\u2026 \u4e0b\u7684\u5305\u90fd\u65e0\u6cd5\u4e0b\u8f7d\u600e\u4e48\u529e\uff1f<\/strong><br \/>\n\u5728go\u5feb\u901f\u53d1\u5c55\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6709\u4e00\u4e9b\u4f9d\u8d56\u5305\u5730\u5740\u53d8\u66f4\u4e86\u3002\u4ee5\u524d\u7684\u505a\u6cd5\uff1a<\/p>\n<p>1.\u4fee\u6539\u6e90\u7801\uff0c\u7528\u65b0\u8def\u5f84\u66ff\u6362import\u7684\u5730\u5740<\/p>\n<p>2.git clone \u6216 go get \u65b0\u5305\u540e\uff0ccopy\u5230$GOPATH\/src\u91cc\u65e7\u7684\u8def\u5f84\u4e0b<\/p>\n<p>\u65e0\u8bba\u4ec0\u4e48\u65b9\u6cd5\uff0c\u90fd\u4e0d\u4fbf\u4e8e\u7ef4\u62a4\uff0c\u7279\u522b\u662f\u591a\u4eba\u534f\u540c\u5f00\u53d1\u65f6\u3002<\/p>\n<p>\u4f7f\u7528go.mod\u5c31\u7b80\u5355\u4e86\uff0c\u5728go.mod\u6587\u4ef6\u91cc\u7528 replace \u66ff\u6362\u5305\uff0c\u4f8b\u5982<\/p>\n<p>replace golang.org\/x\/text => github.com\/golang\/text latest<\/p>\n<p>\u8fd9\u6837\uff0cgo\u4f1a\u7528 github.com\/golang\/text \u66ff\u4ee3golang.org\/x\/text\uff0c\u539f\u7406\u5c31\u662f\u4e0b\u8f7dgithub.com\/golang\/text \u7684\u6700\u65b0\u7248\u672c\u5230 $GOPATH\/pkg\/mod\/golang.org\/x\/text\u4e0b\u3002<\/p>\n<p><strong>\u95ee\u9898\u56db\uff1a init\u751f\u6210\u7684go.mod\u7684\u6a21\u5757\u540d\u79f0\u6709\u4ec0\u4e48\u7528\uff1f<\/strong><\/p>\n<p>\u672c\u4f8b\u91cc\uff0c\u7528 go mod init hello \u751f\u6210\u7684go.mod\u6587\u4ef6\u91cc\u7684\u7b2c\u4e00\u884c\u4f1a\u7533\u660emodule hello<\/p>\n<p>\u56e0\u4e3a\u6211\u4eec\u7684\u9879\u76ee\u5df2\u7ecf\u4e0d\u5728$GOPATH\/src\u91cc\u4e86\uff0c\u90a3\u4e48\u5f15\u7528\u81ea\u5df1\u600e\u4e48\u529e\uff1f\u5c31\u7528\u6a21\u5757\u540d+\u8def\u5f84\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5728\u9879\u76ee\u4e0b\u65b0\u5efa\u76ee\u5f55 utils\uff0c\u521b\u5efa\u4e00\u4e2atools.go\u6587\u4ef6:<\/p>\n<pre class=\"prettyprint\" data-index=\"3\"><code class=\"has-numbering\">package utils\r\n \r\nimport \u201cfmt\u201d\r\n \r\nfunc PrintText(text string) {\r\n    fmt.Println(text)\r\n}\r\n<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<\/ul>\n<p>\u5728\u6839\u76ee\u5f55\u4e0b\u7684hello.go\u6587\u4ef6\u5c31\u53ef\u4ee5 import \u201chello\/utils\u201d \u5f15\u7528utils<\/p>\n<pre class=\"prettyprint\" data-index=\"4\"><code class=\"has-numbering\">package main\r\n \r\nimport (\r\n\t\"hello\/utils\"\r\n\t\"github.com\/astaxie\/beego\"\r\n)\r\n \r\nfunc main() {\r\n\tutils.PrintText(\"Hi\")\r\n\tbeego.Run()\r\n}\r\n<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<\/ul>\n<p><strong>\u95ee\u9898\u4e94\uff1a\u4ee5\u524d\u8001\u9879\u76ee\u5982\u4f55\u7528\u65b0\u7684\u5305\u7ba1\u7406<\/strong><\/p>\n<p>\u5982\u679c\u7528auto\u6a21\u5f0f\uff0c\u628a\u9879\u76ee\u79fb\u52a8\u5230$GOPATH\/src\u5916<br \/>\n\u8fdb\u5165\u76ee\u5f55\uff0c\u8fd0\u884c go mod init + \u6a21\u5757\u540d\u79f0<br \/>\ngo build \u6216\u8005 go run \u4e00\u6b21<\/p>\n<\/div>\n<\/div>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740 \u4ec0\u4e48\u662fgo.mod? Go.m<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[24],"tags":[121],"class_list":["post-6444","post","type-post","status-publish","format-standard","hentry","category-value_docs","tag-05-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/6444","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=6444"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/6444\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=6444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=6444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=6444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}