{"id":207192,"date":"2025-07-08T09:44:22","date_gmt":"2025-07-08T01:44:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207192\/"},"modified":"2025-07-08T09:44:22","modified_gmt":"2025-07-08T01:44:22","slug":"%e4%bb%8e-golang-%e4%b8%ad%e7%9a%84%e9%80%9a%e9%81%93%e5%93%8d%e5%ba%94%e5%a1%ab%e5%85%85%e5%9c%b0%e5%9b%be%e5%80%bc","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207192\/","title":{"rendered":"\u4ece golang \u4e2d\u7684\u901a\u9053\u54cd\u5e94\u586b\u5145\u5730\u56fe\u503c"},"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 golang \u4e2d\u7684\u901a\u9053\u54cd\u5e94\u586b\u5145\u5730\u56fe\u503c<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 18:36:41<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>Golang\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300a\u4ece golang \u4e2d\u7684\u901a\u9053\u54cd\u5e94\u586b\u5145\u5730\u56fe\u503c\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u6839\u636e\u5404\u79cd goroutine \u7684\u8f93\u51fa\u6765\u586b\u5145\u5730\u56fe\u3002\u4e3a\u6b64\uff0c\u6211\u521b\u5efa\u4e86\u4e00\u4e2a\u7c7b\u578b\u4e3a (map[key][]int) \u7684\u901a\u9053<\/p>\n<pre>done := make(chan map[int][]int)<\/pre>\n<p>\u5e76\u5c06\u5176\u8fde\u540c\u952e\u503c\u4e00\u8d77\u4f20\u9012\u7ed9workers goroutine\uff0c\u5728\u672c\u4f8b\u4e2d\u952e\u503c\u662fint\u3002 \u5bf9\u4e8e\u6211\uff1a= 0\uff1b\u6211 &lt; 10;\u6211++ { \u53bb\u5de5\u4f5c\uff08\u6211\uff0c\u5b8c\u6210\uff09 } \u6211\u60f3\u5728\u8bfb\u53d6\u5bc6\u94a5\u65f6\u586b\u5145\u6211\u7684\u5730\u56fe\u3002\u76ee\u524d\u6211\u6b63\u5728\u505a\u5982\u4e0b<\/p>\n<pre>for i := 0; i &lt; 10; i++ {\n    m := &lt;-done\n    fmt.Println(m)\n    for k,v := range m {\n        retmap[k] = v\n    }\n}\nfmt.Println(retmap)<\/pre>\n<p>\u6211\u89c9\u5f97\u6211\u505a\u5f97\u4e0d\u5bf9\u3002\u6709\u6ca1\u6709\u66f4\u597d\u7684\u65b9\u6cd5\u4f7f\u7528\u6e20\u9053\u6765\u505a\u5230\u8fd9\u4e00\u70b9\uff1f\u4efb\u4f55\u5efa\u8bae\u5c06\u4e0d\u80dc\u611f\u6fc0\uff1f<\/p>\n<p>\u6f14\u793a\uff1ahttps:\/\/play.golang.org\/p\/sv4qk4heljx<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u5de5\u4f5c\u4eba\u5458\u4f7f\u7528\u7279\u5b9a\u901a\u9053\uff0c\u800c\u4e0d\u662f\u5728\u5de5\u4f5c\u4eba\u5458\u7684\u7ed3\u679c\u5bf9\u8c61\u4e2d\u7f16\u7801\u8be5\u4fe1\u606f\u3002\u50cf\u8fd9\u6837\u7684\u4e1c\u897f\uff1a<\/p>\n<pre>func worker(done chan []int) {\n    fmt.Print(\"working...\")\n    rnd := rand.Intn(10)\n    fmt.Println(\"Sleeping for \", rnd, \"seconds\")\n    for i := 0; i &lt; rnd; i++ {\n        time.Sleep(time.Second)\n    }\n    fmt.Println(\"done\")\n\n    \/\/ Send a value to notify that we're done.\n    done &lt;- makeRange(0, rnd)\n}\n\nfunc main() {\n    channels := make([]chan []int, 10, 10)\n    for i := 0; i &lt; 10; i++ {\n        channels[i] = make(chan []int)\n        go worker(channels[i])\n    }\n\n    retmap := make(map[int][]int)\n    for i := 0; i &lt; 10; i++ {\n        retmap[i] = &lt;-channels[i]\n    }\n    fmt.Println(retmap)\n}<\/pre>\n<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u4ece golang \u4e2d\u7684\u901a\u9053\u54cd\u5e94\u586b\u5145\u5730\u56fe\u503c\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\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-207192","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207192","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=207192"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207192\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}