{"id":205775,"date":"2025-06-12T11:39:00","date_gmt":"2025-06-12T03:39:00","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205775\/"},"modified":"2025-06-12T11:39:00","modified_gmt":"2025-06-12T03:39:00","slug":"golang-%e5%87%bd%e6%95%b0%e4%b8%ad-goroutine-%e4%b9%8b%e9%97%b4%e5%a6%82%e4%bd%95%e9%80%9a%e4%bf%a1%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205775\/","title":{"rendered":"Golang \u51fd\u6570\u4e2d goroutine \u4e4b\u95f4\u5982\u4f55\u901a\u4fe1\uff1f"},"content":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGOLANG \u51fd\u6570\u4e2d goroutine \u4e4b\u95f4\u5982\u4f55\u901a\u4fe1\uff1f\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01,Goroutine \u4e4b\u95f4\u901a\u4fe1\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u901a\u9053\uff1a\u7528\u4e8e\u5b89\u5168\u4f20\u8f93\u6570\u636e\u503c\uff0c\u5141\u8bb8\u4e00\u4e2a goroutine \u53d1\u9001\u6570\u636e\uff0c\u53e6\u4e00\u4e2a goroutine \u63a5\u6536\u6570\u636e\u3002\u539f\u5b50\u53d8\u91cf\uff1a\u53ef\u5e76\u53d1\u8bfb\u5199\u7684\u53d8\u91cf\uff0c\u65e0\u9700\u9501\uff0c\u9002\u5408\u7ef4\u62a4\u5171\u4eab\u72b6\u6001\u3002\u7b49\u5f85\u7ec4\uff1a\u7528\u4e8e\u7ba1\u7406\u4e00\u7ec4\u5e76\u884c goroutine\uff0c\u5141\u8bb8\u7b49\u5f85\u6240\u6709 goroutine \u5b8c\u6210\u540e\u518d\u7ee7\u7eed\u6267\u884c\u3002,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1729996468671da6b4979cd.jpg\" class=\"aligncenter\">,<br \/>\n<strong>Go \u51fd\u6570\u4e2d goroutine \u4e4b\u95f4\u7684\u901a\u4fe1<\/strong>,Go \u4e2d\u7684 goroutine \u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u5141\u8bb8\u60a8\u5e76\u53d1\u6267\u884c\u4ee3\u7801\u3002goroutine \u4e4b\u95f4\u9700\u8981\u901a\u4fe1\u4ee5\u4ea4\u6362\u6570\u636e\u6216\u534f\u8c03\u4efb\u52a1\u3002Go \u63d0\u4f9b\u4e86\u591a\u79cd\u673a\u5236\u6765\u5b9e\u73b0 goroutine \u4e4b\u95f4\u7684\u901a\u4fe1\u3002,<br \/>\n<strong>\u901a\u9053<\/strong>,\u901a\u9053\u662f\u4e00\u79cd\u5728 goroutine \u4e4b\u95f4\u5b89\u5168\u4f20\u8f93\u6570\u636e\u7684\u503c\u7c7b\u578b\u3002\u5b83\u4eec\u5141\u8bb8\u4e00\u4e2a goroutine \u5c06\u6570\u636e\u53d1\u9001\u5230\u901a\u9053\uff0c\u800c\u53e6\u4e00\u4e2a goroutine \u53ef\u4ee5\u4ece\u901a\u9053\u63a5\u6536\u6570\u636e\u3002,<br \/>\n<strong>\u539f\u5b50\u53d8\u91cf<\/strong>,\u539f\u5b50\u53d8\u91cf\u662f\u53ef\u4ee5\u5e76\u53d1\u8bfb\u5199\u800c\u65e0\u9700\u9501\u7684\u53d8\u91cf\u3002\u5b83\u4eec\u5bf9\u4e8e\u7ef4\u62a4\u5171\u4eab\u72b6\u6001\u975e\u5e38\u6709\u7528\u3002,<br \/>\n<strong>\u7b49\u5f85\u7ec4<\/strong>,\u7b49\u5f85\u7ec4\u662f\u4e00\u4e2a\u7528\u4e8e\u7ba1\u7406\u4e00\u7ec4\u5e76\u884c\u6267\u884c\u7684 goroutine \u7684\u673a\u5236\u3002\u5b83\u5141\u8bb8\u60a8\u7b49\u5f85\u6240\u6709 goroutine \u5b8c\u6210\u540e\u518d\u7ee7\u7eed\u6267\u884c\u3002,\u672c\u7bc7\u5173\u4e8e\u300aGOLANG \u51fd\u6570\u4e2d goroutine \u4e4b\u95f4\u5982\u4f55\u901a\u4fe1\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8eGOLANG\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff01,\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGOLANG \u51fd\u6570\u4e2d goroutine \u4e4b\u95f4\u5982\u4f55\u901a\u4fe1\uff1f\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01,<br \/>\n<b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>GOLANG \u51fd\u6570\u4e2d goroutine \u4e4b\u95f4\u5982\u4f55\u901a\u4fe1\uff1f<\/span><\/p>\n<h1>GOLANG \u51fd\u6570\u4e2d goroutine \u4e4b\u95f4\u5982\u4f55\u901a\u4fe1\uff1f<\/h1>\n<p><span>2024-10-27 10:34:43<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGOLANG \u51fd\u6570\u4e2d goroutine \u4e4b\u95f4\u5982\u4f55\u901a\u4fe1\uff1f\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p>Goroutine \u4e4b\u95f4\u901a\u4fe1\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u901a\u9053\uff1a\u7528\u4e8e\u5b89\u5168\u4f20\u8f93\u6570\u636e\u503c\uff0c\u5141\u8bb8\u4e00\u4e2a goroutine \u53d1\u9001\u6570\u636e\uff0c\u53e6\u4e00\u4e2a goroutine \u63a5\u6536\u6570\u636e\u3002\u539f\u5b50\u53d8\u91cf\uff1a\u53ef\u5e76\u53d1\u8bfb\u5199\u7684\u53d8\u91cf\uff0c\u65e0\u9700\u9501\uff0c\u9002\u5408\u7ef4\u62a4\u5171\u4eab\u72b6\u6001\u3002\u7b49\u5f85\u7ec4\uff1a\u7528\u4e8e\u7ba1\u7406\u4e00\u7ec4\u5e76\u884c goroutine\uff0c\u5141\u8bb8\u7b49\u5f85\u6240\u6709 goroutine \u5b8c\u6210\u540e\u518d\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1729996468671da6b4979cd.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Go \u51fd\u6570\u4e2d goroutine \u4e4b\u95f4\u7684\u901a\u4fe1<\/strong><\/p>\n<p>Go \u4e2d\u7684 goroutine \u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u5141\u8bb8\u60a8\u5e76\u53d1\u6267\u884c\u4ee3\u7801\u3002goroutine \u4e4b\u95f4\u9700\u8981\u901a\u4fe1\u4ee5\u4ea4\u6362\u6570\u636e\u6216\u534f\u8c03\u4efb\u52a1\u3002Go \u63d0\u4f9b\u4e86\u591a\u79cd\u673a\u5236\u6765\u5b9e\u73b0 goroutine \u4e4b\u95f4\u7684\u901a\u4fe1\u3002<\/p>\n<p><strong>\u901a\u9053<\/strong><\/p>\n<p>\u901a\u9053\u662f\u4e00\u79cd\u5728 goroutine \u4e4b\u95f4\u5b89\u5168\u4f20\u8f93\u6570\u636e\u7684\u503c\u7c7b\u578b\u3002\u5b83\u4eec\u5141\u8bb8\u4e00\u4e2a goroutine \u5c06\u6570\u636e\u53d1\u9001\u5230\u901a\u9053\uff0c\u800c\u53e6\u4e00\u4e2a goroutine \u53ef\u4ee5\u4ece\u901a\u9053\u63a5\u6536\u6570\u636e\u3002<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"time\"\n)\n\nfunc main() {\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u901a\u9053\n    ch := make(chan int)\n\n    \/\/ \u542f\u52a8\u4e00\u4e2a goroutine \u53d1\u9001\u6570\u636e\u5230\u901a\u9053\n    go func() {\n        ch &lt;- 42 \/\/ \u53d1\u9001\u6570\u636e\u5230\u901a\u9053\n    }()\n\n    \/\/ \u542f\u52a8\u4e00\u4e2a goroutine \u4ece\u901a\u9053\u63a5\u6536\u6570\u636e\n    go func() {\n        value := &lt;-ch \/\/ \u4ece\u901a\u9053\u63a5\u6536\u6570\u636e\n        fmt.Println(value)\n    }()\n\n    time.Sleep(500 * time.Millisecond) \/\/ \u7b49\u5f85 goroutine \u8fd0\u884c\n}<\/pre>\n<p><strong>\u539f\u5b50\u53d8\u91cf<\/strong><\/p>\n<p>\u539f\u5b50\u53d8\u91cf\u662f\u53ef\u4ee5\u5e76\u53d1\u8bfb\u5199\u800c\u65e0\u9700\u9501\u7684\u53d8\u91cf\u3002\u5b83\u4eec\u5bf9\u4e8e\u7ef4\u62a4\u5171\u4eab\u72b6\u6001\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"runtime\"\n    \"sync\/atomic\"\n)\n\nfunc main() {\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u539f\u5b50\u53d8\u91cf\n    var counter int64\n\n    \/\/ \u542f\u52a8\u591a\u4e2a goroutine \u5e76\u53d1\u589e\u52a0\u8ba1\u6570\u5668\n    for i := 0; i &lt; runtime.NumCPU(); i++ {\n        go func() {\n            for j := 0; j &lt; 1000; j++ {\n                atomic.AddInt64(&amp;counter, 1) \/\/ \u5e76\u53d1\u589e\u52a0\u8ba1\u6570\u5668\n            }\n        }()\n    }\n\n    time.Sleep(500 * time.Millisecond) \/\/ \u7b49\u5f85 goroutine \u8fd0\u884c\n\n    fmt.Println(counter) \/\/ \u6253\u5370\u6700\u7ec8\u8ba1\u6570\n}<\/pre>\n<p><strong>\u7b49\u5f85\u7ec4<\/strong><\/p>\n<p>\u7b49\u5f85\u7ec4\u662f\u4e00\u4e2a\u7528\u4e8e\u7ba1\u7406\u4e00\u7ec4\u5e76\u884c\u6267\u884c\u7684 goroutine \u7684\u673a\u5236\u3002\u5b83\u5141\u8bb8\u60a8\u7b49\u5f85\u6240\u6709 goroutine \u5b8c\u6210\u540e\u518d\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\nfunc main() {\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u7b49\u5f85\u7ec4\n    var wg sync.WaitGroup\n\n    \/\/ \u542f\u52a8\u591a\u4e2a goroutine\n    for i := 0; i &lt; 10; i++ {\n        wg.Add(1) \/\/ \u589e\u52a0\u7b49\u5f85\u7ec4\u8ba1\u6570\u5668\n\n        go func(i int) {\n            defer wg.Done() \/\/ \u51cf\u5c11\u7b49\u5f85\u7ec4\u8ba1\u6570\u5668\n            fmt.Println(i)\n        }(i)\n    }\n\n    \/\/ \u7b49\u5f85\u6240\u6709 goroutine \u5b8c\u6210\n    wg.Wait()\n\n    fmt.Println(\"\u6240\u6709 goroutine \u5df2\u5b8c\u6210\")\n}<\/pre>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aGOLANG \u51fd\u6570\u4e2d goroutine \u4e4b\u95f4\u5982\u4f55\u901a\u4fe1\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8eGOLANG\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5982\u4f55\u5728Windows\u4e0a\u4fee\u590d\u6301\u4e45\u7684Enter Network Credentials\u6d88\u606f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   PHP \u51fd\u6570\u547d\u540d\u89c4\u8303\u89e3\u8bfb\uff1a\u547d\u540d\u7ea6\u5b9a\u5982\u4f55\u968f\u7740\u7248\u672c\u6f14\u8fdb\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0 &#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-205775","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205775","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=205775"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205775\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}