{"id":207536,"date":"2025-07-08T14:03:19","date_gmt":"2025-07-08T06:03:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207536\/"},"modified":"2025-07-08T14:03:19","modified_gmt":"2025-07-08T06:03:19","slug":"%e8%8e%b7%e5%8f%96%e6%a8%a1%e5%9d%97%e5%90%8d%e7%a7%b0%e7%9a%84api","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207536\/","title":{"rendered":"\u83b7\u53d6\u6a21\u5757\u540d\u79f0\u7684API"},"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>\u83b7\u53d6\u6a21\u5757\u540d\u79f0\u7684API<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 13:45:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u201c\u7eb5\u6709\u75be\u98ce\u6765\uff0c\u4eba\u751f\u4e0d\u8a00\u5f03\u201d\uff0c\u8fd9\u53e5\u8bdd\u9001\u7ed9\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u7684\u670b\u53cb\u4eec\uff0c\u4e5f\u5e0c\u671b\u5728\u9605\u8bfb\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u83b7\u53d6\u6a21\u5757\u540d\u79f0\u7684API\u300b<\/span>\u540e\uff0c\u80fd\u591f\u771f\u7684\u5e2e\u52a9\u5230\u5927\u5bb6\u3002\u6211\u4e5f\u4f1a\u5728\u540e\u7eed\u7684\u6587\u7ae0\u4e2d\uff0c\u9646\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u6280\u672f\u6587\u7ae0\uff0c\u6709\u597d\u7684\u5efa\u8bae\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u975e\u5e38\u611f\u8c22\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u662f\u5426\u6709API\u53ef\u4ee5\u83b7\u53d6\u4f7f\u7528go 1.11\u6a21\u5757\u7cfb\u7edf\u7684\u9879\u76ee\u7684\u6a21\u5757\u540d\u79f0\uff1f<\/p>\n<p>\u6240\u4ee5\u6211\u9700\u8981\u4ece <code>go.mod<\/code> \u6587\u4ef6\u4e2d\u7684\u6a21\u5757\u5b9a\u4e49 <code>module abc.com\/a\/m<\/code> \u83b7\u53d6 <code>abc.com\/a\/m<\/code> \u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u622a\u81f3\u64b0\u5199\u672c\u6587\u65f6\uff0c\u6211\u4e0d\u77e5\u9053\u6709\u4efb\u4f55\u516c\u5f00\u7684 api\u3002\u7136\u800c\uff0c\u67e5\u770b <code>go mod<\/code> \u6e90\u4ee3\u7801\uff0c \u4e2d\u6709\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u51fd\u6570<\/p>\n<pre>\/\/ modulepath returns the module path from the gomod file text.\n\/\/ if it cannot find a module path, it returns an empty string.\n\/\/ it is tolerant of unrelated problems in the go.mod file.\nfunc modulepath(mod []byte) string {\n    \/\/...\n}\n\nfunc main() {\n\n    src := `\nmodule github.com\/you\/hello\n\nrequire rsc.io\/quote v1.5.2\n`\n\n    mod := modulepath([]byte(src))\n    fmt.println(mod)\n\n}<\/pre>\n<p>\u54ea\u4e2a\u8f93\u51fa <code>github.com\/you\/hello<\/code><\/p>\n<p>\u8bd5\u8bd5\u8fd9\u4e2a\uff1f<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"io\/ioutil\"\n    \"os\"\n\n    modfile \"golang.org\/x\/mod\/modfile\"\n)\n\nconst (\n    RED   = \"\\033[91m\"\n    RESET = \"\\033[0m\"\n)\n\nfunc main() {\n    modName := GetModuleName()\n    fmt.Fprintf(os.Stdout, \"modName=%+v\\n\", modName)\n}\n\nfunc exitf(beforeExitFunc func(), code int, format string, args ...interface{}) {\n    beforeExitFunc()\n    fmt.Fprintf(os.Stderr, RED+format+RESET, args...)\n    os.Exit(code)\n}\n\nfunc GetModuleName() string {\n    goModBytes, err := ioutil.ReadFile(\"go.mod\")\n    if err != nil {\n        exitf(func() {}, 1, \"%+v\\n\", err)\n    }\n\n    modName := modfile.ModulePath(goModBytes)\n    fmt.Fprintf(os.Stdout, \"modName=%+v\\n\", modName)\n\n    return modName\n}\n<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u83b7\u53d6\u6a21\u5757\u540d\u79f0\u7684API\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207536","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207536"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207536\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}