{"id":42696,"date":"2024-12-01T12:46:00","date_gmt":"2024-12-01T04:46:00","guid":{"rendered":"https:\/\/fwq.ai\/blog\/42696\/"},"modified":"2024-12-01T12:46:00","modified_gmt":"2024-12-01T04:46:00","slug":"%e5%b0%81%e9%97%ad%e6%89%80%e5%ae%b9%e7%ba%b3%e7%9a%84%e7%8e%af%e5%a2%83%e8%8c%83%e5%9b%b4%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/42696\/","title":{"rendered":"\u5c01\u95ed\u6240\u5bb9\u7eb3\u7684\u73af\u5883\u8303\u56f4\u662f\u4ec0\u4e48\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>\u5c01\u95ed\u6240\u5bb9\u7eb3\u7684\u73af\u5883\u8303\u56f4\u662f\u4ec0\u4e48\uff1f<\/span><\/p>\n<h1>\u5c01\u95ed\u6240\u5bb9\u7eb3\u7684\u73af\u5883\u8303\u56f4\u662f\u4ec0\u4e48\uff1f<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-25 13:42:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p><span style=\"font-size: 15px\">\u5b66\u4e60\u77e5\u8bc6\u8981\u5584\u4e8e\u601d\u8003\uff0c\u601d\u8003\uff0c\u518d\u601d\u8003\uff01\u4eca\u5929\u7c73\u4e91\u5c0f\u7f16\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5c01\u95ed\u6240\u5bb9\u7eb3\u7684\u73af\u5883\u8303\u56f4\u662f\u4ec0\u4e48\uff1f\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6216\u51c6\u5907\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u5c31\u90fd\u4e0d\u8981\u9519\u8fc7\u672c\u6587\u5566~\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff0c\u80fd\u5e2e\u52a9\u5230\u4f60\u5c31\u66f4\u597d\u4e86\uff01<\/span><\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6a21\u4eff\u7f51\u4e0a\u5173\u4e8e\u95ed\u5305\u7684\u6559\u7a0b\uff0c\u5199\u4e86\u4e0b\u9762\u7684\u4ee3\u7801\u3002<\/p>\n<pre>func foo1() func() {\n    xvalue := 1\n    x := &amp;xvalue\n    defer func() {\n        xvalue = 2\n    }()\n    return func() {\n        *x = *x + 1\n        fmt.printf(\"foo1 val = %d\\n\", *x)\n    }\n}\n\nfunc main() {\n\n    f1 := foo1()\n    f1()\n    f1()\n    f1()\n}<\/pre>\n<p>\u6211\u5f88\u56f0\u60d1\uff0c\u5728\u6267\u884c <code>f1 := foo1()<\/code> \u540e\uff0c\u53d8\u91cf <code>xvalue<\/code> \u4f3c\u4e4e\u5e94\u8be5\u88ab\u56de\u6536\uff0c\u56e0\u6b64\u4f7f\u7528 <code>*x<\/code> \u5e94\u8be5\u662f\u9519\u8bef\u7684\uff0c\u4f46\u4e0a\u9762\u7684\u4ee3\u7801\u6ca1\u6709\u9519\u8bef\u5e76\u4e14\u6267\u884c\u7f5a\u6b3e\uff0c\u7ed9\u51fa\u8f93\u51fa<\/p>\n<pre>foo1 val = 3\nfoo1 val = 4\nfoo1 val = 5<\/pre>\n<p>\u6240\u4ee5\u6211\u60f3\u77e5\u9053\u95ed\u5305\u9664\u4e86\u4fdd\u5b58\u6307\u9488\u672c\u8eab\u4e4b\u5916\u8fd8\u4fdd\u5b58\u4e86\u6307\u9488\u7684\u503c\u8fd8\u662fgo\u8bed\u8a00\u7684\u5783\u573e\u56de\u6536\u673a\u5236\u5bfc\u81f4xvalue\u6ca1\u6709\u88ab\u5220\u9664\uff1f<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u5728 Go \u4e2d\uff0c\u95ed\u5305\u83b7\u53d6\u5bf9\u5176\u5173\u95ed\u7684\u4efb\u4f55\u53d8\u91cf\uff08\u7684\u5730\u5740\uff09\u7684\u5f15\u7528\u3002\u5f15\u7528\uff1a<\/p>\n<p>\u51fd\u6570\u6587\u5b57\u662f\u95ed\u5305\uff1a\u5b83\u4eec\u53ef\u4ee5\u5f15\u7528\u5468\u56f4\u51fd\u6570\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u3002\u7136\u540e\uff0c\u8fd9\u4e9b\u53d8\u91cf\u5728\u5468\u56f4\u7684\u51fd\u6570\u548c\u51fd\u6570\u6587\u5b57\u4e4b\u95f4\u5171\u4eab\uff0c\u5e76\u4e14\u53ea\u8981\u53ef\u8bbf\u95ee\uff0c\u5b83\u4eec\u5c31\u4f1a\u4e00\u76f4\u5b58\u5728\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u5728\u60a8\u7684\u793a\u4f8b\u4e2d\uff1a<\/p>\n<ol>\n<li>\n<p><code>f1 := foo1()<\/code><\/p>\n<ol>\n<li>\u4f7f <code>xValue<\/code> \u53d8\u91cf\u5b58\u5728\uff08\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u5728\u5806\u4e0a\u5206\u914d\u5b83\uff09\u3002\u5b83\u5c06\u4ee5\u5176\u7c7b\u578b\u7684\u96f6\u503c 0 \u5f00\u59cb\u3002<\/li>\n<li>\u4f7f\u53d8\u91cf <code>x<\/code> \u5b58\u5728\u5e76\u4e3a\u5176\u5206\u914d <code>xValue<\/code> \u7684\u5730\u5740\u3002<\/li>\n<li><code>defer<\/code>-red \u95ed\u5305\u8fd0\u884c\u5e76\u5c06\u503c 2 \u5206\u914d\u7ed9 <code>xValue<\/code>\u3002<\/li>\n<li>\u8fd4\u56de\u4e00\u4e2a\u5173\u95ed\u53d8\u91cf <code>x<\/code> \u7684\u95ed\u5305\u3002<\/li>\n<\/ol>\n<p>\u540e\u4e00\u70b9\u53ef\u80fd\u6709\u70b9\u68d8\u624b\uff1a\u7531\u4e8e\u8fd4\u56de\u7684\u95ed\u5305\u5f15\u7528\u4e86\u53d8\u91cf <code>x<\/code>\uff0c\u7f16\u8bd1\u5668\u4fdd\u8bc1\u5373\u4f7f\u5728 <code>foo<\/code> \u8fd4\u56de\u540e\u8be5\u53d8\u91cf\u4e5f\u5b58\u5728\u3002\u7531\u4e8e <code>x<\/code> \u5305\u542b <code>xValue<\/code> \u7684\u5730\u5740\uff08\u56e0\u6b64\u662f\u5bf9\u5b83\u7684\u5b9e\u65f6\u5f15\u7528\uff09\uff0c\u56e0\u6b64\u8be5\u5730\u5740\u4ecd\u7136\u5b58\u5728\uff0c\u5e76\u4e14\u4e0d\u80fd\u88ab\u5783\u573e\u6536\u96c6\u3002 \u4f7f\u7528\u76f8\u540c\u7684\u8f6c\u4e49\u5206\u6790\u65b9\u6cd5\uff0c\u7f16\u8bd1\u5668\u4fdd\u8bc1 <code>xValue<\/code> \u5728\u5176\u58f0\u660e\u7684\u51fd\u6570\u8fd4\u56de\u540e\u4ecd\u7136\u5b58\u5728\u3002<\/p>\n<\/li>\n<li>\n<p>\u60a8\u6267\u884c\u8fd4\u56de\u7684\u95ed\u5305\uff0c\u8be5\u95ed\u5305\u901a\u8fc7\u6307\u5411\u5b83\u7684\u6307\u9488\u4fee\u6539 <code>xValue<\/code> \u2013 \u8fd9\u91cc\u6ca1\u6709\u53d1\u751f\u4efb\u4f55\u9b54\u6cd5\u3002\u53e6\u5916\u4e24\u4e2a\u8c03\u7528\u6267\u884c\u76f8\u540c\u7684\u64cd\u4f5c\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u603b\u800c\u8a00\u4e4b\uff0c\u4e5f\u8bb8\u60a8\u88ab C++ \u77e5\u8bc6\u7eca\u5012\u4e86\uff0c\u4e00\u65e6\u4ece\u8be5\u51fd\u6570\u8fd4\u56de\u63a7\u5236\u6743\uff0c\u51fd\u6570\u4e2d\u58f0\u660e\u7684\u4efb\u4f55\u53d8\u91cf\u90fd\u5c06\u4e0d\u518d\u5b58\u5728\uff0c\u56e0\u6b64\u8be5\u51fd\u6570\u5916\u90e8\u5b58\u5728\u7684\u5bf9\u8be5\u53d8\u91cf\u7684\u4efb\u4f55\u5f15\u7528\u90fd\u5c06\u53d8\u4e3a\u65e0\u6548\u7684\u3002\u5728 Go \u4e2d\uff0c\u60c5\u51b5\u5e76\u975e\u5982\u6b64\uff1a\u5728\u8fd9\u65b9\u9762\uff0c\u8be5\u8bed\u8a00\u88ab\u663e\u5f0f\u5b9a\u4e49\u4e3a\u5b89\u5168\uff1a\u7f16\u8bd1\u5668\u786e\u4fdd\u4efb\u4f55\u53d8\u91cf\u90fd\u6709\u9002\u5f53\u7684\u5206\u914d\uff0c\u4ee5\u4fbf\u5728\u8fd4\u56de\uff08\u6216\u4ee5\u5176\u4ed6\u65b9\u5f0f\u4f20\u8fbe\uff09\u5bf9\u5b83\u7684\u5f15\u7528\u65f6\u5728\u521b\u5efa\u5b83\u7684\u51fd\u6570\u8c03\u7528\u4e2d\u751f\u5b58\u4e0b\u6765\u3002\u4ece\u8be5\u51fd\u6570\u8c03\u7528\u5230\u5916\u90e8\u4e16\u754c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u5c01\u95ed\u6240\u5bb9\u7eb3\u7684\u73af\u5883\u8303\u56f4\u662f\u4ec0\u4e48\uff1f \u5c01\u95ed\u6240\u5bb9\u7eb3\u7684\u73af\u5883\u8303\u56f4\u662f\u4ec0\u4e48\uff1f \u6765\u6e90\uff1astackoverflow 2024-04-25 13:42:34 0\u6d4f\u89c8 \u6536\u85cf \u5b66\u4e60\u77e5\u8bc6\u8981\u5584\u4e8e\u601d\u8003\uff0c\u601d\u8003\uff0c\u518d\u601d\u8003\uff01\u4eca\u5929\u7c73\u4e91\u5c0f\u7f16\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765\u300a\u5c01\u95ed\u6240\u5bb9\u7eb3\u7684\u73af\u5883\u8303\u56f4\u662f\u4ec0\u4e48\uff1f\u300b\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u5305\u542b\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6216\u51c6\u5907\u5b66\u4e60Golang\uff0c\u5c31\u90fd\u4e0d\u8981\u9519\u8fc7\u672c\u6587\u5566~\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff0c\u80fd\u5e2e\u52a9\u5230\u4f60\u5c31\u66f4\u597d\u4e86\uff01 \u95ee\u9898\u5185\u5bb9 \u6211\u6a21\u4eff\u7f51\u4e0a\u5173\u4e8e\u95ed\u5305\u7684\u6559\u7a0b\uff0c\u5199\u4e86\u4e0b\u9762\u7684\u4ee3\u7801\u3002 func foo1() func() { xvalue := 1 x := &amp;xvalue defer func() { xvalue = 2 }() return func() { *x = *x + 1 fmt.printf(&#8220;foo1 val = %d\\n&#8221;, *x) } } func main() { f1 := foo1() f1() f1() [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-42696","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42696","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=42696"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42696\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=42696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=42696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=42696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}