{"id":204990,"date":"2025-05-29T08:12:07","date_gmt":"2025-05-29T00:12:07","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204990\/"},"modified":"2025-05-29T08:12:07","modified_gmt":"2025-05-29T00:12:07","slug":"go%e8%af%ad%e8%a8%80%e4%b8%ad%e4%b8%ba%e4%bb%80%e4%b9%88%e5%87%bd%e6%95%b0%e9%97%ad%e5%8c%85%e4%bc%9a%e8%bf%94%e5%9b%9e%e7%9b%b8%e5%90%8c%e7%9a%84%e5%80%bc%ef%bc%9f%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204990\/","title":{"rendered":"Go\u8bed\u8a00\u4e2d\u4e3a\u4ec0\u4e48\u51fd\u6570\u95ed\u5305\u4f1a\u8fd4\u56de\u76f8\u540c\u7684\u503c\uff1f\u5982\u4f55\u89e3\u51b3\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Go\u8bed\u8a00\u4e2d\u4e3a\u4ec0\u4e48\u51fd\u6570\u95ed\u5305\u4f1a\u8fd4\u56de\u76f8\u540c\u7684\u503c\uff1f\u5982\u4f55\u89e3\u51b3\uff1f<\/h1>\n<p>\u5728\u6587\u7ae0\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u5c31\u6574\u7406\u5206\u4eab\u300aGo\u8bed\u8a00\u4e2d\u4e3a\u4ec0\u4e48\u51fd\u6570\u95ed\u5305\u4f1a\u8fd4\u56de\u76f8\u540c\u7684\u503c\uff1f\u5982\u4f55\u89e3\u51b3\uff1f\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241121\/1732147793673e7a51b04ea.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>go\u8bed\u8a00\u91c7\u7528\u665a\u7ed1\u5b9a\u7684\u539f\u56e0\u53ca\u5176\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728go\u8bed\u8a00\u4e2d\uff0c\u5f53\u6211\u4eec\u4f7f\u7528\u7a7a\u6570\u7ec4\u5b58\u50a8\u7c7b\u578b\u4e3a void -&gt; int \u7684\u51fd\u6570\u65f6\uff0c\u6211\u4eec\u4f1a\u9047\u5230\u51fd\u6570\u95ed\u5305\u7684\u95ee\u9898\uff0c\u5373\u6570\u7ec4\u4e2d\u51fd\u6570\u8fd4\u56de\u7684\u662f\u76f8\u540c\u7684\u503c\uff0c\u800c\u4e0d\u662f\u53d8\u91cf\u5728\u5faa\u73af\u4e2d\u7684\u503c\u3002<\/p>\n<p>\u8fd9\u79cd\u95ee\u9898\u662f\u7531\u4e8ego\u8bed\u8a00\u91c7\u7528<strong>\u665a\u7ed1\u5b9a<\/strong>\u673a\u5236\u9020\u6210\u7684\u3002\u4e0e\u65e9\u7ed1\u5b9a\u8bed\u8a00\u4e0d\u540c\uff0cgo\u8bed\u8a00\u5728\u8fd0\u884c\u65f6\u7ed1\u5b9a\u51fd\u6570\u8c03\u7528\uff0c\u8fd9\u610f\u5473\u7740\u51fd\u6570\u5bf9\u8c61\u5728\u7f16\u8bd1\u65f6\u5e76\u4e0d\u786e\u5b9a\uff0c\u800c\u662f\u52a8\u6001\u786e\u5b9a\u3002<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e24\u79cd\u65b9\u6cd5\uff1a<\/p>\n<p><strong>\u4f7f\u7528\u5165\u53c2<\/strong><\/p>\n<p>\u4fee\u6539\u533f\u540d\u51fd\u6570\u7684\u539f\u578b\uff0c\u6dfb\u52a0\u4e00\u4e2a\u989d\u5916\u7684\u5165\u53c2\uff1a<\/p>\n<pre>var fns [ls_size]func(i int) int<\/pre>\n<p>\u5728\u533f\u540d\u51fd\u6570\u4e2d\uff0c\u5c06\u5faa\u73af\u53d8\u91cf\u4f5c\u4e3a\u5165\u53c2\u4f20\u5165\uff1a<\/p>\n<pre>fns[i] = func(i int) int {\n    return i\n}<\/pre>\n<p>\u5728\u8c03\u7528\u6570\u7ec4\u51fd\u6570\u65f6\uff0c\u4f20\u5165\u5faa\u73af\u53d8\u91cf\uff1a<\/p>\n<pre>fmt.printf(\"fns[%d]: %d\\n\", n, fns[n](n))<\/pre>\n<p><strong>\u4f7f\u7528\u95ed\u5305<\/strong><\/p>\n<p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u95ed\u5305\uff0c\u5373\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u533f\u540d\u5b57\u6bb5\uff0c\u5176\u4e2d\u5305\u542b\u5faa\u73af\u53d8\u91cf\u7684\u503c\uff1a<\/p>\n<pre>var fns [ls_size]func() int\nfor i := 0; i &lt; ls_size; i++ {\n    fn := func(i int) int {\n        return i\n    }\n    fns[i] = fn(i)\n}<\/pre>\n<p>\u5728\u8c03\u7528\u6570\u7ec4\u51fd\u6570\u65f6\uff0c\u6267\u884c\u95ed\u5305\u6765\u83b7\u53d6\u5faa\u73af\u53d8\u91cf\u7684\u503c\uff1a<\/p>\n<pre>fmt.Printf(\"fns[%d]: %d\\n\", n, fns[n]())<\/pre>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aGo\u8bed\u8a00\u4e2d\u4e3a\u4ec0\u4e48\u51fd\u6570\u95ed\u5305\u4f1a\u8fd4\u56de\u76f8\u540c\u7684\u503c\uff1f\u5982\u4f55\u89e3\u51b3\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go\u8bed\u8a00\u4e2d\u4e3a\u4ec0\u4e48\u51fd\u6570\u95ed\u5305\u4f1a\u8fd4\u56de\u76f8&#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-204990","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204990","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=204990"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204990\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}