{"id":207083,"date":"2025-07-08T08:33:19","date_gmt":"2025-07-08T00:33:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207083\/"},"modified":"2025-07-08T08:33:19","modified_gmt":"2025-07-08T00:33:19","slug":"godef%ef%bc%9a%e5%bf%85%e9%a1%bb%e8%87%b3%e5%b0%91%e6%9c%89%e4%b8%80%e4%b8%aa%e5%8c%85%e5%90%ab%e8%af%a5%e6%96%87%e4%bb%b6%e7%9a%84%e5%8c%85%ef%bc%8c%e5%8d%b3%e4%bd%bf%e8%af%a5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207083\/","title":{"rendered":"\u201cgodef\uff1a\u5fc5\u987b\u81f3\u5c11\u6709\u4e00\u4e2a\u5305\u542b\u8be5\u6587\u4ef6\u7684\u5305\u201d\uff0c\u5373\u4f7f\u8be5\u6587\u4ef6\u786e\u5b9e\u6709\u4e00\u4e2a\u5305\uff1f"},"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>\u201cgodef\uff1a\u5fc5\u987b\u81f3\u5c11\u6709\u4e00\u4e2a\u5305\u542b\u8be5\u6587\u4ef6\u7684\u5305\u201d\uff0c\u5373\u4f7f\u8be5\u6587\u4ef6\u786e\u5b9e\u6709\u4e00\u4e2a\u5305\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 22:21:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u201cgodef\uff1a\u5fc5\u987b\u81f3\u5c11\u6709\u4e00\u4e2a\u5305\u542b\u8be5\u6587\u4ef6\u7684\u5305\u201d\uff0c\u5373\u4f7f\u8be5\u6587\u4ef6\u786e\u5b9e\u6709\u4e00\u4e2a\u5305\uff1f\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u4f7f\u7528\u5e26\u6709 go \u6269\u5c55\u7684 vs code\uff0c\u4f46\u6211\u6ce8\u610f\u5230\u5f53\u6211\u53f3\u952e\u5355\u51fb\u67d0\u4e2a\u65b9\u6cd5\u5e76\u9009\u62e9\u201c\u8f6c\u5230\u5b9a\u4e49\u201d\u65f6\uff0c\u627e\u4e0d\u5230\u4efb\u4f55\u5b9a\u4e49\u3002\u4f8b\u5982\uff0c<\/p>\n<p>\u8fd9\u662f\u6211\u4f7f\u7528 cobra \u751f\u6210\u7684\u793a\u4f8b\u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n<pre>cobra init mycobraapp --pkg-name=github.com\/khpeek\/mycobraapp<\/pre>\n<p>\u6211\u5e0c\u671b\u8fd9\u80fd\u8d77\u4f5c\u7528\uff0c\u56e0\u4e3a <code>mycobraapp<\/code> \u76ee\u5f55\u7684\u7ec4\u7ec7\u65b9\u5f0f\u5982\u4e0b<\/p>\n<pre>.\n\u251c\u2500\u2500 license\n\u251c\u2500\u2500 cmd\n\u2502&nbsp;&nbsp; \u2514\u2500\u2500 root.go\n\u2514\u2500\u2500 main.go<\/pre>\n<p>\u5176\u4e2d <code>cmd\/root.go<\/code> \u5305\u542b<\/p>\n<pre>package cmd\n\nimport (\n  \"fmt\"\n  \"os\"\n  \"github.com\/spf13\/cobra\"\n\n  homedir \"github.com\/mitchellh\/go-homedir\"\n  \"github.com\/spf13\/viper\"\n\n)\n\n\nvar cfgfile string\n\n\n\/\/ rootcmd represents the base command when called without any subcommands\nvar rootcmd = &amp;cobra.command{\n  use:   \"mycobraapp\",\n  short: \"a brief description of your application\",\n  long: `a longer description that spans multiple lines and likely contains\nexamples and usage of using your application. for example:\n\ncobra is a cli library for go that empowers applications.\nthis application is a tool to generate the needed files\nto quickly create a cobra application.`,\n  \/\/ uncomment the following line if your bare application\n  \/\/ has an action associated with it:\n  \/\/    run: func(cmd *cobra.command, args []string) { },\n}\n\n\/\/ execute adds all child commands to the root command and sets flags appropriately.\n\/\/ this is called by main.main(). it only needs to happen once to the rootcmd.\nfunc execute() {\n  if err := rootcmd.execute(); err != nil {\n    fmt.println(err)\n    os.exit(1)\n  }\n}<\/pre>\n<p>\u6211\u8fd8\u5c1d\u8bd5\u4ece\u547d\u4ee4\u884c\u8c03\u7528 <code>godef<\/code>\uff0c\u4f46\u6536\u5230\u6b64\u9519\u8bef\u6d88\u606f\uff1a<\/p>\n<pre>~\/g\/s\/g\/k\/myCobraApp&gt; godef -f main.go \"cmd.Execute()\"\ngodef: There must be at least one package that contains the file<\/pre>\n<p>\u6211\u4e0d\u660e\u767d\u6b64\u9519\u8bef\u6d88\u606f\uff1a\u662f\u5426\u6709\u5305\u542b\u8be5\u6587\u4ef6\u7684\u5305\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u4e8b\u5b9e\u8bc1\u660e\uff0c\u901a\u8fc7\u5728\u6211\u7684 <code>~\/.config\/fish\/config.fish<\/code> \u4e2d\u5c06 <code>GO111MODULE<\/code> \u73af\u5883\u53d8\u91cf\u8bbe\u7f6e\u4e3a <code>on<\/code> \u6765\u7834\u574f\u6b64\u529f\u80fd\u3002\u5f53\u6211\u5220\u9664\u5b83\u65f6\uff0c\u201c\u8f6c\u5230\u5b9a\u4e49\u201d\u6309\u94ae\u518d\u6b21\u8d77\u4f5c\u7528\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u201cgodef\uff1a\u5fc5\u987b\u81f3\u5c11\u6709\u4e00\u4e2a\u5305\u542b\u8be5\u6587\u4ef6\u7684\u5305\u201d\uff0c\u5373\u4f7f\u8be5\u6587\u4ef6\u786e\u5b9e\u6709\u4e00\u4e2a\u5305\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\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-207083","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207083","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=207083"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207083\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}