{"id":41213,"date":"2024-12-01T13:09:07","date_gmt":"2024-12-01T05:09:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/41213\/"},"modified":"2024-12-01T13:09:07","modified_gmt":"2024-12-01T05:09:07","slug":"goroutines-%e5%9c%a8%e5%8f%a6%e4%b8%80%e4%b8%aa%e5%87%bd%e6%95%b0%e5%86%85%e8%bf%90%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/41213\/","title":{"rendered":"Goroutines \u5728\u53e6\u4e00\u4e2a\u51fd\u6570\u5185\u8fd0\u884c"},"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>Goroutines \u5728\u53e6\u4e00\u4e2a\u51fd\u6570\u5185\u8fd0\u884c<\/span><\/p>\n<h1>Goroutines \u5728\u53e6\u4e00\u4e2a\u51fd\u6570\u5185\u8fd0\u884c<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 15:18:30<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGoroutines \u5728\u53e6\u4e00\u4e2a\u51fd\u6570\u5185\u8fd0\u884c\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u8fd9\u53ef\u80fd\u662f\u4e00\u4e2a\u611a\u8822\u7684\u95ee\u9898\uff0c\u4f46\u6211\u53ea\u662f\u60f3\u77e5\u9053\uff0c<\/p>\n<p><strong>\u4ee3\u7801\uff1a<\/strong> <\/p>\n<pre>func sumNumber(value int){\n    array1 := []int{3, 6, 7)\n\n    for _, v:= range array1{\n        go anotherFunction(v) \/\/ goroutine function is run three times \n    }\n}<\/pre>\n<p>\u6211\u7684\u95ee\u9898\u662f\uff0cfunc sumnumber \u8fd8\u662f goroutine anotherfunction \u54ea\u4e2a\u4f1a\u5148\u5b8c\u6210\uff1f <\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8fd9\u5b8c\u5168\u53d6\u51b3\u4e8e\u5f88\u591a\u56e0\u7d20\uff0c\u5c24\u5176\u662f\u201canotherFunction\u201d\u7684\u4f5c\u7528\u3002\u7136\u800c sumNumber \u51fd\u6570\u7684\u751f\u547d\u5468\u671f\u80af\u5b9a\u4f1a\u975e\u5e38\u77ed\u6682\uff0c\u56e0\u4e3a\u5b83\u53ea\u4f1a\u542f\u52a8\u4e09\u4e2a go \u4f8b\u7a0b\u5e76\u9000\u51fa\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; Goroutines \u5728\u53e6\u4e00\u4e2a\u51fd\u6570\u5185\u8fd0\u884c Goroutines \u5728\u53e6\u4e00\u4e2a\u51fd\u6570\u5185\u8fd0\u884c \u6765\u6e90\uff1astackoverflow 2024-04-19 15:18:30 0\u6d4f\u89c8 \u6536\u85cf \u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\u300aGoroutines \u5728\u53e6\u4e00\u4e2a\u51fd\u6570\u5185\u8fd0\u884c\u300b\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01 \u95ee\u9898\u5185\u5bb9 \u8fd9\u53ef\u80fd\u662f\u4e00\u4e2a\u611a\u8822\u7684\u95ee\u9898\uff0c\u4f46\u6211\u53ea\u662f\u60f3\u77e5\u9053\uff0c \u4ee3\u7801\uff1a func sumNumber(value int){ array1 := []int{3, 6, 7) for _, v:= range array1{ go anotherFunction(v) \/\/ goroutine function is run three times } } \u6211\u7684\u95ee\u9898\u662f\uff0cfunc sumnumber \u8fd8\u662f goroutine anotherfunction \u54ea\u4e2a\u4f1a\u5148\u5b8c\u6210\uff1f \u89e3\u51b3\u65b9\u6848 \u8fd9\u5b8c\u5168\u53d6\u51b3\u4e8e\u5f88\u591a\u56e0\u7d20\uff0c\u5c24\u5176\u662f\u201canotherFunction\u201d\u7684\u4f5c\u7528\u3002\u7136\u800c sumNumber \u51fd\u6570\u7684\u751f\u547d\u5468\u671f\u80af\u5b9a\u4f1a\u975e\u5e38\u77ed\u6682\uff0c\u56e0\u4e3a\u5b83\u53ea\u4f1a\u542f\u52a8\u4e09\u4e2a go \u4f8b\u7a0b\u5e76\u9000\u51fa\u3002 [&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-41213","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41213","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=41213"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41213\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=41213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=41213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=41213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}