{"id":207381,"date":"2025-07-08T12:58:29","date_gmt":"2025-07-08T04:58:29","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207381\/"},"modified":"2025-07-08T12:58:29","modified_gmt":"2025-07-08T04:58:29","slug":"%e5%81%9c%e6%ad%a2%e6%97%a0%e9%99%90%e6%9c%9f%e5%86%99%e5%85%a5%e9%80%9a%e9%81%93%e7%9a%84-goroutine","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207381\/","title":{"rendered":"\u505c\u6b62\u65e0\u9650\u671f\u5199\u5165\u901a\u9053\u7684 goroutine"},"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>\u505c\u6b62\u65e0\u9650\u671f\u5199\u5165\u901a\u9053\u7684 goroutine<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 08:18:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u505c\u6b62\u65e0\u9650\u671f\u5199\u5165\u901a\u9053\u7684 goroutine\u300b\uff0c\u8986\u76d6\u4e86Golang\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6709\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65e0\u9650\u671f\u586b\u5145\u901a\u9053\u7684 goroutine\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>func foo() &lt;-chan int {\n  ch := make(chan int) \n  go func() {\n    defer close(ch)\n    for {\n      ch &lt;- 1\n    } \n  }() \n  return ch\n}<\/pre>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u6d88\u8d39\u8005\u60f3\u5728\u4e00\u6bb5\u65f6\u95f4\u540e\u505c\u6b62\uff1a<\/p>\n<pre>ch:=foo() \n&lt;-ch\n&lt;-ch\n\/\/ done<\/pre>\n<p>\u73b0\u5728\u6211\u60f3\u6e05\u7406 goroutine \u8d44\u6e90\uff0c\u5305\u62ec\u901a\u9053\u3002\u6211\u5c1d\u8bd5\u4e3a\u6b64\u6dfb\u52a0\u4e00\u4e2a\u201c\u5b8c\u6210\u201d\u901a\u9053\uff0c\u4f46\u968f\u540e\u6211\u9047\u5230\u4e86\u50f5\u5c40\uff1a<\/p>\n<pre>func Foo() (&lt;-chan int, chan&lt;- bool) {\n  ch := make(chan int)\n  done := make(chan bool)\n  go func() {\n    defer close(ch)\n    for {\n      select {\n      case &lt;-done:\n          return\n      default:\n          ch &lt;- 1\n      }\n    } \n  }() \n  return ch, done\n}\n\nfunc main() {\n  ch, done := Foo()\n  &lt;-ch\n  &lt;-ch\n  done &lt;- true\n  \/\/ HERE\n}<\/pre>\n<p>\u73b0\u5728\uff0c\u5b83\u4f3c\u4e4e\u53ef\u4ee5\u5de5\u4f5c\uff0c\u4f46\u8fd9\u53ea\u662f\u56e0\u4e3a\u7a0b\u5e8f\u9000\u51fa\uff0c\u5982\u679c\u6211\u7528\u4e00\u4e9bio\u64cd\u4f5c\u66ff\u6362<code>\/\/ here<\/code>\uff08\u4f8b\u5982\uff1a<code>http.get\uff08\u201chttp:\/\/google.com\u201d\uff09<\/code>\uff09 \uff0c\u6211\u9762\u4e34\u6b7b\u9501\uff08<code>fatal \u9519\u8bef\uff1a\u6240\u6709 goroutine \u90fd\u5728\u7761\u89c9 - \u6b7b\u9501\uff01<\/code>\uff09\u3002 \u6211\u60f3\u77e5\u9053\u662f\u5426\u6709\u53e6\u4e00\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6e05\u7406\u7531 <code>foo<\/code> \u51fd\u6570\u521b\u5efa\u7684\u751f\u6210\u7684 goroutine \u548c\u901a\u9053\u3002<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u53ea\u9700\u5728\u542f\u52a8\u7684 goroutine \u4e2d\u5c06 <code>default<\/code> \u66ff\u6362\u4e3a <code>case<\/code> \u5373\u53ef\uff1a<\/p>\n<pre>func Foo() (&lt;-chan int, chan&lt;- bool) {\n  ch := make(chan int)\n  done := make(chan bool)\n  go func() {\n    defer close(ch)\n    for {\n      select {\n      case &lt;-done:\n          return\n      case ch &lt;- 1:\n      }\n    } \n  }() \n  return ch, done\n}\n<\/pre>\n<p>\u539f\u4ee3\u7801\u5728<code>default<\/code>\u60c5\u51b5\u4e0b\u51fa\u73b0\u6b7b\u9501\u7684\u539f\u56e0\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u5f53\u6ca1\u6709\u5176\u4ed6\u5e76\u53d1\u8fd0\u884c\u7684 goroutine \u5199\u5165 <code>done<\/code> \u901a\u9053\u65f6\uff0c\u542f\u52a8\u7684\u5199\u5165 <code>ch<\/code> \u901a\u9053\u7684 goroutines \u4f1a\u7acb\u5373\u8f6c\u5230 <code>default<\/code> \u60c5\u51b5\u3002\u7136\u540e goroutine \u4f1a\u963b\u585e\u5728 <code>ch &lt;- 1<\/code> \u884c\uff0c\u76f4\u5230\u5176\u4ed6 goroutine \u4ece <code>ch<\/code> \u8bfb\u53d6\u503c\u3002<\/li>\n<li>\u4e3b\u534f\u7a0b\u4ece <code>ch<\/code> \u8bfb\u53d6\u4e24\u6b21\u3002\u8fd9\u4f1a\u5bfc\u81f4\u5728\u542f\u52a8\u7684 goroutine \u5904\u6709\u4e24\u4e2a\u6210\u529f\u7684\u6267\u884c\u5faa\u73af\u3002\u7136\u540e\u5b83\u5c1d\u8bd5\u5199\u5165 <code>done<\/code>\u3002\u6b64\u65f6\u542f\u52a8\u7684goroutine\u53ef\u80fd\u5df2\u7ecf\u68c0\u67e5\u4e86<code>select<\/code>\u8bed\u53e5\uff0c\u9677\u5165<code>default<\/code>\u60c5\u51b5\u5e76\u963b\u585e\u5728<code>ch &lt;- 1<\/code>\u884c\u3002\u56e0\u6b64\u4e3b goroutine \u4e5f\u4f1a\u65e0\u9650\u671f\u5730\u963b\u585e\u5728 <code>done &lt;- true<\/code> \u884c\u3002\u8fd9\u4f1a\u5bfc\u81f4\u50f5\u5c40\u3002<\/li>\n<\/ul>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u505c\u6b62\u65e0\u9650\u671f\u5199\u5165\u901a\u9053\u7684 goroutine\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591aGolang\u77e5\u8bc6\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-207381","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207381","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=207381"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207381\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}