{"id":43069,"date":"2024-12-01T09:36:10","date_gmt":"2024-12-01T01:36:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/43069\/"},"modified":"2024-12-01T09:36:10","modified_gmt":"2024-12-01T01:36:10","slug":"%e4%bb%8e%e4%b8%8d%e5%90%8c%e7%9a%84-go-%e4%be%8b%e7%a8%8b%e5%b0%86%e6%95%b0%e6%8d%ae%e5%86%99%e5%85%a5%e5%90%8c%e4%b8%80%e9%80%9a%e9%81%93%e6%97%a0%e9%9c%80%e7%ad%89%e5%be%85%e7%bb%84%e5%8d%b3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/43069\/","title":{"rendered":"\u4ece\u4e0d\u540c\u7684 go \u4f8b\u7a0b\u5c06\u6570\u636e\u5199\u5165\u540c\u4e00\u901a\u9053\u65e0\u9700\u7b49\u5f85\u7ec4\u5373\u53ef\u6b63\u5e38\u5de5\u4f5c"},"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>\u4ece\u4e0d\u540c\u7684 go \u4f8b\u7a0b\u5c06\u6570\u636e\u5199\u5165\u540c\u4e00\u901a\u9053\u65e0\u9700\u7b49\u5f85\u7ec4\u5373\u53ef\u6b63\u5e38\u5de5\u4f5c<\/span><\/p>\n<h1>\u4ece\u4e0d\u540c\u7684 go \u4f8b\u7a0b\u5c06\u6570\u636e\u5199\u5165\u540c\u4e00\u901a\u9053\u65e0\u9700\u7b49\u5f85\u7ec4\u5373\u53ef\u6b63\u5e38\u5de5\u4f5c<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-29 09:15: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>\u5728Golang\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300a\u4ece\u4e0d\u540c\u7684 go \u4f8b\u7a0b\u5c06\u6570\u636e\u5199\u5165\u540c\u4e00\u901a\u9053\u65e0\u9700\u7b49\u5f85\u7ec4\u5373\u53ef\u6b63\u5e38\u5de5\u4f5c\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5728\u7b49\u5f85 wg.wait() \u540e\uff0c\u4f7f\u7528\u5e26\u6709 waitgroup \u7684\u591a\u4e2a go \u4f8b\u7a0b\u5c06\u6570\u636e\u5199\u5165\u540c\u4e00\u901a\u9053\u65f6\uff0c\u51fa\u73b0\u5f02\u5e38\uff0c\u8868\u793a\u6240\u6709 go \u4f8b\u7a0b\u90fd\u5904\u4e8e\u7761\u7720\u72b6\u6001\u6216 deedlock\u3002 <\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"runtime\"\n    \"sync\"\n)\n\nvar wg sync.waitgroup\n\nfunc createmultipleroutines() {\n    ch := make(chan int)\n\n    for i := 0; i &lt; 10; i++ { \/\/ creates 10 go routines and adds to waitgroup\n        wg.add(1)\n        go func() {\n            for j := 0; j &lt; 10; j++ {\n                ch &lt;- j\n            }\n            wg.done() \/\/ indication of go routine is done to main routine\n        }()\n    }\n\n    fmt.println(runtime.numgoroutine())\n    wg.wait()           \/\/wait for all go routines to complete\n    close(ch)           \/\/ closing channel after completion of wait fo go routines\n    for v := range ch { \/\/ range can be used since channel is closed\n        fmt.println(v)\n    }\n    fmt.println(\"about to exit program ...\")\n}<\/pre>\n<p>\u5f53\u5c1d\u8bd5\u5728\u6ca1\u6709\u7b49\u5f85\u7ec4\u7684\u60c5\u51b5\u4e0b\u5b9e\u73b0\u6b64\u76ee\u7684\u65f6\uff0c\u6211\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u5c06\u6570\u636e\u63a8\u9001\u5230\u901a\u9053\u7684\u786e\u5207\u6b21\u6570\u6765\u4ece\u901a\u9053\u8bfb\u53d6\u6570\u636e\uff0c\u4f46\u6211\u65e0\u6cd5\u8303\u56f4\uff0c\u56e0\u4e3a\u5f53\u6211\u4eec\u5173\u95ed\u901a\u9053\u65f6\u4f1a\u51fa\u73b0\u6050\u614c\u3002\u8fd9\u662f\u793a\u4f8b\u4ee3\u7801<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"runtime\"\n)\n\nfunc main() {\n    ch := make(chan int)\n\n\n    for i := 0; i &lt; 10; i++ { \/\/ creates 10 go routines and adds to waitgroup\n        go func(i int) {\n            for j := 0; j &lt; 10; j++ {\n                ch &lt;- j * i\n            }\n        }(i)\n    }\n\n    fmt.Println(runtime.NumGoroutine())\n\n    for v := 0; v &lt; 100; v++ {\n        fmt.Println(&lt;-ch)\n    }\n    fmt.Println(\"About to exit program ...\")\n}<\/pre>\n<p>\u6211\u60f3\u4e86\u89e3\u4e3a\u4ec0\u4e48\u5904\u4e8e\u7b49\u5f85\u72b6\u6001\u7684 waitgroup \u4ecd\u5728\u7b49\u5f85\uff0c\u5373\u4f7f\u6240\u6709 go \u4f8b\u7a0b\u90fd\u53d1\u51fa done() \u4fe1\u53f7\uff0c\u8fd9\u53c8\u4f7f go \u4f8b\u7a0b\u7684\u6570\u91cf\u4e3a\u96f6<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>chan<\/code> <em>\u4e0d\u4fdd\u5b58<\/em>\u4efb\u4f55\u9879\u76ee\uff0c\u56e0\u6b64\u6240\u6709 go \u4f8b\u7a0b\u5728\u53d1\u9001\u65f6\u90fd\u4f1a\u88ab\u963b\u6b62\uff0c\u76f4\u5230\u4ece\u4e2d\u8bfb\u53d6\u5185\u5bb9\u3002\u4ed6\u4eec\u4ece\u672a\u771f\u6b63\u5230\u8fbe <code>wg.done()<\/code> \u8bed\u53e5\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848\u662f\u5728\u5b83\u81ea\u5df1\u7684 go \u4f8b\u7a0b\u4e2d\u5173\u95ed\u901a\u9053\u3002\u5c06 <code>wg.wait()<\/code> \u548c <code>close(ch)<\/code> \u884c\u5305\u88c5\u5982\u4e0b\uff1a<\/p>\n<pre>go func() {\n    wg.wait() \/\/wait for all go routines to complete\n    close(ch) \/\/ closing channel after completion of wait fo go routines\n}()<\/pre>\n<p>\u7136\u540e\u60a8\u53ef\u4ee5\u8303\u56f4\u8986\u76d6\u901a\u9053\uff0c\u8be5\u901a\u9053\u4ec5\u5728\u6240\u6709\u53d1\u9001 go \u4f8b\u7a0b\u5b8c\u6210\u540e\u624d\u4f1a\u5173\u95ed\uff08\u5e76\u4e14\u9690\u5f0f\u5730\u6536\u5230\u6240\u6709\u503c\uff09\u3002<\/p>\n<p>\u6211\u8ba4\u4e3a\u60a8\u7684\u539f\u59cb\u4ee3\u7801\u6709\u4e00\u4e9b\u95ee\u9898\u3002 <\/p>\n<ol>\n<li>\u60a8\u5c06\u5728\u8bfb\u53d6\u9891\u9053\u4e4b\u524d\u5173\u95ed\u8be5\u9891\u9053\u3002<\/li>\n<li>\u7531\u4e8e\u60a8\u7684\u901a\u9053\u7684\u201c\u5927\u5c0f\u201d\u4e3a 1\uff0c\u56e0\u6b64\u60a8\u65e0\u6cd5\u83b7\u5f97\u4f7f\u7528 10 \u4e2a goroutine \u7684\u4f18\u52bf\u3002\u56e0\u6b64\uff0c\u4e00\u4e2a goroutine \u4e00\u6b21\u4f1a\u4ea7\u751f\u4e00\u4e2a\u7ed3\u679c\u3002<\/li>\n<\/ol>\n<p>\u6211\u7684\u89e3\u51b3\u65b9\u6848\u662f\u751f\u6210\u4e00\u4e2a\u65b0\u7684 goroutine \u6765\u76d1\u89c6 10 \u4e2a goroutine \u662f\u5426\u5b8c\u6210\u5176\u5de5\u4f5c\u3002\u5728\u90a3\u91cc\u60a8\u5c06\u4f7f\u7528\u60a8\u7684 <code>waitgroup<\/code>\u3002<\/p>\n<p>\u90a3\u4e48\u4ee3\u7801\u5c06\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<p> 557188\u200b\u200b879518 <\/p>\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\u4ece\u4e0d\u540c\u7684 go \u4f8b\u7a0b\u5c06\u6570\u636e\u5199\u5165\u540c\u4e00\u901a\u9053\u65e0\u9700\u7b49\u5f85\u7ec4\u5373\u53ef\u6b63\u5e38\u5de5\u4f5c\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\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; &gt; &gt; \u4ece\u4e0d\u540c\u7684 go \u4f8b\u7a0b\u5c06\u6570\u636e\u5199\u5165\u540c\u4e00\u901a\u9053\u65e0\u9700\u7b49\u5f85\u7ec4\u5373\u53ef\u6b63\u5e38\u5de5\u4f5c \u4ece\u4e0d\u540c\u7684 go \u4f8b\u7a0b\u5c06\u6570\u636e\u5199\u5165\u540c\u4e00\u901a\u9053\u65e0\u9700\u7b49\u5f85\u7ec4\u5373\u53ef\u6b63\u5e38\u5de5\u4f5c \u6765\u6e90\uff1astackoverflow 2024-04-29 09:15:38 0\u6d4f\u89c8 \u6536\u85cf \u5728Golang\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300a\u4ece\u4e0d\u540c\u7684 go \u4f8b\u7a0b\u5c06\u6570\u636e\u5199\u5165\u540c\u4e00\u901a\u9053\u65e0\u9700\u7b49\u5f85\u7ec4\u5373\u53ef\u6b63\u5e38\u5de5\u4f5c\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002 \u95ee\u9898\u5185\u5bb9 \u5728\u7b49\u5f85 wg.wait() \u540e\uff0c\u4f7f\u7528\u5e26\u6709 waitgroup \u7684\u591a\u4e2a go \u4f8b\u7a0b\u5c06\u6570\u636e\u5199\u5165\u540c\u4e00\u901a\u9053\u65f6\uff0c\u51fa\u73b0\u5f02\u5e38\uff0c\u8868\u793a\u6240\u6709 go \u4f8b\u7a0b\u90fd\u5904\u4e8e\u7761\u7720\u72b6\u6001\u6216 deedlock\u3002 package main import ( &#8220;fmt&#8221; &#8220;runtime&#8221; &#8220;sync&#8221; ) var wg sync.waitgroup func createmultipleroutines() { ch := make(chan int) for i := 0; i &lt; 10; [&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-43069","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43069","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=43069"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43069\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=43069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=43069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=43069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}