{"id":42296,"date":"2024-12-01T09:33:43","date_gmt":"2024-12-01T01:33:43","guid":{"rendered":"https:\/\/fwq.ai\/blog\/42296\/"},"modified":"2024-12-01T09:33:43","modified_gmt":"2024-12-01T01:33:43","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88%e5%9c%a8%e6%88%91%e7%9a%84%e4%bb%a3%e7%a0%81%e4%b8%ad-goroutine-%e4%bc%bc%e4%b9%8e%e8%87%aa%e5%8a%a8%e5%86%bb%e7%bb%93%e5%be%aa%e7%8e%af%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/42296\/","title":{"rendered":"\u4e3a\u4ec0\u4e48\u5728\u6211\u7684\u4ee3\u7801\u4e2d goroutine \u4f3c\u4e4e\u81ea\u52a8\u51bb\u7ed3\u5faa\u73af\u53d8\u91cf"},"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>\u4e3a\u4ec0\u4e48\u5728\u6211\u7684\u4ee3\u7801\u4e2d goroutine \u4f3c\u4e4e\u81ea\u52a8\u51bb\u7ed3\u5faa\u73af\u53d8\u91cf<\/span><\/p>\n<h1>\u4e3a\u4ec0\u4e48\u5728\u6211\u7684\u4ee3\u7801\u4e2d goroutine \u4f3c\u4e4e\u81ea\u52a8\u51bb\u7ed3\u5faa\u73af\u53d8\u91cf<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 13:30:24<\/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: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4e3a\u4ec0\u4e48\u5728\u6211\u7684\u4ee3\u7801\u4e2d goroutine \u4f3c\u4e4e\u81ea\u52a8\u51bb\u7ed3\u5faa\u73af\u53d8\u91cf\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\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\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<pre>import (\n    \"fmt\"\n)\n\n\nfunc Print(i int) {\n    fmt.Println(i)\n}\n\nfunc PrintR(i *int) {\n    fmt.Println(*i)\n}\n\n\nfunc main() {\n    for i := 0; i &lt; 10; i++ {\n        go Print(i)   \/\/ Get: 1 9 2 3 4 5 7 6 8 0\n\n        \/\/go func() {\n        \/\/  Print(i)\n        \/\/}()  \/\/ Get: 10 10 7 10 4 10 7 10 10 10 10\n\n        \/\/go PrintR(&amp;i)  \/\/ Get: 7 10 10 7 10 7 10 10 10 10\n    }\n    select {\n\n    }\n}\n<\/pre>\n<p>\u7b2c\u4e8c\u4e2a\u3001\u7b2c\u4e09\u4e2a\u793a\u4f8b\u5f97\u5230\u4e86\u9884\u671f\u7684\u8f93\u51fa\uff0c\u4f46\u6211\u8ba4\u4e3a\u7b2c\u4e00\u79cd\u60c5\u51b5\u7684\u8f93\u51fa <code>go print(i)<\/code> \u5e94\u8be5\u4e0e\u7b2c\u4e8c\u4e2a\u7c7b\u4f3c\uff0c\u56e0\u4e3a\u5b83\u4eec\u90fd\u6ca1\u6709\u4f7f\u7528\u65b0\u53d8\u91cf\u6765\u51bb\u7ed3\u5f53\u524d\u503c\u6211\u3002\u9020\u6210\u8fd9\u79cd\u5dee\u5f02\u7684\u539f\u56e0\u662f\u4ec0\u4e48\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>Go \u6309\u503c\u4f20\u9012\u53c2\u6570\u3002\u60a8\u7684\u7b2c\u4e00\u4e2a\u793a\u4f8b\u5b9e\u9645\u4e0a\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u53d8\u91cf\uff0c\u5e76\u5728\u542f\u52a8\u65b0 goroutine \u4e4b\u524d\u5c06\u5f53\u524d\u7d22\u5f15\u590d\u5236\u5230\u5176\u4e2d\uff0c\u8fd9\u4e0e\u60a8\u7684\u7b2c\u4e09\u4e2a\u793a\u4f8b\u76f8\u540c\u3002<\/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\u4e3a\u4ec0\u4e48\u5728\u6211\u7684\u4ee3\u7801\u4e2d goroutine \u4f3c\u4e4e\u81ea\u52a8\u51bb\u7ed3\u5faa\u73af\u53d8\u91cf\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; \u4e3a\u4ec0\u4e48\u5728\u6211\u7684\u4ee3\u7801\u4e2d goroutine \u4f3c\u4e4e\u81ea\u52a8\u51bb\u7ed3\u5faa\u73af\u53d8\u91cf \u4e3a\u4ec0\u4e48\u5728\u6211\u7684\u4ee3\u7801\u4e2d goroutine \u4f3c\u4e4e\u81ea\u52a8\u51bb\u7ed3\u5faa\u73af\u53d8\u91cf \u6765\u6e90\uff1astackoverflow 2024-04-23 13:30:24 0\u6d4f\u89c8 \u6536\u85cf Golang\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u300a\u4e3a\u4ec0\u4e48\u5728\u6211\u7684\u4ee3\u7801\u4e2d goroutine \u4f3c\u4e4e\u81ea\u52a8\u51bb\u7ed3\u5faa\u73af\u53d8\u91cf\u300b\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01 \u95ee\u9898\u5185\u5bb9 import ( &#8220;fmt&#8221; ) func Print(i int) { fmt.Println(i) } func PrintR(i *int) { fmt.Println(*i) } func main() { for i := 0; i &lt; 10; i++ { go Print(i) \/\/ Get: 1 9 2 [&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-42296","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42296","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=42296"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42296\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=42296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=42296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=42296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}