{"id":41641,"date":"2024-12-01T13:24:15","date_gmt":"2024-12-01T05:24:15","guid":{"rendered":"https:\/\/fwq.ai\/blog\/41641\/"},"modified":"2024-12-01T13:24:15","modified_gmt":"2024-12-01T05:24:15","slug":"%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96-goroutine-%e7%9a%84%e8%bf%90%e8%a1%8c%e7%bb%93%e6%9e%9c%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/41641\/","title":{"rendered":"\u5982\u4f55\u83b7\u53d6 goroutine \u7684\u8fd0\u884c\u7ed3\u679c\uff1f"},"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>\u5982\u4f55\u83b7\u53d6 goroutine \u7684\u8fd0\u884c\u7ed3\u679c\uff1f<\/span><\/p>\n<h1>\u5982\u4f55\u83b7\u53d6 goroutine \u7684\u8fd0\u884c\u7ed3\u679c\uff1f<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 22:00:37<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300a\u5982\u4f55\u83b7\u53d6 goroutine \u7684\u8fd0\u884c\u7ed3\u679c\uff1f\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5728\u5176\u4ed6\u8bed\u8a00\u4e2d\uff0c\u6211\u53ef\u4ee5\u540c\u65f6\u8fd0\u884c\u591a\u4e2a\u4efb\u52a1\u5e76\u5728\u76f8\u5e94\u7684\u53d8\u91cf\u4e2d\u83b7\u53d6\u6bcf\u4e2a\u4efb\u52a1\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u4f8b\u5982\u5728 js \u4e2d\uff1a<\/p>\n<pre>getapi1()\n .then( res =&gt; console.debug(\"result 1: \", res) )\n\ngetapi2()\n .then( res =&gt; console.debug(\"result 2: \", res) )\n\ngetapi3()\n .then( res =&gt; console.debug(\"result 3: \", res) )<\/pre>\n<p>\u800c\u4e14\u6211\u786e\u5207\u5730\u77e5\u9053\u54ea\u4e2a\u51fd\u6570\u7684\u6267\u884c\u7ed3\u679c\u5728\u54ea\u4e2a\u53d8\u91cf\u4e2d\u3002<\/p>\n<p>python asyncio \u4e2d\u4e5f\u662f\u5982\u6b64\uff1a<\/p>\n<pre>task1 = asyncio.create_task(getapi1)\ntask2 = asyncio.create_task(getapi2)\nresult1 = await task1\nresult2 = await task2<\/pre>\n<p>\u6211\u662f go \u8bed\u8a00\u7684\u65b0\u624b\u3002\u6240\u6709\u6307\u5357\u90fd\u8bf4\u8981\u4f7f\u7528\u5e26\u6709 goroutine \u7684\u901a\u9053\u3002<\/p>\n<p>\u4f46\u662f\u6211\u4e0d\u660e\u767d\uff0c\u5f53\u6211\u4ece\u9891\u9053\u4e2d\u8bfb\u53d6\u5185\u5bb9\u65f6\uff0c\u5982\u4f55\u786e\u5b9a\u54ea\u4e2a\u6d88\u606f\u4e0e\u54ea\u4e2a\u7ed3\u679c\u5339\u914d\uff1f<\/p>\n<pre>resultsChan := make(chan map)\n\ngo getApi1(resultsChan)\ngo getApi2(resultsChan)\ngo getApi3(resultsChan)\n\nfor {\n result, ok := &lt;- resultsChan\n\n if ok == false {\n  break\n \n } else {\n\n  \/\/ HERE\n  fmt.Println(result)  \/\/ How to understand which message the result of what API request?\n \n }\n\n}<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u5982\u679c\u540c\u4e00\u4e2a\u901a\u9053\u8981\u5c06\u6240\u6709 <code>getapin<\/code> \u51fd\u6570\u7684\u7ed3\u679c\u4f20\u8fbe\u7ed9 <code>main<\/code>\uff0c\u5e76\u4e14\u60a8\u5e0c\u671b\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u786e\u5b9a\u6bcf\u4e2a\u7ed3\u679c\u7684\u6765\u6e90\uff0c\u5219\u53ea\u9700\u5411\u901a\u9053\u5143\u7d20\u7c7b\u578b\u6dfb\u52a0\u4e13\u7528\u5b57\u6bb5\u5373\u53ef\u3002\u4e0b\u9762\uff0c\u6211\u58f0\u660e\u4e86\u4e00\u4e2a\u540d\u4e3a <code>result<\/code> \u7684\u81ea\u5b9a\u4e49\u7ed3\u6784\u7c7b\u578b\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u540d\u4e3a <code>orig<\/code> \u7684\u5b57\u6bb5\uff0c\u6b63\u662f\u51fa\u4e8e\u6b64\u76ee\u7684\u3002<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\ntype origin int\n\nconst (\n    unknown origin = iota\n    api1\n    api2\n    api3\n)\n\ntype result struct {\n    orig origin\n    data string\n}\n\nfunc getapi1(c chan result) {\n    res := result{\n        orig: api1,\n        data: \"some value\",\n    }\n    c &lt;- res\n}\n\nfunc getapi2(c chan result) {\n    res := result{\n        orig: api2,\n        data: \"some value\",\n    }\n    c &lt;- res\n}\n\nfunc getapi3(c chan result) {\n    res := result{\n        orig: api3,\n        data: \"some value\",\n    }\n    c &lt;- res\n}\n\nfunc main() {\n    results := make(chan result, 3)\n    var wg sync.waitgroup\n    wg.add(3)\n    go func() {\n        defer wg.done()\n        getapi1(results)\n    }()\n    go func() {\n        defer wg.done()\n        getapi2(results)\n    }()\n    go func() {\n        defer wg.done()\n        getapi3(results)\n    }()\n    go func() {\n        wg.wait()\n        close(results)\n    }()\n    for res := range results {\n        fmt.printf(\"%#v\\n\", res)\n    }\n}<\/pre>\n<p>()<\/p>\n<p>\u53ef\u80fd\u7684\u8f93\u51fa\uff08\u7ed3\u679c\u7684\u987a\u5e8f\u4e0d\u786e\u5b9a\uff09\uff1a<\/p>\n<p> 8834735538\u200b\u200b17 <\/p>\n<p>\u65e0\u8bba\u5982\u4f55\uff0c\u6211\u4e0d\u4f1a\u5173\u6ce8\uff1b\u4f60\u7684\u95ee\u9898\u6839\u672c\u4e0d\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u53cd\u601d\u7528\u4f8b\u3002 \uff0c<\/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\u5982\u4f55\u83b7\u53d6 goroutine \u7684\u8fd0\u884c\u7ed3\u679c\uff1f\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; \u5982\u4f55\u83b7\u53d6 goroutine \u7684\u8fd0\u884c\u7ed3\u679c\uff1f \u5982\u4f55\u83b7\u53d6 goroutine \u7684\u8fd0\u884c\u7ed3\u679c\uff1f \u6765\u6e90\uff1astackoverflow 2024-04-20 22:00:37 0\u6d4f\u89c8 \u6536\u85cf IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300a\u5982\u4f55\u83b7\u53d6 goroutine \u7684\u8fd0\u884c\u7ed3\u679c\uff1f\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01 \u95ee\u9898\u5185\u5bb9 \u5728\u5176\u4ed6\u8bed\u8a00\u4e2d\uff0c\u6211\u53ef\u4ee5\u540c\u65f6\u8fd0\u884c\u591a\u4e2a\u4efb\u52a1\u5e76\u5728\u76f8\u5e94\u7684\u53d8\u91cf\u4e2d\u83b7\u53d6\u6bcf\u4e2a\u4efb\u52a1\u7684\u7ed3\u679c\u3002 \u4f8b\u5982\u5728 js \u4e2d\uff1a getapi1() .then( res =&gt; console.debug(&#8220;result 1: &#8220;, res) ) getapi2() .then( res =&gt; console.debug(&#8220;result 2: &#8220;, res) ) getapi3() .then( res =&gt; console.debug(&#8220;result 3: &#8220;, res) ) \u800c\u4e14\u6211\u786e\u5207\u5730\u77e5\u9053\u54ea\u4e2a\u51fd\u6570\u7684\u6267\u884c\u7ed3\u679c\u5728\u54ea\u4e2a\u53d8\u91cf\u4e2d\u3002 python asyncio \u4e2d\u4e5f\u662f\u5982\u6b64\uff1a [&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-41641","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41641","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=41641"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41641\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=41641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=41641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=41641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}