{"id":207349,"date":"2025-07-08T10:06:46","date_gmt":"2025-07-08T02:06:46","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207349\/"},"modified":"2025-07-08T10:06:46","modified_gmt":"2025-07-08T02:06:46","slug":"%e8%87%b4%e5%91%bd%e9%94%99%e8%af%af%ef%bc%9a%e6%89%80%e6%9c%89-goroutine-%e9%83%bd%e5%9c%a8%e7%9d%a1%e8%a7%89-%e6%ad%bb%e9%94%81%ef%bc%81%e5%8d%8f%e7%a8%8b-1-%ef%bc%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207349\/","title":{"rendered":"\u81f4\u547d\u9519\u8bef\uff1a\u6240\u6709 goroutine \u90fd\u5728\u7761\u89c9 &#8211; \u6b7b\u9501\uff01\u534f\u7a0b 1 \uff1a"},"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>\u81f4\u547d\u9519\u8bef\uff1a\u6240\u6709 goroutine \u90fd\u5728\u7761\u89c9 &#8211; \u6b7b\u9501\uff01\u534f\u7a0b 1 \uff1a<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 21:18:39<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u81f4\u547d\u9519\u8bef\uff1a\u6240\u6709 goroutine \u90fd\u5728\u7761\u89c9 &#8211; \u6b7b\u9501\uff01\u534f\u7a0b 1 \uff1a\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u8fd0\u884c goroutine \u53ca\u5176\u901a\u9053\uff0c\u4f46\u4ecd\u7136\u51fa\u73b0\u9519\u8bef<\/p>\n<pre>\npackage main\n\nimport (\n    \"fmt\"\n    \"sync\"\n    \"time\"\n)\n\nfunc foo(c chan int, wg *sync.WaitGroup) {\n    c &lt;- 45\n    time.Sleep(time.Second * 4)\n    c &lt;- 122\n    wg.Done()\n\n}\n\nfunc main() {\n    canal := make(chan int)\n    var wg sync.WaitGroup\n\n    wg.Add(2)\n    go foo(canal,&amp;wg)\n    go foo(canal,&amp;wg)\n    wg.Wait()\n\n    for a := range canal {\n        fmt.Println(a)\n    }\n\n}\n<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p><code>make(chan int)<\/code> \u521b\u5efa\u4e00\u4e2a \u3002\u8fd9\u610f\u5473\u7740\u6240\u6709\u53d1\u9001\u548c\u63a5\u6536\u64cd\u4f5c\u5458\u90fd\u4f1a\u7b49\u5f85\uff0c\u76f4\u5230\u53e6\u4e00\u65b9\u51c6\u5907\u597d\u3002<\/p>\n<p>\u5f53\u4f60\u5728 Goroutine \u4e2d\u8c03\u7528 <code>c &lt;- 45<\/code> \u65f6\uff0c\u8be5 Goroutine \u5c06\u7b49\u5f85\uff0c\u76f4\u5230\u6709\u4eba\u4ece\u901a\u9053\u63a5\u6536\u6570\u636e\u3002\u4f46\u662f\uff0c\u76f4\u5230 <code>range canal<\/code> \u8bed\u53e5\u5361\u5728 <code>wg.Wait()<\/code> \u540e\u9762\u4e4b\u524d\uff0c\u4e0d\u5b58\u5728\u4efb\u4f55\u6d88\u8d39\u8005\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u60a8\u7684\u4e24\u4e2a goroutine \u88ab\u56f0\u5728\u53d1\u9001\u5230\u901a\u9053\u7684\u8fc7\u7a0b\u4e2d\uff0c\u800c main \u6b63\u5728\u7b49\u5f85 goroutine \u5b8c\u6210\uff0c\u7136\u540e\u518d\u4ece\u901a\u9053\u6d88\u8d39\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u5728\u5355\u72ec\u7684 goroutine \u4e2d\u4f7f\u7528\u901a\u9053\u4e2d\u7684\u5143\u7d20\uff08\u5728 <code>wg.Wait()<\/code> \u4e4b\u524d\uff09\uff0c\u6216\u8005\u521b\u5efa\u4e00\u4e2a\u7f13\u51b2\u901a\u9053\uff08\u4f8b\u5982\uff1a<code>make(chan int, 10)<\/code>\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/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-207349","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207349","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=207349"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207349\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}