{"id":256279,"date":"2022-11-18T09:55:18","date_gmt":"2022-11-18T01:55:18","guid":{"rendered":"https:\/\/gulass.cn\/?p=256279"},"modified":"2022-10-28T12:56:53","modified_gmt":"2022-10-28T04:56:53","slug":"go-mod-init","status":"publish","type":"post","link":"https:\/\/gulass.cn\/go-mod-init.html","title":{"rendered":"Go\u521d\u5b66\u8005\u8e29\u5751\u4e4bgo mod init\u4e0e\u81ea\u5b9a\u4e49\u5305\u7684\u4f7f\u7528\u8be6\u89e3"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\ngo mod\u662fgo\u7684\u4e00\u4e2a\u6a21\u5757\u7ba1\u7406\u5de5\u5177,\u7528\u6765\u4ee3\u66ff\u4f20\u7edf\u7684GOPATH\u65b9\u6848,\u4e0b\u9762\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e86\u5173\u4e8eGo\u521d\u5b66\u8005\u8e29\u5751\u4e4bgo mod init\u4e0e\u81ea\u5b9a\u4e49\u5305\u7684\u4f7f\u7528,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u524d\u8a00<\/strong><\/div>\n

\u5165\u95e8\u5f88\u5bb9\u6613\u7684 hello world, \u5230\u4f7f\u7528\u81ea\u5b9a\u4e49\u5305\u7b97\u4e00\u4e2a\u5c0f\u7684\u8fdb\u6b65,\u7136\u800c\u5728\u4f7f\u7528\u4e0a\u7adf\u7136\u6d6a\u8d39\u4e86\u4e0d\u5c11\u65f6\u95f4,\u4e0a\u7f51\u67e5\u4e86\u534a\u5929,\u624d\u89e3\u51b3\u95ee\u9898.<\/p>\n

\u7b2c\u4e00\u660e\u786e \u662f\u54ea\u79cd\u65b9\u5f0f,\u662fgo mod \u65b9\u5f0f \u8fd8\u662f go path \u65b9\u5f0f, \u8bf7\u81ea\u5df1\u67e5\u4e00\u4e0b,\u672c\u5c0f\u6587\u76f4\u8bf4go mog \u65b9\u5f0f\u7684\u5c0f\u95ee\u9898.<\/p>\n

\u9996\u5148\u770b\u4e00\u4e0b \u6587\u6863\u7ed3\u6784<\/strong><\/div>\n

\"\"<\/p>\n

\u6211\u81ea\u5df1\u5199\u4e86\u4e2a ToolKit.go, \u5185\u5bb9\u5927\u81f4\u5982\u4e0b:<\/p>\n

\u95ee\u9898\u662f:\u600e\u4e48\u5f15\u7528\u5b83<\/strong><\/div>\n
\/\/for all kind of tools\r\npackage Toolkit\r\n \r\nimport (\r\n    \"net\/http\"\r\n    \"strings\"\r\n)\r\n \r\n \r\nfunc Left(str string, cnt int) string {\r\n    l := strings.Count(str, \"\")\r\n    if cnt >= l {\r\n        cnt = l - 1\r\n    } else if l < 0 {\r\n        l = 0\r\n    }\r\n    return str[0:cnt]\r\n}\r\n...<\/pre>\n

\u5f15\u7528\u65b9\u5f0f\u5404\u79cd\u51fa\u9519,\u4e3e\u4f8b\u5982\u4e0b:<\/p>\n

package main\r\nimport (\r\n    \"testing\"\r\n    \"ToolKit\"\r\n)<\/pre>\n

\u6216\u8005:<\/p>\n

package main\r\nimport (\r\n    \"testing\"\r\n    \".\/ToolKit\"\r\n)<\/pre>\n

\u6216\u8005<\/p>\n

package main\r\nimport (\r\n    \"testing\"\r\n    \"\/usr\/local\/src\/go\/iot_watcher\/ToolKit\"\r\n)<\/pre>\n

\u6211\u80fd\u60f3\u5230\u7684\u65b9\u6cd5\u90fd\u7528\u5230\u4e86,\u8fd8\u662f\u4e0d\u884c.<\/p>\n

\u4e0b\u9762\u76f4\u63a5\u7ed9\u51fa\u89e3\u51b3\u65b9\u6848\u5427<\/strong><\/div>\n

\u67e5\u770b go.mod,\u5185\u5bb9\u5982\u4e0b<\/p>\n

module iot_watcher.com\r\n \r\ngo 1.17<\/pre>\n

OK, \u628a module \u540e\u9762\u90a3\u4e2a\u4e1c\u897f\u8003\u4e0b\u6765, \u5199\u6210\u8fd9\u6837<\/p>\n

ackage main\r\n \r\nimport (\r\n    \"testing\"\r\n    \"iot_watcher.com\/ToolKit\"\r\n)<\/pre>\n

\u5c31\u6ca1\u95ee\u9898\u4e86.<\/p>\n

\u4e3a\u4e86\u65b9\u4fbf,\u5199\u6210\u522b\u540d\u5f15\u7528\u65b9\u5f0f<\/p>\n

package main\r\n \r\nimport (\r\n    \"testing\"\r\n     \r\n    ToolKit \"iot_watcher.com\/ToolKit\"\r\n)<\/pre>\n

\u8865\u5145: \u7ec8\u4e8e\u660e\u767d go mod init anyName.com<\/p>\n

\u8fd9\u4e2aanyName.com \u4e1c\u897f\u7684\u7528\u9014\u4e86(\u4e00\u70b9\u513f\u7528\u9014)<\/p>\n

\u8bf4\u5b9e\u8bdd, \u6211\u6700\u8ba8\u538c\u8fd9\u79cd\u51e0\u4e4e\u4e00\u70b9\u513f\u542b\u91d1\u91cf\u90fd\u6ca1\u6709\u7684\u7cfb\u7edf\u914d\u7f6e\u4e86. \u4ece\u5fae\u8f6f\u7684MFC \u5230go , \u771f\u662f\u65e0\u5948.\u6d6a\u8d39\u5927\u91cf\u65f6\u95f4.<\/p>\n

\u8865\u5145\uff1ago mod init\u51fa\u9519<\/strong><\/div>\n

go mod init\u51fa\u9519\uff0c\u9519\u8bef\u63d0\u793a\u5982\u4e0b\uff1a<\/p>\n

go: cannot determine module path for source directory D:\\workspace\\go (outside GOPATH, no import comments)<\/pre>\n

\u89e3\u51b3\u529e\u6cd5\uff1a\u6dfb\u52a0\u6a21\u5757\u540d\u53c2\u6570<\/p>\n

go mod init module_name<\/pre>\n

\u4f8b\u5982\uff0c\u5728test\u76ee\u5f55\u4e0b\uff0c\u5373test\u6a21\u5757\uff0c\u5c31\u7528<\/p>\n

go mod init test<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u5165\u95e8\u5f88\u5bb9\u6613\u7684 hello world, \u5230\u4f7f\u7528\u81ea\u5b9a\u4e49\u5305\u7b97\u4e00\u4e2a\u5c0f\u7684\u8fdb\u6b65,\u7136\u800c\u5728\u4f7f\u7528\u4e0a\u7adf\u7136\u6d6a\u8d39\u4e86\u4e0d\u5c11\u65f6\u95f4,\u4e0a\u7f51\u67e5\u4e86 […]<\/p>\n","protected":false},"author":1482,"featured_media":235049,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-256279","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/256279","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=256279"}],"version-history":[{"count":5,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/256279\/revisions"}],"predecessor-version":[{"id":256296,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/256279\/revisions\/256296"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/235049"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=256279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=256279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=256279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}