{"id":207213,"date":"2025-07-08T08:22:20","date_gmt":"2025-07-08T00:22:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207213\/"},"modified":"2025-07-08T08:22:20","modified_gmt":"2025-07-08T00:22:20","slug":"%e9%80%92%e5%bd%92%e5%87%bd%e6%95%b0%e7%b1%bb%e5%9e%8b%e5%ae%9a%e4%b9%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207213\/","title":{"rendered":"\u9012\u5f52\u51fd\u6570\u7c7b\u578b\u5b9a\u4e49"},"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>\u9012\u5f52\u51fd\u6570\u7c7b\u578b\u5b9a\u4e49<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 20:42:38<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9Golang\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300a\u9012\u5f52\u51fd\u6570\u7c7b\u578b\u5b9a\u4e49\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u5bf9\u8fd9\u4e2a\u9012\u5f52\u7c7b\u578b\u5b9a\u4e49\u53d1\u751f\u7684\u4e8b\u60c5\u6709\u70b9\u56f0\u60d1\uff1a<\/p>\n<pre>type func func() (int, int, func)<\/pre>\n<p>\u6ce8\u610f\uff1a\u6211\u77e5\u9053\u5982\u4f55\u901a\u8fc7\u53cd\u590d\u8bd5\u9a8c\u6765\u4f7f\u7528\u5b83\uff0c\u4f46\u6211\u975e\u5e38\u4e0d\u786e\u5b9a\u5b83\uff08\u9012\u5f52\u7c7b\u578b\u5b9a\u4e49\uff09\u662f\u4ec0\u4e48\u3002<\/p>\n<pre>package main\n\nimport \"fmt\"\n\nfunc fib(x int) int {\n    if x == 0 {\n        return 0\n    } else if x == 1 {\n        return 1\n    } else {\n        return fib(x-1) + fib(x-2)\n    }\n}\n\ntype Func func() (int, int, Func)\n\nfunc get_fib(x int) (int, int, Func) {\n    return x, fib(x), func() (int, int, Func) { return get_fib(x + 1) }\n}\n\nfunc main() {\n    d, n, f := get_fib(10)\n    d1, n1, f1 := f()\n    d2, n2, _ := f1()\n    fmt.Println(d, n)\n    fmt.Println(d1, n1)\n    fmt.Println(d2, n2)\n}<\/pre>\n<p>\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u9610\u660e\u4e0a\u9762\u7684\u9012\u5f52\u7c7b\u578b\u5b9a\u4e49\u4e2d\u521b\u5efa\u7684\u5185\u5bb9\u5417\uff1f<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>type <code>Func func() (int, int, Func)<\/code> \u53ea\u662f\u4e00\u4e2a\u51fd\u6570\u7c7b\u578b\uff0c\u5176\u7c7b\u578b\u540d\u79f0\u4e3a <code>Func<\/code>\u3002\u4f60\u53ef\u4ee5\u628a\u5b83\u770b\u6210\u4e00\u4e2a\u96f6\u53c2\u6570\u30013\u4e2a\u8fd4\u56de\u503c\u7684\u533f\u540d\u51fd\u6570\uff08\u6700\u540e\u4e00\u4e2a\u8fd4\u56de\u503c\u4e5f\u662fFunc\u7c7b\u578b\uff09\u3002<\/p>\n<p>\u5f53\u5c06\u67d0\u4e2a\u53d8\u91cf\u5206\u914d\u7ed9\u8be5\u51fd\u6570\u7c7b\u578b\uff08<code>Func<\/code>\uff09\u65f6\uff0c\u8be5\u53d8\u91cf\u5c06\u662f\u8be5\u51fd\u6570\u7684\u540d\u79f0\u3002\u7136\u540e\u5c31\u53ef\u4ee5\u4ee5\u53d8\u91cf\u540d\u4f5c\u4e3a\u51fd\u6570\u540d\u6765\u8c03\u7528\u8be5\u51fd\u6570\u4e86\u3002<\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>d, n, f := get_fib(10)<\/code>, d get <code>10<\/code>, n get <code>fib(10)<\/code>, fu get <code>func() (int, int, Func) { return get_fib(11) ) }<\/code>\u3002\u7136\u540e\u8c03\u7528<code>f()<\/code>\u5373\u53ef\u76f4\u63a5\u8fd4\u56de<code>get_fib(11)<\/code>\u7684\u7ed3\u679c\u3002<\/p>\n<p>================================================== ===============<\/p>\n<p>\u4e3a\u4ec0\u4e48\u9700\u8981\u9012\u5f52\u7c7b\u578b\u6765\u521b\u5efa\u6b64\u529f\u80fd\uff1a<\/p>\n<p>\u53ea\u662f\u6211\u7684\u60f3\u6cd5\uff1aget_fib\u51fd\u6570\u60f3\u8981\u8fd4\u56de\u4e09\u4e2a\u7ed3\u679c\uff1ax\uff0cfib\u51fd\u6570\u7684\u8f93\u5165\uff08int\u7c7b\u578b\uff09\uff0cfib\u51fd\u6570\u7684\u7ed3\u679c\uff08int\u7c7b\u578b\uff09\uff0c\u4e00\u4e2a\u8fd4\u56deget_fib(x+1)\u51fd\u6570\u7684\u51fd\u6570\uff08\u73b0\u5728\u8f93\u5165 func\uff0c\u800c\u4e0d\u662f Func\uff09\u3002 \uff08\u56e0\u6b64 get_fib \u4e5f\u662f\u4e00\u79cd\u9012\u5f52\uff0c\u56e0\u4e3a\u5b83\u5728\u8fd4\u56de\u4e2d\u4f7f\u7528 get_fib\u3002\uff09\u7531\u4e8e\u51fd\u6570\uff08\u7c7b\u578b\u4e3a func\uff0c\u73b0\u5728\u4e0d\u662f Func\uff09\u8fd4\u56de get_fib(x+1)\uff0c\u5176\u8fd4\u56de\u7c7b\u578b\u4e0e get_fib(x) \u7684\u8fd4\u56de\u7c7b\u578b\u76f8\u540c\u3002\u6240\u4ee5\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u5e94\u8be5\u662f <code>int\u3001int\u3001func<\/code> \uff08func \u662f get_fib \u8fd4\u56de\u7684\u7b2c\u4e09\u4e2a\u7ed3\u679c\uff0c\u5373\u51fd\u6570\u5b9a\u4e49\u672c\u8eab\uff09\u3002\u6240\u4ee5\u9700\u8981\u4e00\u4e2a\u9012\u5f52\u51fd\u6570\u7c7b\u578b\u3002<\/p>\n<p>\u7b80\u800c\u8a00\u4e4b\uff1a<\/p>\n<ul>\n<li>get_fib \u7684\u8f93\u51fa\u4e3a (int, int, customfunc)<\/li>\n<li>customfunc \u7684\u8f93\u51fa\u662f (get_fib)\uff0c\u5373 (int, int, customfunc) <strong>\u518d\u6b21<\/strong>\u3002<\/li>\n<li>\u6240\u4ee5customfunc\u7684\u8f93\u51fa\u662f(int, int, customfunc)\uff0c\u5b83\u662f\u4e00\u4e2a\u9012\u5f52\u51fd\u6570\u7c7b\u578b<\/li>\n<\/ul>\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\u300a\u9012\u5f52\u51fd\u6570\u7c7b\u578b\u5b9a\u4e49\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>\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-207213","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207213","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=207213"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207213\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}