{"id":208242,"date":"2025-07-08T16:53:57","date_gmt":"2025-07-08T08:53:57","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208242\/"},"modified":"2025-07-08T16:53:57","modified_gmt":"2025-07-08T08:53:57","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-go-%e4%b8%ad%e6%98%be%e5%bc%8f%e8%bd%ac%e6%8d%a2%e7%b1%bb%e5%9e%8b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208242\/","title":{"rendered":"\u5982\u4f55\u5728 Go \u4e2d\u663e\u5f0f\u8f6c\u6362\u7c7b\u578b\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>\u5982\u4f55\u5728 Go \u4e2d\u663e\u5f0f\u8f6c\u6362\u7c7b\u578b\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-30 17:25:53<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728Golang\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u5982\u4f55\u5728 Go \u4e2d\u663e\u5f0f\u8f6c\u6362\u7c7b\u578b\uff1f\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6709\u4e00\u4e2a\u9759\u6001\u58f0\u660e\u7684\u53d8\u91cf<\/p>\n<p><code>var fun *ast.fundecl<\/code><\/p>\n<p>\u8fd8\u6709\u4e00\u4e2a\u540d\u4e3a <code>decl<\/code> \u7684\u6570\u7ec4\uff0c\u7c7b\u578b\u4e3a <code>ast.decl<\/code>\uff0c\u5176\u4e2d\u5305\u542b\u4e0d\u540c\u7c7b\u578b <code>ast.gendecl<\/code> \u548c <code>*ast.fundecl<\/code> \u7684\u9879\u76ee\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u5728\u8fd0\u884c\u65f6\u6211\u60f3\u8fed\u4ee3\u6570\u7ec4\u5e76\u5c06\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684 <code>*ast.fundecl<\/code> \u7c7b\u578b\u7684\u9879\u5206\u914d\u7ed9\u6211\u7684\u53d8\u91cf <code>fun<\/code>\u3002<\/p>\n<p>\u5728\u6211\u7684\u6570\u7ec4\u8fed\u4ee3\u4e2d\uff0c\u5176\u4e2d <code>d<\/code> \u662f\u5f53\u524d\u6570\u7ec4\u5143\u7d20\uff0c\u6211\u6b63\u5728\u4f7f\u7528\uff1a<\/p>\n<pre>switch t := d.(type)\n{\n    case *ast.fundecl:\n    {\n        fun = d \/\/ cannot use d (variable of type ast.decl) as *ast.funcdecl value in assignment\n    }\n\n    \/\/ more type cases ...\n}<\/pre>\n<p>\u6b64\u5916\uff0c\u5c1d\u8bd5\u4f7f\u7528\u663e\u5f0f\u5f3a\u5236\u8f6c\u6362<\/p>\n<pre>fun = *ast.FunDecl(d)<\/pre>\n<p>\u6050\u614c\u8bf4\uff1a<\/p>\n<p> \u65e0\u6cd5\u5c06 d\uff08ast.decl \u7c7b\u578b\u7684\u53d8\u91cf\uff09\u8f6c\u6362\u4e3a ast.funcdecl\u3002 <\/p>\n<p>\u9664\u4e86\u89e3\u51b3\u8fd9\u4e2a\u7279\u6b8a\u60c5\u51b5\u4e4b\u5916\uff0c\u8fd9\u8fd8\u7ed9\u6211\u5e26\u6765\u4e86\u4e00\u4e2a\u666e\u904d\u95ee\u9898\uff0c\u5982\u4f55\u5904\u7406\u8fd9\u6837\u7684\u7c7b\u578b\u8f6c\u6362\u60c5\u51b5\uff1f\u5982\u679c\u6211\u77e5\u9053\u53d8\u91cf\u7684\u7c7b\u578b\u4e0e\u6211\u7684\u8f6c\u6362\u76f8\u5339\u914d\uff0c\u5982\u4f55\u5c06\u5176\u8f6c\u6362\u4e3a\u7279\u5b9a\u7c7b\u578b\uff1f<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u9700\u8981\u5206\u914d\u7c7b\u578b\u8f6c\u6362\u503c t \u800c\u4e0d\u662f d<\/p>\n<pre>switch t := d.(type){\n    case *ast.FunDecl:\n    {\n        fun = t\n    }\n}<\/pre>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u5728 Go \u4e2d\u663e\u5f0f\u8f6c\u6362\u7c7b\u578b\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\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-208242","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208242","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=208242"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208242\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}