{"id":41966,"date":"2024-12-01T08:09:21","date_gmt":"2024-12-01T00:09:21","guid":{"rendered":"https:\/\/fwq.ai\/blog\/41966\/"},"modified":"2024-12-01T08:09:21","modified_gmt":"2024-12-01T00:09:21","slug":"%e5%90%8c%e6%97%b6%e6%94%b6%e5%90%ac%e5%a4%9a%e4%b8%aa-go-%e9%a2%91%e9%81%93","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/41966\/","title":{"rendered":"\u540c\u65f6\u6536\u542c\u591a\u4e2a go \u9891\u9053"},"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>\u540c\u65f6\u6536\u542c\u591a\u4e2a go \u9891\u9053<\/span><\/p>\n<h1>\u540c\u65f6\u6536\u542c\u591a\u4e2a go \u9891\u9053<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 08:06:28<\/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=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u5404\u4f4d\u5c0f\u4f19\u4f34\u4eec\uff0c\u5927\u5bb6\u597d\u5440\uff01\u770b\u770b\u4eca\u5929\u6211\u53c8\u7ed9\u5404\u4f4d\u5e26\u6765\u4e86\u4ec0\u4e48\u6587\u7ae0\uff1f\u672c\u6587\u6807\u9898<span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u662f<\/span><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u540c\u65f6\u6536\u542c\u591a\u4e2a go \u9891\u9053\u300b<\/span>\uff0c\u5f88\u660e\u663e\u662f\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u7684\u6587\u7ae0\u54c8\u54c8\u54c8\uff0c\u5176\u4e2d\u5185\u5bb9\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u80fd\u5e2e\u5230\u4f60\uff0c\u89c9\u5f97\u5f88\u4e0d\u9519\u7684\u8bdd\uff0c\u6b22\u8fce\u5404\u4f4d\u591a\u591a\u70b9\u8bc4\u548c\u5206\u4eab\uff01<\/span><\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5047\u8bbe\u6211\u6709\u4e00\u4e2a go \u63a5\u6536\u901a\u9053\u3002\u6709\u6ca1\u6709\u4e00\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u6211\u540c\u65f6\u6536\u542c\u6240\u6709\u5185\u5bb9\uff1f\u4f8b\u5982\uff1a<\/p>\n<pre>channels := make([]&lt;-chan int, 0, N)\n\/\/ fill the slice with channels\nfor _, channel := range channels {\n  &lt;-channel\n}<\/pre>\n<p>\u8fd9\u662f\u6211\u80fd\u505a\u5230\u7684\u6700\u63a5\u8fd1\u7684\u4e8b\u60c5\u3002\u4f46\u662f\uff0c\u6b64\u5b9e\u73b0\u53d6\u51b3\u4e8e\u5207\u7247\u5143\u7d20\u7684\u987a\u5e8f\u3002 <\/p>\n<p>\u4e3a\u4e86\u6e05\u695a\u8d77\u89c1\uff0c\u6211\u4e0d\u9700\u8981\u77e5\u9053 go \u901a\u9053\u7684\u503c\u3002\u6211\u53ea\u9700\u8981\u77e5\u9053\u4ed6\u4eec\u90fd\u5b8c\u6210\u4e86\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528sync\u5305\u6765\u521b\u5efa\u7b49\u5f85\u7ec4\u3002 \u6dfb\u52a0\u5230 waitgroup \u540e\uff0c\u4e3a\u6bcf\u4e2a\u901a\u9053\u542f\u52a8\u4e00\u4e2a goroutine\u3002 \u6700\u540e\uff0c\u5f53\u901a\u9053\u5b8c\u6210\u65f6\uff0c\u5b83\u4f1a\u9012\u51cf waitgroup\u3002\u547c\u53eb\u8005\u53ea\u662f\u5728\u7b49\u5f85\u7ec4\u4e2d\u7b49\u5f85\u3002\u8fd9\u662f \u3002<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n    \"time\"\n)\n\nfunc main() {\n    wg       := sync.WaitGroup{}\n    numChans := 10\n    chans    := make([]chan interface{}, numChans)\n\n    for i:=0;i&lt;numChans;i++ {\n      chans[i] = make(chan interface{})\n      wg.Add(1) \n      go func(c chan interface{}) {\n        defer wg.Done()     \n        &lt;-c \n      }(chans[i])\n    }\n\n    \/\/simulate closing channels\n    go func(){\n       time.Sleep(time.Second)\n       fmt.Println(\"closing\")\n        for _, ch := range chans{\n            close(ch)\n        }       \n    }()\n\n    fmt.Println(\"waiting\")\n    wg.Wait()\n}<\/pre>\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\u540c\u65f6\u6536\u542c\u591a\u4e2a go \u9891\u9053\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; \u540c\u65f6\u6536\u542c\u591a\u4e2a go \u9891\u9053 \u540c\u65f6\u6536\u542c\u591a\u4e2a go \u9891\u9053 \u6765\u6e90\uff1astackoverflow 2024-04-22 08:06:28 0\u6d4f\u89c8 \u6536\u85cf \u5404\u4f4d\u5c0f\u4f19\u4f34\u4eec\uff0c\u5927\u5bb6\u597d\u5440\uff01\u770b\u770b\u4eca\u5929\u6211\u53c8\u7ed9\u5404\u4f4d\u5e26\u6765\u4e86\u4ec0\u4e48\u6587\u7ae0\uff1f\u672c\u6587\u6807\u9898\u662f\u300a\u540c\u65f6\u6536\u542c\u591a\u4e2a go \u9891\u9053\u300b\uff0c\u5f88\u660e\u663e\u662f\u5173\u4e8eGolang\u7684\u6587\u7ae0\u54c8\u54c8\u54c8\uff0c\u5176\u4e2d\u5185\u5bb9\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\uff0c\u5982\u679c\u80fd\u5e2e\u5230\u4f60\uff0c\u89c9\u5f97\u5f88\u4e0d\u9519\u7684\u8bdd\uff0c\u6b22\u8fce\u5404\u4f4d\u591a\u591a\u70b9\u8bc4\u548c\u5206\u4eab\uff01 \u95ee\u9898\u5185\u5bb9 \u5047\u8bbe\u6211\u6709\u4e00\u4e2a go \u63a5\u6536\u901a\u9053\u3002\u6709\u6ca1\u6709\u4e00\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u6211\u540c\u65f6\u6536\u542c\u6240\u6709\u5185\u5bb9\uff1f\u4f8b\u5982\uff1a channels := make([]&lt;-chan int, 0, N) \/\/ fill the slice with channels for _, channel := range channels { &lt;-channel } \u8fd9\u662f\u6211\u80fd\u505a\u5230\u7684\u6700\u63a5\u8fd1\u7684\u4e8b\u60c5\u3002\u4f46\u662f\uff0c\u6b64\u5b9e\u73b0\u53d6\u51b3\u4e8e\u5207\u7247\u5143\u7d20\u7684\u987a\u5e8f\u3002 \u4e3a\u4e86\u6e05\u695a\u8d77\u89c1\uff0c\u6211\u4e0d\u9700\u8981\u77e5\u9053 go \u901a\u9053\u7684\u503c\u3002\u6211\u53ea\u9700\u8981\u77e5\u9053\u4ed6\u4eec\u90fd\u5b8c\u6210\u4e86\u3002 \u89e3\u51b3\u65b9\u6848 \u60a8\u53ef\u4ee5\u4f7f\u7528sync\u5305\u6765\u521b\u5efa\u7b49\u5f85\u7ec4\u3002 \u6dfb\u52a0\u5230 waitgroup \u540e\uff0c\u4e3a\u6bcf\u4e2a\u901a\u9053\u542f\u52a8\u4e00\u4e2a goroutine\u3002 \u6700\u540e\uff0c\u5f53\u901a\u9053\u5b8c\u6210\u65f6\uff0c\u5b83\u4f1a\u9012\u51cf [&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-41966","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41966","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=41966"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41966\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=41966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=41966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=41966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}