{"id":184654,"date":"2023-09-18T16:59:38","date_gmt":"2023-09-18T08:59:38","guid":{"rendered":"http:\/\/www.idc.net\/help\/184654\/"},"modified":"2023-09-18T16:59:38","modified_gmt":"2023-09-18T08:59:38","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91go%e6%95%99%e7%a8%8bgo%e8%af%ad%e8%a8%80go-mod%e5%8c%85%e4%be%9d%e8%b5%96%e7%ae%a1%e7%90%86%e5%b7%a5%e5%85%b7%e4%bd%bf%e7%94%a8%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/184654\/","title":{"rendered":"\u540e\u6d6a\u4e91GO\u6559\u7a0b:Go\u8bed\u8a00go mod\u5305\u4f9d\u8d56\u7ba1\u7406\u5de5\u5177\u4f7f\u7528\u8be6\u89e3"},"content":{"rendered":"<p>\u6700\u65e9\u7684\u65f6\u5019\uff0cGo\u8bed\u8a00\u6240\u4f9d\u8d56\u7684\u6240\u6709\u7684\u7b2c\u4e09\u65b9\u5e93\u90fd\u653e\u5728 GOPATH \u8fd9\u4e2a\u76ee\u5f55\u4e0b\u9762\uff0c\u8fd9\u5c31\u5bfc\u81f4\u4e86\u540c\u4e00\u4e2a\u5e93\u53ea\u80fd\u4fdd\u5b58\u4e00\u4e2a\u7248\u672c\u7684\u4ee3\u7801\u3002\u5982\u679c\u4e0d\u540c\u7684\u9879\u76ee\u4f9d\u8d56\u540c\u4e00\u4e2a\u7b2c\u4e09\u65b9\u7684\u5e93\u7684\u4e0d\u540c\u7248\u672c\uff0c\u5e94\u8be5\u600e\u4e48\u89e3\u51b3\uff1f <\/p>\n<p> go module \u662fGo\u8bed\u8a00\u4ece 1.11 \u7248\u672c\u4e4b\u540e\u5b98\u65b9\u63a8\u51fa\u7684\u7248\u672c\u7ba1\u7406\u5de5\u5177\uff0c\u5e76\u4e14\u4ece Go1.13 \u7248\u672c\u5f00\u59cb\uff0cgo module \u6210\u4e3a\u4e86Go\u8bed\u8a00\u9ed8\u8ba4\u7684\u4f9d\u8d56\u7ba1\u7406\u5de5\u5177\u3002 <\/p>\n<p> Modules \u5b98\u65b9\u5b9a\u4e49\u4e3a\uff1a <\/p>\n<p> Modules \u662f\u76f8\u5173 Go \u5305\u7684\u96c6\u5408\uff0c\u662f\u6e90\u4ee3\u7801\u4ea4\u6362\u548c\u7248\u672c\u63a7\u5236\u7684\u5355\u5143\u3002Go\u8bed\u8a00\u547d\u4ee4\u76f4\u63a5\u652f\u6301\u4f7f\u7528 Modules\uff0c\u5305\u62ec\u8bb0\u5f55\u548c\u89e3\u6790\u5bf9\u5176\u4ed6\u6a21\u5757\u7684\u4f9d\u8d56\u6027\uff0cModules \u66ff\u6362\u65e7\u7684\u57fa\u4e8e GOPATH \u7684\u65b9\u6cd5\uff0c\u6765\u6307\u5b9a\u4f7f\u7528\u54ea\u4e9b\u6e90\u6587\u4ef6\u3002<\/p>\n<h2> \u5982\u4f55\u4f7f\u7528 Modules\uff1f<\/h2>\n<p> 1) \u9996\u5148\u9700\u8981\u628a golang \u5347\u7ea7\u5230 1.11 \u7248\u672c\u4ee5\u4e0a\uff08\u73b0\u5728 1.13 \u5df2\u7ecf\u53d1\u5e03\u4e86\uff0c\u5efa\u8bae\u4f7f\u7528 1.13\uff09\u3002 <\/p>\n<p> 2) \u8bbe\u7f6e GO111MODULE\u3002 <\/p>\n<h4> GO111MODULE<\/h4>\n<p> \u5728Go\u8bed\u8a00 1.12 \u7248\u672c\u4e4b\u524d\uff0c\u8981\u542f\u7528 go module \u5de5\u5177\u9996\u5148\u8981\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf GO111MODULE\uff0c\u4e0d\u8fc7\u5728Go\u8bed\u8a00 1.13 \u53ca\u4ee5\u540e\u7684\u7248\u672c\u5219\u4e0d\u518d\u9700\u8981\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u3002\u901a\u8fc7 GO111MODULE \u53ef\u4ee5\u5f00\u542f\u6216\u5173\u95ed go module \u5de5\u5177\u3002<br \/>\n <\/p>\n<ul>\n<li> GO111MODULE=off \u7981\u7528&nbsp;go module\uff0c\u7f16\u8bd1\u65f6\u4f1a\u4ece GOPATH \u548c vendor \u6587\u4ef6\u5939\u4e2d\u67e5\u627e\u5305\uff1b<\/li>\n<li> GO111MODULE=on \u542f\u7528&nbsp;go module\uff0c\u7f16\u8bd1\u65f6\u4f1a\u5ffd\u7565 GOPATH \u548c vendor \u6587\u4ef6\u5939\uff0c\u53ea\u6839\u636e go.mod\u4e0b\u8f7d\u4f9d\u8d56\uff1b<\/li>\n<li> GO111MODULE=auto\uff08\u9ed8\u8ba4\u503c\uff09\uff0c\u5f53\u9879\u76ee\u5728 GOPATH\/src \u76ee\u5f55\u4e4b\u5916\uff0c\u5e76\u4e14\u9879\u76ee\u6839\u76ee\u5f55\u6709 go.mod \u6587\u4ef6\u65f6\uff0c\u5f00\u542f&nbsp;go module\u3002<\/li>\n<\/ul>\n<p> Windows \u4e0b\u5f00\u542f GO111MODULE \u7684\u547d\u4ee4\u4e3a\uff1a <\/p>\n<p> set&nbsp;GO111MODULE=on \u6216\u8005 set GO111MODULE=auto<\/p>\n<p> MacOS \u6216\u8005 Linux \u4e0b\u5f00\u542f GO111MODULE&nbsp;\u7684\u547d\u4ee4\u4e3a\uff1a <\/p>\n<p> export GO111MODULE=on \u6216\u8005 export GO111MODULE=auto<\/p>\n<p> \u5728\u5f00\u542f GO111MODULE \u4e4b\u540e\u5c31\u53ef\u4ee5\u4f7f\u7528 go module \u5de5\u5177\u4e86\uff0c\u4e5f\u5c31\u662f\u8bf4\u5728\u4ee5\u540e\u7684\u5f00\u53d1\u4e2d\u5c31\u6ca1\u6709\u5fc5\u8981\u5728 GOPATH \u4e2d\u521b\u5efa\u9879\u76ee\u4e86\uff0c\u5e76\u4e14\u8fd8\u80fd\u591f\u5f88\u597d\u7684\u7ba1\u7406\u9879\u76ee\u4f9d\u8d56\u7684\u7b2c\u4e09\u65b9\u5305\u4fe1\u606f\u3002 <\/p>\n<p> \u5e38\u7528\u7684<br \/>\n<code>go mod<\/code>\u547d\u4ee4\u5982\u4e0b\u8868\u6240\u793a\uff1a <\/p>\n<table>\n<tbody>\n<tr>\n<th> \u547d\u4ee4<\/th>\n<th> \u4f5c\u7528<\/th>\n<\/tr>\n<tr>\n<td> go mod download<\/td>\n<td> \u4e0b\u8f7d\u4f9d\u8d56\u5305\u5230\u672c\u5730\uff08\u9ed8\u8ba4\u4e3a GOPATH\/pkg\/mod \u76ee\u5f55\uff09<\/td>\n<\/tr>\n<tr>\n<td> go mod edit<\/td>\n<td> \u7f16\u8f91 go.mod \u6587\u4ef6<\/td>\n<\/tr>\n<tr>\n<td> go mod graph<\/td>\n<td> \u6253\u5370\u6a21\u5757\u4f9d\u8d56\u56fe<\/td>\n<\/tr>\n<tr>\n<td> go mod init<\/td>\n<td> \u521d\u59cb\u5316\u5f53\u524d\u6587\u4ef6\u5939\uff0c\u521b\u5efa go.mod \u6587\u4ef6<\/td>\n<\/tr>\n<tr>\n<td> go mod tidy<\/td>\n<td> \u589e\u52a0\u7f3a\u5c11\u7684\u5305\uff0c\u5220\u9664\u65e0\u7528\u7684\u5305<\/td>\n<\/tr>\n<tr>\n<td> go mod vendor<\/td>\n<td> \u5c06\u4f9d\u8d56\u590d\u5236\u5230 vendor \u76ee\u5f55\u4e0b<\/td>\n<\/tr>\n<tr>\n<td> go mod verify<\/td>\n<td> \u6821\u9a8c\u4f9d\u8d56<\/td>\n<\/tr>\n<tr>\n<td> go mod why<\/td>\n<td> \u89e3\u91ca\u4e3a\u4ec0\u4e48\u9700\u8981\u4f9d\u8d56<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4> GOPROXY<\/h4>\n<p> proxy \u987e\u540d\u601d\u4e49\u5c31\u662f\u4ee3\u7406\u670d\u52a1\u5668\u7684\u610f\u601d\u3002\u5927\u5bb6\u90fd\u77e5\u9053\uff0c\u56fd\u5185\u7684\u7f51\u7edc\u6709\u9632\u706b\u5899\u7684\u5b58\u5728\uff0c\u8fd9\u5bfc\u81f4\u6709\u4e9bGo\u8bed\u8a00\u7684\u7b2c\u4e09\u65b9\u5305\u6211\u4eec\u65e0\u6cd5\u76f4\u63a5\u901a\u8fc7<br \/>\n<code>go get<\/code>\u547d\u4ee4\u83b7\u53d6\u3002GOPROXY \u662fGo\u8bed\u8a00\u5b98\u65b9\u63d0\u4f9b\u7684\u4e00\u79cd\u901a\u8fc7\u4e2d\u95f4\u4ee3\u7406\u5546\u6765\u4e3a\u7528\u6237\u63d0\u4f9b\u5305\u4e0b\u8f7d\u670d\u52a1\u7684\u65b9\u5f0f\u3002\u8981\u4f7f\u7528 GOPROXY \u53ea\u9700\u8981\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf GOPROXY \u5373\u53ef\u3002 <\/p>\n<p> \u76ee\u524d\u516c\u5f00\u7684\u4ee3\u7406\u670d\u52a1\u5668\u7684\u5730\u5740\u6709\uff1a <\/p>\n<ul>\n<li> goproxy.io\uff1b<\/li>\n<li> goproxy.cn\uff1a\uff08\u63a8\u8350\uff09\u7531\u56fd\u5185\u7684\u4e03\u725b\u4e91\u63d0\u4f9b\u3002<\/li>\n<\/ul>\n<p> Windows \u4e0b\u8bbe\u7f6e GOPROXY \u7684\u547d\u4ee4\u4e3a\uff1a <\/p>\n<p> go env -w GOPROXY=https:\/\/goproxy.cn,direct<\/p>\n<p> MacOS \u6216 Linux \u4e0b\u8bbe\u7f6e GOPROXY \u7684\u547d\u4ee4\u4e3a\uff1a <\/p>\n<p> export GOPROXY=https:\/\/goproxy.cn<\/p>\n<p> Go\u8bed\u8a00\u5728 1.13 \u7248\u672c\u4e4b\u540e GOPROXY \u9ed8\u8ba4\u503c\u4e3a https:\/\/proxy.golang.org\uff0c\u5728\u56fd\u5185\u53ef\u80fd\u4f1a\u5b58\u5728\u4e0b\u8f7d\u6162\u6216\u8005\u65e0\u6cd5\u8bbf\u95ee\u7684\u60c5\u51b5\uff0c\u6240\u4ee5\u5341\u5206\u5efa\u8bae\u5927\u5bb6\u5c06 GOPROXY \u8bbe\u7f6e\u4e3a\u56fd\u5185\u7684 goproxy.cn\u3002 <\/p>\n<h4> \u4f7f\u7528go get\u547d\u4ee4\u4e0b\u8f7d\u6307\u5b9a\u7248\u672c\u7684\u4f9d\u8d56\u5305<\/h4>\n<p> \u6267\u884c<br \/>\n<code>go get&nbsp;<\/code>\u547d\u4ee4\uff0c\u5728\u4e0b\u8f7d\u4f9d\u8d56\u5305\u7684\u540c\u65f6\u8fd8\u53ef\u4ee5\u6307\u5b9a\u4f9d\u8d56\u5305\u7684\u7248\u672c\u3002<br \/>\n <\/p>\n<ul>\n<li> \u8fd0\u884c<code>go get -u<\/code>\u547d\u4ee4\u4f1a\u5c06\u9879\u76ee\u4e2d\u7684\u5305\u5347\u7ea7\u5230\u6700\u65b0\u7684\u6b21\u8981\u7248\u672c\u6216\u8005\u4fee\u8ba2\u7248\u672c\uff1b<\/li>\n<li> \u8fd0\u884c<code>go get -u=patch<\/code>\u547d\u4ee4\u4f1a\u5c06\u9879\u76ee\u4e2d\u7684\u5305\u5347\u7ea7\u5230\u6700\u65b0\u7684\u4fee\u8ba2\u7248\u672c\uff1b<\/li>\n<li> \u8fd0\u884c<code>go get [\u5305\u540d]@[\u7248\u672c\u53f7]<\/code>\u547d\u4ee4\u4f1a\u4e0b\u8f7d\u5bf9\u5e94\u5305\u7684\u6307\u5b9a\u7248\u672c\u6216\u8005\u5c06\u5bf9\u5e94\u5305\u5347\u7ea7\u5230\u6307\u5b9a\u7684\u7248\u672c\u3002<\/li>\n<\/ul>\n<p> \u63d0\u793a\uff1a<code>go get [\u5305\u540d]@[\u7248\u672c\u53f7]<\/code>\u547d\u4ee4\u4e2d\u7248\u672c\u53f7\u53ef\u4ee5\u662f x.y.z \u7684\u5f62\u5f0f\uff0c\u4f8b\u5982 go get foo@v1.2.3\uff0c\u4e5f\u53ef\u4ee5\u662f git \u4e0a\u7684\u5206\u652f\u6216 tag\uff0c\u4f8b\u5982 go get foo@master\uff0c\u8fd8\u53ef\u4ee5\u662f git \u63d0\u4ea4\u65f6\u7684\u54c8\u5e0c\u503c\uff0c\u4f8b\u5982 go get foo@e3702bed2\u3002<\/p>\n<h2> \u5982\u4f55\u5728\u9879\u76ee\u4e2d\u4f7f\u7528<\/h2>\n<p> \u3010\u793a\u4f8b 1\u3011\u521b\u5efa\u4e00\u4e2a\u65b0\u9879\u76ee\uff1a <\/p>\n<p> 1) \u5728 GOPATH \u76ee\u5f55\u4e4b\u5916\u65b0\u5efa\u4e00\u4e2a\u76ee\u5f55\uff0c\u5e76\u4f7f\u7528<br \/>\n<code>go mod init<\/code>\u521d\u59cb\u5316\u751f\u6210 go.mod \u6587\u4ef6\u3002<br \/>\n <\/p>\n<p> go mod init hello<br \/> go: creating new go.mod: module hello<\/p>\n<p> go.mod \u6587\u4ef6\u4e00\u65e6\u521b\u5efa\u540e\uff0c\u5b83\u7684\u5185\u5bb9\u5c06\u4f1a\u88ab go toolchain \u5168\u9762\u638c\u63a7\uff0cgo toolchain \u4f1a\u5728\u5404\u7c7b\u547d\u4ee4\u6267\u884c\u65f6\uff0c\u6bd4\u5982<br \/>\n<code>go get<\/code>\u3001<br \/>\n<code>go build<\/code>\u3001<br \/>\n<code>go mod<\/code>\u7b49\u4fee\u6539\u548c\u7ef4\u62a4 go.mod \u6587\u4ef6\u3002 <\/p>\n<p> go.mod \u63d0\u4f9b\u4e86 module\u3001require\u3001replace \u548c exclude \u56db\u4e2a\u547d\u4ee4\uff1a<br \/>\n <\/p>\n<ul>\n<li> module \u8bed\u53e5\u6307\u5b9a\u5305\u7684\u540d\u5b57\uff08\u8def\u5f84\uff09\uff1b<\/li>\n<li> require \u8bed\u53e5\u6307\u5b9a\u7684\u4f9d\u8d56\u9879\u6a21\u5757\uff1b<\/li>\n<li> replace \u8bed\u53e5\u53ef\u4ee5\u66ff\u6362\u4f9d\u8d56\u9879\u6a21\u5757\uff1b<\/li>\n<li> exclude \u8bed\u53e5\u53ef\u4ee5\u5ffd\u7565\u4f9d\u8d56\u9879\u6a21\u5757\u3002<\/li>\n<\/ul>\n<p> \u521d\u59cb\u5316\u751f\u6210\u7684 go.mod \u6587\u4ef6\u5982\u4e0b\u6240\u793a\uff1a <\/p>\n<p> module hello<\/p>\n<p> go 1.13<\/p>\n<p> 2) \u6dfb\u52a0\u4f9d\u8d56\u3002 <\/p>\n<p> \u65b0\u5efa\u4e00\u4e2a main.go \u6587\u4ef6\uff0c\u5199\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<br \/>\n <\/p>\n<pre>\r\npackage main\r\n\r\nimport (\r\n    \"net\/http\"\r\n    \"github.com\/labstack\/echo\"\r\n)\r\n\r\nfunc main() {\r\n    e := echo.New()\r\n    e.GET(\"\/\", func(c echo.Context) error {\r\n        return c.String(http.StatusOK, \"Hello, World!\")\r\n    })\r\n    e.Logger.Fatal(e.Start(\":1323\"))\r\n}<\/pre>\n<p> \u6267\u884c<br \/>\n<code>go run main.go<\/code>\u8fd0\u884c\u4ee3\u7801\u4f1a\u53d1\u73b0 go mod \u4f1a\u81ea\u52a8\u67e5\u627e\u4f9d\u8d56\u81ea\u52a8\u4e0b\u8f7d\uff1a<br \/>\n <\/p>\n<p> go run main.go<br \/> go: finding github.com\/labstack\/echo v3.3.10+incompatible<br \/> go: downloading github.com\/labstack\/echo v3.3.10+incompatible<br \/> go: extracting github.com\/labstack\/echo v3.3.10+incompatible<br \/> go: finding github.com\/labstack\/gommon v0.3.0<br \/> ......<br \/> go: finding golang.org\/x\/text v0.3.0<\/p>\n<p> &nbsp;&nbsp; ____&nbsp;&nbsp;&nbsp; __<br \/> &nbsp; \/ __\/___\/ \/&nbsp; ___<br \/> \/ _\/\/ __\/ _ \\\/ _ \\<br \/> \/___\/\\__\/_\/\/_\/\\___\/ v3.3.10-dev<br \/> High performance, minimalist Go web framework<br \/> https:\/\/echo.labstack.com<br \/> ____________________________________O\/_______<br \/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; O\\<br \/> \u21e8 http server started on [::]:1323<\/p>\n<p> \u73b0\u5728\u67e5\u770b go.mod \u5185\u5bb9\uff1a<br \/>\n <\/p>\n<p> module hello<\/p>\n<p> go 1.13<\/p>\n<p> require (<br \/> &nbsp; &nbsp; github.com\/labstack\/echo v3.3.10+incompatible \/\/ indirect<br \/> &nbsp; &nbsp; github.com\/labstack\/gommon v0.3.0 \/\/ indirect<br \/> &nbsp; &nbsp; golang.org\/x\/crypto v0.0.0-20191206172530-e9b2fee46413 \/\/ indirect<br \/> )<\/p>\n<p> go module \u5b89\u88c5 package \u7684\u539f\u5219\u662f\u5148\u62c9\u53d6\u6700\u65b0\u7684 release tag\uff0c\u82e5\u65e0 tag \u5219\u62c9\u53d6\u6700\u65b0\u7684 commit\uff0c\u8be6\u89c1 &nbsp;Modules \u5b98\u65b9\u4ecb\u7ecd\u3002 <\/p>\n<p> go \u4f1a\u81ea\u52a8\u751f\u6210\u4e00\u4e2a go.sum \u6587\u4ef6\u6765\u8bb0\u5f55 dependency tree\uff1a<br \/>\n <\/p>\n<p> github.com\/davecgh\/go-spew v1.1.0\/go.mod h1:J7Y8YcW2NihsgmVo\/mv3lAwl\/skON4iLHjSsI+c5H38=<br \/> github.com\/labstack\/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg=<br \/> github.com\/labstack\/echo v3.3.10+incompatible\/go.mod h1:0INS7j\/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s=<br \/> github.com\/labstack\/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0=<br \/> github.com\/labstack\/gommon v0.3.0\/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=<br \/> github.com\/mattn\/go-colorable v0.1.2 h1:\/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=<br \/> ... \u7701\u7565\u5f88\u591a\u884c<\/p>\n<p> \u518d\u6b21\u6267\u884c\u811a\u672c<br \/>\n<code>go run main.go<\/code>\u53d1\u73b0\u8df3\u8fc7\u4e86\u68c0\u67e5\u5e76\u5b89\u88c5\u4f9d\u8d56\u7684\u6b65\u9aa4\u3002 <\/p>\n<p> \u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4<br \/>\n<code>go list -m -u all<\/code>\u6765\u68c0\u67e5\u53ef\u4ee5\u5347\u7ea7\u7684 package\uff0c\u4f7f\u7528<br \/>\n<code>go get -u need-upgrade-package<\/code>\u5347\u7ea7\u540e\u4f1a\u5c06\u65b0\u7684\u4f9d\u8d56\u7248\u672c\u66f4\u65b0\u5230 go.mod * \u4e5f\u53ef\u4ee5\u4f7f\u7528<br \/>\n<code>go get -u<\/code>\u5347\u7ea7\u6240\u6709\u4f9d\u8d56\u3002 <\/p>\n<p> \u3010\u793a\u4f8b 2\u3011\u6539\u9020\u73b0\u6709\u9879\u76ee\u3002 <\/p>\n<p> \u9879\u76ee\u76ee\u5f55\u7ed3\u6784\u4e3a\uff1a<br \/>\n <\/p>\n<p> \u251c\u2500 main.go<br \/> \u2502<br \/> \u2514\u2500&nbsp;api<br \/> &nbsp; &nbsp; &nbsp; \u2514\u2500 apis.go<\/p>\n<p> main.go \u6e90\u7801\u4e3a\uff1a<br \/>\n <\/p>\n<pre>\r\npackage main\r\n\r\nimport (\r\n    api \".\/api\"  \/\/ \u8fd9\u91cc\u4f7f\u7528\u7684\u662f\u76f8\u5bf9\u8def\u5f84\r\n    \"github.com\/labstack\/echo\"\r\n)\r\n\r\nfunc main() {\r\n    e := echo.New()\r\n    e.GET(\"\/\", api.HelloWorld)\r\n    e.Logger.Fatal(e.Start(\":1323\"))\r\n}<\/pre>\n<p> api\/apis.go \u6e90\u7801\u4e3a\uff1a<br \/>\n <\/p>\n<pre>\r\npackage api\r\n\r\nimport (\r\n    \"net\/http\"\r\n\r\n    \"github.com\/labstack\/echo\"\r\n)\r\n\r\nfunc HelloWorld(c echo.Context) error {\r\n    return c.JSON(http.StatusOK, \"hello world\")\r\n}<\/pre>\n<p> 1) \u4f7f\u7528&nbsp;<br \/>\n<code>go mod init ***<\/code>&nbsp;\u521d\u59cb\u5316 go.mod\u3002<br \/>\n <\/p>\n<p> go mod init hello<br \/> go: creating new go.mod: module hello<\/p>\n<p> 2) \u8fd0\u884c<br \/>\n<code>go run main.go<\/code>\u3002<br \/>\n <\/p>\n<p> go run main.go<br \/> go: finding golang.org\/x\/crypto latest<br \/> build _\/D_\/code\/src\/api: cannot find module for path _\/D_\/code\/src\/api<\/p>\n<p> \u9996\u5148\u8fd8\u662f\u4f1a\u67e5\u627e\u5e76\u4e0b\u8f7d\u5b89\u88c5\u4f9d\u8d56\uff0c\u7136\u540e\u8fd0\u884c\u811a\u672c main.go\uff0c\u8fd9\u91cc\u4f1a\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\uff1a<br \/>\n <\/p>\n<p> build _\/D_\/code\/src\/api: cannot find module for path _\/D_\/code\/src\/api<\/p>\n<p> \u4f46\u662f go.mod \u5df2\u7ecf\u66f4\u65b0\uff1a<br \/>\n <\/p>\n<p> module hello<\/p>\n<p> go 1.13<\/p>\n<p> require (<br \/> &nbsp; &nbsp; github.com\/labstack\/echo v3.3.10+incompatible \/\/ indirect<br \/> &nbsp; &nbsp; github.com\/labstack\/gommon v0.3.0 \/\/ indirect<br \/> &nbsp; &nbsp; golang.org\/x\/crypto v0.0.0-20191206172530-e9b2fee46413 \/\/ indirect<br \/> )<\/p>\n<p> \u90a3\u4e3a\u4ec0\u4e48\u4f1a\u629b\u51fa\u8fd9\u4e2a\u9519\u8bef\u5462\uff1f <\/p>\n<p> \u8fd9\u662f\u56e0\u4e3a main.go \u4e2d\u4f7f\u7528 internal package \u7684\u65b9\u6cd5\u8ddf\u4ee5\u524d\u5df2\u7ecf\u4e0d\u540c\u4e86\uff0c\u7531\u4e8e go.mod \u4f1a\u626b\u63cf\u540c\u5de5\u4f5c\u76ee\u5f55\u4e0b\u6240\u6709 package \u5e76\u4e14\u53d8\u66f4\u5f15\u5165\u65b9\u6cd5\uff0c\u5fc5\u987b\u5c06 hello \u5f53\u6210\u8def\u5f84\u7684\u524d\u7f00\uff0c\u4e5f\u5c31\u662f\u9700\u8981\u5199\u6210 import hello\/api\uff0c\u4ee5\u5f80 GOPATH\/dep \u6a21\u5f0f\u5141\u8bb8\u7684 import .\/api \u5df2\u7ecf\u5931\u6548\u3002 <\/p>\n<p> 3) \u66f4\u65b0\u65e7\u7684 package import \u65b9\u5f0f\u3002 <\/p>\n<p> \u6240\u4ee5 main.go \u9700\u8981\u6539\u5199\u6210\uff1a<br \/>\n <\/p>\n<pre>\r\npackage main\r\n\r\nimport (\r\n    api \"hello\/api\" \/\/ \u8fd9\u91cc\u4f7f\u7528\u7684\u662f\u76f8\u5bf9\u8def\u5f84\r\n    \"github.com\/labstack\/echo\"\r\n)\r\n\r\nfunc main() {\r\n    e := echo.New()\r\n    e.GET(\"\/\", api.HelloWorld)\r\n    e.Logger.Fatal(e.Start(\":1323\"))\r\n}<\/pre>\n<blockquote>\n<p> \u63d0\u793a\uff1a\u5728 Go\u8bed\u8a00 1.11 \u7248\u672c\u4e0b\u4f7f\u7528 go mod \u65f6\u53ef\u80fd\u4f1a\u9047\u5230 go build github.com\/valyala\/fasttemplate: module requires go 1.12 \u8fd9\u79cd\u9519\u8bef\uff0c\u9047\u5230\u7c7b\u4f3c\u8fd9\u79cd\u9700\u8981\u5347\u7ea7\u5230 1.12 \u7684\u95ee\u9898\uff0c\u76f4\u63a5\u5347\u7ea7\u5230Go\u8bed\u8a001.12 \u7248\u672c\u4ee5\u4e0a\u5c31\u597d\u4e86\u3002<\/p>\n<\/blockquote>\n<p> 4) \u5230\u8fd9\u91cc\u5c31\u548c\u65b0\u521b\u5efa\u4e00\u4e2a\u9879\u76ee\u6ca1\u4ec0\u4e48\u533a\u522b\u4e86\u3002<br \/>\n <\/p>\n<h2> \u4f7f\u7528 replace \u66ff\u6362\u65e0\u6cd5\u76f4\u63a5\u83b7\u53d6\u7684 package<\/h2>\n<p> \u7531\u4e8e\u67d0\u4e9b\u5df2\u77e5\u7684\u539f\u56e0\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u7684 package \u90fd\u80fd\u6210\u529f\u4e0b\u8f7d\uff0c\u6bd4\u5982\uff1agolang.org \u4e0b\u7684\u5305\u3002 <\/p>\n<p> modules \u53ef\u4ee5\u901a\u8fc7\u5728 go.mod \u6587\u4ef6\u4e2d\u4f7f\u7528 replace \u6307\u4ee4\u66ff\u6362\u6210 github \u4e0a\u5bf9\u5e94\u7684\u5e93\uff0c\u6bd4\u5982\uff1a<br \/>\n <\/p>\n<p> replace (<br \/> &nbsp;&nbsp;&nbsp; golang.org\/x\/crypto v0.0.0-20190313024323-a1f597ede03a =&gt; github.com\/golang\/crypto v0.0.0-20190313024323-a1f597ede03a<br \/> )<\/p>\n<p> \u6216\u8005<br \/>\n <\/p>\n<p> replace golang.org\/x\/crypto v0.0.0-20190313024323-a1f597ede03a =&gt; github.com\/golang\/crypto v0.0.0-20190313024323-a1f597ede03a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u65e9\u7684\u65f6\u5019\uff0cGo\u8bed\u8a00\u6240\u4f9d\u8d56\u7684\u6240\u6709\u7684\u7b2c\u4e09\u65b9\u5e93\u90fd\u653e\u5728 GOPATH \u8fd9\u4e2a\u76ee\u5f55\u4e0b\u9762\uff0c\u8fd9\u5c31\u5bfc\u81f4\u4e86\u540c\u4e00\u4e2a\u5e93\u53ea\u80fd\u4fdd\u5b58\u4e00\u4e2a\u7248 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182396],"tags":[],"class_list":["post-184654","post","type-post","status-publish","format-standard","hentry","category-go"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/184654","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=184654"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/184654\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=184654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=184654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=184654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}