{"id":6579,"date":"2024-09-20T19:14:32","date_gmt":"2024-09-20T11:14:32","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6579"},"modified":"2024-09-20T19:14:32","modified_gmt":"2024-09-20T11:14:32","slug":"%e5%bf%85%e7%9f%a5%e5%bf%85%e4%bc%9a-go-%e9%80%83%e9%80%b8%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6579","title":{"rendered":"\u5fc5\u77e5\u5fc5\u4f1a &#8211; Go \u9003\u9038\u5206\u6790"},"content":{"rendered":"<p>\u9003\u9038\u5206\u6790\uff08Escape Analysis\uff09\u662f Go \u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u671f\u95f4\u8fdb\u884c\u7684\u4e00\u79cd\u4f18\u5316\u5206\u6790\uff0c\u7528\u4e8e\u786e\u5b9a\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\uff0c\u4ee5\u53ca\u53d8\u91cf\u662f\u5426\u9700\u8981\u4ece\u6808\u4e0a\u5206\u914d\u5230\u5806\u4e0a\u3002\u5f53\u67d0\u4e2a\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u8d85\u51fa\u4e86\u5176\u6240\u5728\u7684\u51fd\u6570\u6216 Goroutine \u7684\u8303\u56f4\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c06\u8be5\u53d8\u91cf\u201c\u9003\u9038\u201d\u5230\u5806\u4e0a\u3002\u5426\u5219\uff0c\u53d8\u91cf\u4f1a\u5206\u914d\u5728\u6808\u4e0a\u3002<\/p>\n<h3>\u5177\u4f53\u6d41\u7a0b<\/h3>\n<ul>\n<li><strong>\u6808\u5206\u914d<\/strong>\uff1a\u5c40\u90e8\u53d8\u91cf\u901a\u5e38\u5206\u914d\u5728\u51fd\u6570\u7684\u6808\u5e27\u4e2d\u3002\u6808\u4e0a\u7684\u5185\u5b58\u5206\u914d\u548c\u56de\u6536\u975e\u5e38\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u662f\u57fa\u4e8e\u6808\u7684\u201c\u63a8\u5165\u201d\u548c\u201c\u5f39\u51fa\u201d\u64cd\u4f5c\u3002\u51fd\u6570\u8c03\u7528\u7ed3\u675f\u65f6\uff0c\u6808\u4e0a\u7684\u5185\u5b58\u4f1a\u81ea\u52a8\u91ca\u653e\u3002<\/li>\n<li><strong>\u5806\u5206\u914d<\/strong>\uff1a\u5982\u679c\u7f16\u8bd1\u5668\u901a\u8fc7\u9003\u9038\u5206\u6790\u53d1\u73b0\u67d0\u4e2a\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u8d85\u51fa\u4e86\u5f53\u524d\u51fd\u6570\u7684\u4f5c\u7528\u57df\uff0c\u5b83\u4f1a\u5c06\u8be5\u53d8\u91cf\u5206\u914d\u5230\u5806\u4e0a\uff0c\u4ee5\u4fbf\u5728\u51fd\u6570\u8fd4\u56de\u540e\u4ecd\u80fd\u7ee7\u7eed\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u5f53\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\u7684\u5f15\u7528\u88ab\u8fd4\u56de\u6216\u4f20\u9012\u7ed9\u5176\u4ed6 Goroutine \u65f6\uff0c\u8fd9\u4e2a\u53d8\u91cf\u4f1a\u9003\u9038\u5230\u5806\u4e0a\u3002<\/li>\n<\/ul>\n<h3>\u9003\u9038\u7684\u5e38\u89c1\u60c5\u51b5<\/h3>\n<ol>\n<li>\n<p><strong>\u8fd4\u56de\u5c40\u90e8\u53d8\u91cf\u7684\u6307\u9488<\/strong>\uff1a<\/p>\n<pre><code class=\"language-go\">func foo() *int {\n   x := 10\n   return &x\n}<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>x<\/code> \u662f\u5c40\u90e8\u53d8\u91cf\uff0c\u4f46\u5b83\u7684\u5730\u5740\u88ab\u8fd4\u56de\uff0c\u56e0\u6b64 <code>x<\/code> \u9700\u8981\u5206\u914d\u5230\u5806\u4e0a\uff0c\u786e\u4fdd\u5728 <code>foo()<\/code> \u51fd\u6570\u7ed3\u675f\u540e\uff0c\u8fd4\u56de\u7684\u6307\u9488\u4ecd\u7136\u6709\u6548\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u95ed\u5305\u6355\u83b7\u5916\u90e8\u53d8\u91cf<\/strong>\uff1a<\/p>\n<pre><code class=\"language-go\">func foo() func() int {\n   x := 10\n   return func() int {\n       return x\n   }\n}<\/code><\/pre>\n<p>\u95ed\u5305\u4e2d\u7684 <code>x<\/code> \u88ab\u6355\u83b7\uff0c\u8d85\u51fa\u4e86 <code>foo()<\/code> \u7684\u4f5c\u7528\u57df\uff0c\u56e0\u6b64 <code>x<\/code> \u4f1a\u9003\u9038\u5230\u5806\u4e0a\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u4f20\u9012\u5f15\u7528\u7ed9 Goroutine<\/strong>\uff1a<\/p>\n<pre><code class=\"language-go\">func foo() {\n   x := 10\n   go func() {\n       fmt.Println(x)\n   }()\n}<\/code><\/pre>\n<p><code>x<\/code> \u88ab Goroutine \u4f7f\u7528\uff0c\u867d\u7136\u5b83\u5728 <code>foo()<\/code> \u4e2d\u5b9a\u4e49\uff0c\u4f46\u56e0\u4e3a Goroutine \u53ef\u80fd\u5728 <code>foo()<\/code> \u8fd4\u56de\u540e\u4ecd\u7136\u8fd0\u884c\uff0c<code>x<\/code> \u9700\u8981\u9003\u9038\u5230\u5806\u4e0a\u3002<\/p>\n<\/li>\n<\/ol>\n<h3>\u5982\u4f55\u67e5\u770b\u9003\u9038\u5206\u6790<\/h3>\n<p>Go \u63d0\u4f9b\u4e86 <code>go build<\/code> \u548c <code>go run<\/code> \u7684 <code>-gcflags<\/code> \u53c2\u6570\uff0c\u53ef\u4ee5\u67e5\u770b\u9003\u9038\u5206\u6790\u7684\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-bash\">go build -gcflags=&quot;-m&quot;<\/code><\/pre>\n<p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u8f93\u51fa\u7f16\u8bd1\u671f\u95f4\u7684\u4f18\u5316\u4fe1\u606f\uff0c\u5305\u542b\u9003\u9038\u5206\u6790\u7684\u7ed3\u679c\u3002\u5982\u679c\u67d0\u4e2a\u53d8\u91cf\u53d1\u751f\u4e86\u9003\u9038\uff0c\u7f16\u8bd1\u5668\u4f1a\u63d0\u793a\u7c7b\u4f3c &quot;moved to heap&quot; \u7684\u4fe1\u606f\u3002<\/p>\n<h3>\u4f18\u5316\u9003\u9038<\/h3>\n<p>\u7406\u89e3\u9003\u9038\u5206\u6790\u7684\u539f\u7406\u53ef\u4ee5\u5e2e\u52a9\u4f60\u4f18\u5316\u4ee3\u7801\uff0c\u5c3d\u91cf\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u5806\u5206\u914d\u3002\u4f8b\u5982\uff0c\u907f\u514d\u5728\u95ed\u5305\u6216 Goroutine \u4e2d\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\u7684\u5f15\u7528\uff0c\u51cf\u5c11\u5806\u5206\u914d\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9003\u9038\u5206\u6790\uff08Escape Analysis\uff09\u662f Go \u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u671f\u95f4\u8fdb\u884c\u7684\u4e00\u79cd\u4f18\u5316\u5206\u6790\uff0c\u7528\u4e8e\u786e\u5b9a\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6579\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[123],"tags":[],"class_list":["post-6579","post","type-post","status-publish","format-standard","hentry","category-123"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6579","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6579"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6579\/revisions"}],"predecessor-version":[{"id":6580,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6579\/revisions\/6580"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}