{"id":6856,"date":"2022-02-09T09:03:54","date_gmt":"2022-02-09T01:03:54","guid":{"rendered":"https:\/\/blog.ywdevops.cn\/?p=6856"},"modified":"2025-08-27T10:58:54","modified_gmt":"2025-08-27T02:58:54","slug":"go-12","status":"publish","type":"post","link":"https:\/\/blog.ywdevops.cn\/index.php\/2022\/02\/09\/go-12\/","title":{"rendered":"Go\u8bed\u8a00\u4e4b\u5e76\u53d1\u4e0e\u901a\u9053"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>\u8fdb\u7a0b\u548c\u7ebf\u7a0b\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fdb\u7a0b\u662f\u7a0b\u5e8f\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u7684\u4e00\u6b21\u6267\u884c\u8fc7\u7a0b\uff0c\u7cfb\u7edf\u8fdb\u884c\u8d44\u6e90\u5206\u914d\u548c\u8c03\u5ea6\u7684\u4e00\u4e2a\u72ec\u7acb\u5355\u4f4d<\/li>\n\n\n\n<li>\u7ebf\u7a0b\u662f\u8fdb\u7a0b\u7684\u4e00\u4e2a\u6267\u884c\u5b9e\u4f53,\u662fCPU\u8c03\u5ea6\u548c\u5206\u6d3e\u7684\u57fa\u672c\u5355\u4f4d,\u5b83\u662f\u6bd4\u8fdb\u7a0b\u66f4\u5c0f\u7684\u80fd\u72ec\u7acb\u8fd0\u884c\u7684\u57fa\u672c\u5355\u4f4d<\/li>\n\n\n\n<li>\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u521b\u5efa\u548c\u64a4\u9500\u591a\u4e2a\u7ebf\u7a0b;\u540c\u4e00\u4e2a\u8fdb\u7a0b\u4e2d\u7684\u591a\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u53ef\u4ee5\u5e76\u53d1\u6267\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e76\u53d1\u548c\u5e76\u884c\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u5728\u4e00\u4e2a\u6838\u7684cpu\u4e0a\u8fd0\u884c\uff0c\u5c31\u662f\u5e76\u53d1<\/li>\n\n\n\n<li>\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u5728\u591a\u4e2a\u6838\u7684cpu\u4e0a\u8fd0\u884c\uff0c\u5c31\u662f\u5e76\u884c\uff0c\u5e76\u53d1\u5305\u62ec\u5e76\u884c\uff0c\u5728\u5e76\u53d1\u57fa\u7840\u4e0a\u5e76\u884c<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u534f\u7a0b\u548c\u7ebf\u7a0b\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u534f\u7a0b\uff1a\u72ec\u7acb\u7684\u6808\u7a7a\u95f4\uff0c\u5171\u4eab\u5806\u7a7a\u95f4\uff0c\u8c03\u5ea6\u7531\u7528\u6237\u81ea\u5df1\u63a7\u5236\uff0c\u672c\u8d28\u4e0a\u6709\u70b9\u7c7b\u4f3c\u4e8e\u7528\u6237\u7ea7\u7ebf\u7a0b\uff0c\u8fd9\u4e9b\u7528\u6237\u7ea7\u7ebf\u7a0b\u7684\u8c03\u5ea6\u4e5f\u662f\u81ea\u5df1\u5b9e\u73b0\u7684<\/li>\n\n\n\n<li>\u7ebf\u7a0b\uff1a\u4e00\u4e2a\u7ebf\u7a0b\u4e0a\u53ef\u4ee5\u8dd1\u591a\u4e2a\u534f\u7a0b\uff0c\u534f\u7a0b\u662f\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e76\u53d1\u4e0d\u662f\u5e76\u884c\uff1a\u5e76\u53d1\u4e3b\u8981\u7531\u5207\u6362\u65f6\u95f4\u7247\u6765\u5b9e\u73b0&#8221;\u540c\u65f6&#8221;\u8fd0\u884c\uff0c\u5e76\u884c\u5219\u662f\u76f4\u63a5\u5229\u7528\u591a\u6838\u5b9e\u73b0\u591a\u7ebf\u7a0b\u7684\u8fd0\u884c\uff0cgo\u53ef\u4ee5\u8bbe\u7f6e\u4f7f\u7528\u6838\u6570\uff0c\u4ee5\u53d1\u6325\u591a\u6838\u8ba1\u7b97\u673a\u7684\u80fd\u529b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e3b\u52a8\u5173\u95edgoroutine\u7684\u4e24\u79cd\u65b9\u5f0f\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>channel<\/li>\n\n\n\n<li>context<\/li>\n<\/ul>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>\u4e00\u3001Goroutine:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728java\/c++\u4e2d\u6211\u4eec\u8981\u5b9e\u73b0\u5e76\u53d1\u7f16\u7a0b\u7684\u65f6\u5019\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u81ea\u5df1\u7ef4\u62a4\u4e00\u4e2a\u7ebf\u7a0b\u6c60\uff0c\u5e76\u4e14\u9700\u8981\u81ea\u5df1\u53bb\u5305\u88c5\u4e00\u4e2a\u53c8\u4e00\u4e2a\u7684\u4efb\u52a1\uff0c\u540c\u65f6\u9700\u8981\u81ea\u5df1\u53bb\u8c03\u5ea6\u7ebf\u7a0b\u6267\u884c\u4efb\u52a1\u5e76\u7ef4\u62a4\u4e0a\u4e0b\u6587\u5207\u6362\uff0c\u8fd9\u4e00\u5207\u901a\u5e38\u4f1a\u8017\u8d39\u7a0b\u5e8f\u5458\u5927\u91cf\u7684\u5fc3\u667a\u3002\u90a3\u4e48\u80fd\u4e0d\u80fd\u6709\u4e00\u79cd\u673a\u5236\uff0c\u7a0b\u5e8f\u5458\u53ea\u9700\u8981\u5b9a\u4e49\u5f88\u591a\u4e2a\u4efb\u52a1\uff0c\u8ba9\u7cfb\u7edf\u53bb\u5e2e\u52a9\u6211\u4eec\u628a\u8fd9\u4e9b\u4efb\u52a1\u5206\u914d\u5230CPU\u4e0a\u5b9e\u73b0\u5e76\u53d1\u6267\u884c\u5462\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u7684goroutine\u5c31\u662f\u8fd9\u6837\u4e00\u79cd\u673a\u5236\uff0cgoroutine\u7684\u6982\u5ff5\u7c7b\u4f3c\u4e8e\u7ebf\u7a0b\uff0c\u4f46 goroutine\u662f\u7531Go\u7684\u8fd0\u884c\u65f6\uff08runtime\uff09\u8c03\u5ea6\u548c\u7ba1\u7406\u7684\u3002Go\u7a0b\u5e8f\u4f1a\u667a\u80fd\u5730\u5c06 goroutine \u4e2d\u7684\u4efb\u52a1\u5408\u7406\u5730\u5206\u914d\u7ed9\u6bcf\u4e2aCPU\uff0cGo\u8bed\u8a00\u4e4b\u6240\u4ee5\u88ab\u79f0\u4e3a\u73b0\u4ee3\u5316\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5c31\u662f\u56e0\u4e3a\u5b83\u5728\u8bed\u8a00\u5c42\u9762\u5df2\u7ecf\u5185\u7f6e\u4e86\u8c03\u5ea6\u548c\u4e0a\u4e0b\u6587\u5207\u6362\u7684\u673a\u5236<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728Go\u8bed\u8a00\u7f16\u7a0b\u4e2d\u4f60\u4e0d\u9700\u8981\u53bb\u81ea\u5df1\u5199\u8fdb\u7a0b\u3001\u7ebf\u7a0b\u3001\u534f\u7a0b\uff0c\u4f60\u7684\u6280\u80fd\u5305\u91cc\u53ea\u6709\u4e00\u4e2a\u6280\u80fd\u2013goroutine\uff0c\u5f53\u4f60\u9700\u8981\u8ba9\u67d0\u4e2a\u4efb\u52a1\u5e76\u53d1\u6267\u884c\u7684\u65f6\u5019\uff0c\u4f60\u53ea\u9700\u8981\u628a\u8fd9\u4e2a\u4efb\u52a1\u5305\u88c5\u6210\u4e00\u4e2a\u51fd\u6570\uff0c\u5f00\u542f\u4e00\u4e2agoroutine\u53bb\u6267\u884c\u8fd9\u4e2a\u51fd\u6570\u5c31\u53ef\u4ee5\u4e86\uff0c\u5c31\u662f\u8fd9\u4e48\u7b80\u5355\u7c97\u66b4\uff0c<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u53ef\u4ee5\u5c06goroutine\u7406\u89e3\u4e3a\u5176\u4ed6\u53d8\u6210\u8bed\u8a00\u4e2d\u7684\u7ebf\u7a0b<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Go\u8bed\u8a00\u4e2d\u4f7f\u7528goroutine\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5728\u8c03\u7528\u51fd\u6570\u7684\u65f6\u5019\u5728\u524d\u9762\u52a0\u4e0ago\u5173\u952e\u5b57\uff0c\u5c31\u53ef\u4ee5\u4e3a\u4e00\u4e2a\u51fd\u6570\u521b\u5efa\u4e00\u4e2agoroutine<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bed\u6cd5\u683c\u5f0f\uff1ago  \u51fd\u6570\u540d(\u53c2\u6570\u5217\u8868)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u4e2agoroutine\u5fc5\u5b9a\u5bf9\u5e94\u4e00\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u521b\u5efa\u591a\u4e2agoroutine\u53bb\u6267\u884c\u76f8\u540c\u7684\u51fd\u6570<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1<strong>\u3001\u542f\u52a8\u5355\u4e2agoroutine<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4f8b\u5b50\u4e2d\uff0c\u6ca1\u6709\u4f7f\u7528goroutine\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"262\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4-3.png\" alt=\"\" class=\"wp-image-6880\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4-3.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4-3-300x111.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4-3-600x222.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"134\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/5-3.png\" alt=\"\" class=\"wp-image-6881\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/5-3.png 710w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/5-3-300x57.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/5-3-600x113.png 600w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765\u5728\u8c03\u7528hello\u51fd\u6570\u524d\u9762\u52a0\u4e0a\u5173\u952e\u5b57go\uff0c\u542f\u52a8\u4e00\u4e2agoroutine\u6267\u884c\u8fd9\u4e2a\u51fd\u6570\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"230\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6-3.png\" alt=\"\" class=\"wp-image-6882\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6-3.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6-3-300x97.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6-3-600x195.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"122\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-3.png\" alt=\"\" class=\"wp-image-6883\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-3.png 706w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-3-300x52.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-3-600x104.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u4e0a\u56fe\u53ef\u4ee5\u770b\u51fa\uff0chello\u51fd\u6570\u5185\u5bb9\u6ca1\u6709\u6253\u5370\uff0c\u56e0\u4e3a\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\uff0cGo\u7a0b\u5e8f\u5c31\u4f1a\u4e3amain()\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u9ed8\u8ba4\u7684goroutine\uff0c<strong>\u5f53main()\u51fd\u6570\u8fd4\u56de\u7684\u65f6\u5019\u8be5goroutine\u5c31\u7ed3\u675f\u4e86\uff0c\u6240\u6709\u5728main()\u51fd\u6570\u4e2d\u542f\u52a8\u7684goroutine\u4f1a\u4e00\u540c\u7ed3\u675f<\/strong><\/p>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u4f7f\u7528go \u5173\u952e\u5b57\u521b\u5efagoroutine \u65f6\uff0c \u88ab\u8c03\u7528\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4f1a\u88ab\u5ffd\u7565\uff0c\u5982\u679c\u9700\u8981\u5728goroutine\u4e2d\u8fd4\u56de\u6570\u636e\uff0c\u9700\u8981\u4f7f\u7528\u901a\u9053(channel)\u7279\u6027\uff0c\u901a\u8fc7\u901a\u9053\u5c06\u6570\u636e\u8fd4\u56de<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\u6211\u4eec\u8981\u60f3\u529e\u6cd5\u8ba9main\u51fd\u6570\u7b49\u4e00\u7b49hello\u51fd\u6570\uff0c\u6700\u7b80\u5355\u7c97\u66b4\u7684\u65b9\u5f0f\u5c31\u662ftime.Sleep\u4e86\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"313\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/8-3.png\" alt=\"\" class=\"wp-image-6886\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/8-3.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/8-3-300x133.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/8-3-600x265.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"121\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/9-3.png\" alt=\"\" class=\"wp-image-6888\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/9-3.png 707w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/9-3-300x51.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/9-3-600x103.png 600w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<p class=\"has-white-background-color has-background wp-block-paragraph\">go\u7528\u6765\u6307\u5b9a\u7761\u7720\u65f6\u95f4\u7684\u51fd\u6570\u4e3atime.Sleep\uff0c\u9ed8\u8ba4\u662f1s\uff0c\u5982\u679c\u60f3\u7761\u77205s\u949f\uff0c\u8981\u5199\u6210 <strong>time.Sleep(5 * time.Second)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9664\u4e86\u4f7f\u7528time.Sleep\u4e4b\u5916\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528WaitGroup\uff0c\u8fd8\u53ef\u4ee5 \u4f7f\u7528\u901a\u9053(channel)\u7684\u7279\u6027\uff0c\u901a\u8fc7\u901a\u9053\u628a\u6570\u636e\u4ecegoroutine \u4e2d\u4f5c\u4e3a\u8fd4\u56de\u503c\u4f20\u51fa\uff0c\u53ef\u53c2\u8003\u901a\u9053\u7ae0\u8282\u4e2d\u7684\u901a\u9053\u63a5\u6536\u6570\u636e\u7684\u591a\u79cd\u65b9\u6cd5\u4e2d\u7684\u7b2c\u4e09\u4e2d\uff0c\u63a5\u6536\u6570\u636e\u4f46\u662f\u5ffd\u7565\u6570\u636e\u7684\u4f8b\u5b50<\/p>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\uff1a\u5b9e\u9645\u751f\u4ea7\u73af\u5883\u4e2d\u4e0d\u63a8\u8350\u4f7f\u7528time.Sleep(\u65f6\u95f4\u7b49\u5f85\u5982\u679c\u65f6\u95f4\u8bbe\u7f6e\u4e0d\u5408\u7406\u4e5f\u4f1a\u51fa\u95ee\u9898)\uff0c\u63a8\u8350\u4f7f\u7528WaitGroup\u6216\u8005chaneel\u7b49\u7279\u6027<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2<strong>\u3001\u542f\u52a8\u591a\u4e2agoroutine<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528go \u5173\u952e\u5b57\uff0c \u5c06running()\u51fd\u6570\u5e76\u53d1\u6267\u884c\uff0c\u6bcf\u9694\u4e00\u79d2\u6253\u5370\u4e00\u6b21\u8ba1\u6570\u5668\uff0c\u800cmain \u7684goroutine \u5219\u7b49\u5f85\u7528\u6237\u8f93\u5165\uff0c\u4e24\u4e2a\u884c\u4e3a\u53ef\u4ee5\u540c\u65f6\u8fdb\u884c\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"423\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/11-1.png\" alt=\"\" class=\"wp-image-6897\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/11-1.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/11-1-300x179.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/11-1-600x358.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u56fe\u4e2d\u7684\u82f1\u6587\u5b57\u6bcd\u5c31\u662f\u547d\u4ee4\u884c\u8f93\u5165\u7684\u5185\u5bb9\uff0c\u548c\u5faa\u73af\u6253\u5370\u7684\u5185\u5bb9\u53ef\u4ee5\u540c\u65f6\u8fdb\u884c\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"339\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/12-1.png\" alt=\"\" class=\"wp-image-6896\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/12-1.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/12-1-300x144.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/12-1-600x287.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bf4\u660e\uff1a\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cGo \u7a0b\u5e8f\u5728\u542f\u52a8\u65f6\uff0c\u8fd0\u884c\u65f6(runtime)\u4f1a\u9ed8\u8ba4\u4e3amain()\u51fd\u6570\u521b\u5efa\u4e00\u4e2agoroutine\uff0c\u5728main()\u51fd\u6570\u7684goroutine \u4e2d\u6267\u884c\u5230go running \u8bed\u53e5\u65f6\uff0c\u5f52\u5c5e\u4e8erunning()\u51fd\u6570\u7684goroutine\u88ab\u521b\u5efa\uff0c running()\u51fd\u6570\u5f00\u59cb\u5728\u81ea\u5df1\u7684goroutine \u4e2d\u6267\u884c\uff0c\u6b64\u65f6\uff0c main() \u7ee7\u7eed\u6267\u884c\uff0c<strong>\u4e24\u4e2agoroutine\u901a\u8fc7Go \u7a0b\u5e8f\u7684\u8c03\u5ea6\u673a\u5236\u540c\u65f6\u8fd0\u4f5c<\/strong><\/p>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">goroutine\u95f4\u4f7f\u7528channel \u901a\u4fe1<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3\u3001\u4f7f\u7528\u533f\u540d\u51fd\u6570\u521b\u5efagoroutine<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">go \u5173\u952e\u5b57\u540e\u4e5f\u53ef\u4ee5\u4e3a\u533f\u540d\u51fd\u6570\u6216\u95ed\u5305\u542f\u52a8goroutine<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528\u533f\u540d\u51fd\u6570\u6216\u95ed\u5305\u521b\u5efagoroutine \u65f6\uff0c\u9664\u4e86\u5c06\u51fd\u6570\u5b9a\u4e49\u90e8\u5206\u5199\u5728go \u7684\u540e\u9762\u4e4b\u5916\uff0c\u8fd8\u9700\u8981\u52a0\u4e0a\u533f\u540d\u51fd\u6570\u7684\u8c03\u7528\u53c2\u6570\uff0c\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>go func(\u53c2\u6570\u5217\u8868){\n        \u51fd\u6570\u4f53\n}(\u8c03\u7528\u53c2\u6570\u5217\u8868)\n\n#\u5982\u679c\u533f\u540d\u51fd\u6570\u6ca1\u6709\u53c2\u6570\uff0c\u90a3\u5c31\u4e0d\u9700\u8981\u8c03\u7528\u53c2\u6570\u5217\u8868<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4f8b\u5b50\u662f\u901a\u8fc7\u533f\u540d\u51fd\u6570\u65b9\u5f0f\u521b\u5efagoroutine\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"378\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/13-2.png\" alt=\"\" class=\"wp-image-6906\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/13-2.png 707w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/13-2-300x160.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/13-2-600x321.png 600w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\u4e0e\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u7ed3\u679c\u4e00\u6837<\/p>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\u610f\uff1a\u4e0a\u56fe\u4e2d\u5982\u679c\u4e0d\u52a0\u540e\u9762\u7684\u547d\u4ee4\u884c\u8f93\u5165\u5b57\u6bb5\uff0c\u90a3\u4e48\u533f\u540d\u51fd\u6570\u7684\u4e2d\u7684\u5185\u5bb9\u4e0d\u4f1a\u6253\u5370\u51fa\u6765\uff0c\u56e0\u4e3amain()\u51fd\u6570\u7ed3\u675f\u540e\uff0c\u5185\u90e8\u6240\u6709\u7684goroutine\u90fd\u4f1a\u4e00\u8d77\u7ed3\u675f\uff0c\u52a0\u4e0a\u547d\u4ee4\u884c\u8f93\u5165\uff0c\u90a3\u4e48main()\u51fd\u6570\u4e00\u76f4\u5728\u8fd0\u884c\uff0c\u56e0\u6b64func()\u521b\u5efa\u7684goroutine\u4e5f\u662f\u4e00\u76f4\u8fd0\u884c\u7684<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u603b\u7ed3\uff1a\u53ef\u4ee5\u5c06goroutine\u7406\u89e3\u5c06\u8ba9\u4e24\u4e2a\u51fd\u6570\u540c\u65f6\u8fd0\u884c\uff0c\u5982\u4e0a\u56fe\uff0c\u4f7f\u7528go\u540e\uff0c\u5176\u5b9e\u662f\u8ba9main\u9ed8\u8ba4\u7684goroutine\u548cgo func()\u5f00\u542f\u7684goroutine\u540c\u65f6\u53ef\u4ee5\u8fd0\u884c\uff0c\u5982\u679c\u4e0d\u52a0go\uff0c\u90a3\u4e48func()\u4f1a\u4e00\u76f4\u8fd0\u884c\u4e0b\u53bb<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>\u4e8c\u3001goroutine\u8c03\u5ea6<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">GPM\u662fGo\u8bed\u8a00\u8fd0\u884c\u65f6\uff08runtime\uff09\u5c42\u9762\u7684\u5b9e\u73b0\uff0c\u662fgo\u8bed\u8a00\u81ea\u5df1\u5b9e\u73b0\u7684\u4e00\u5957\u8c03\u5ea6\u7cfb\u7edf\u3002\u533a\u522b\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u8c03\u5ea6OS\u7ebf\u7a0b<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>G\uff1a\u5f88\u597d\u7406\u89e3\uff0c\u5c31\u662f\u4e2agoroutine\u7684\uff0c\u91cc\u9762\u9664\u4e86\u5b58\u653e\u672cgoroutine\u4fe1\u606f\u5916 \u8fd8\u6709\u4e0e\u6240\u5728P\u7684\u7ed1\u5b9a\u7b49\u4fe1\u606f<\/li>\n\n\n\n<li>P\uff1a\u7ba1\u7406\u7740\u4e00\u7ec4goroutine\u961f\u5217\uff0cP\u91cc\u9762\u4f1a\u5b58\u50a8\u5f53\u524dgoroutine\u8fd0\u884c\u7684\u4e0a\u4e0b\u6587\u73af\u5883\uff08\u51fd\u6570\u6307\u9488\u3001\u5806\u6808\u5730\u5740\u53ca\u5730\u5740\u8fb9\u754c\uff09\uff0cP\u4f1a\u5bf9\u81ea\u5df1\u7ba1\u7406\u7684goroutine\u961f\u5217\u505a\u4e00\u4e9b\u8c03\u5ea6\uff08\u6bd4\u5982\u628a\u5360\u7528CPU\u65f6\u95f4\u8f83\u957f\u7684goroutine\u6682\u505c\u3001\u8fd0\u884c\u540e\u7eed\u7684goroutine\u7b49\u7b49\uff09\u5f53\u81ea\u5df1\u7684\u961f\u5217\u6d88\u8d39\u5b8c\u4e86\u5c31\u53bb\u5168\u5c40\u961f\u5217\u91cc\u53d6\uff0c\u5982\u679c\u5168\u5c40\u961f\u5217\u91cc\u4e5f\u6d88\u8d39\u5b8c\u4e86\u4f1a\u53bb\u5176\u4ed6P\u7684\u961f\u5217\u91cc\u62a2\u4efb\u52a1<\/li>\n\n\n\n<li>M\uff1a\uff08machine\uff09\u662fGo\u8fd0\u884c\u65f6\uff08runtime\uff09\u5bf9\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u7ebf\u7a0b\u7684\u865a\u62df\uff0c M\u4e0e\u5185\u6838\u7ebf\u7a0b\u4e00\u822c\u662f\u4e00\u4e00\u6620\u5c04\u7684\u5173\u7cfb\uff0c \u4e00\u4e2agoroutine\u6700\u7ec8\u662f\u8981\u653e\u5230M\u4e0a\u6267\u884c\u7684<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">P\u4e0eM\u4e00\u822c\u4e5f\u662f\u4e00\u4e00\u5bf9\u5e94\u7684\u3002\u4ed6\u4eec\u5173\u7cfb\u662f\uff1a P\u7ba1\u7406\u7740\u4e00\u7ec4G\u6302\u8f7d\u5728M\u4e0a\u8fd0\u884c\u3002\u5f53\u4e00\u4e2aG\u957f\u4e45\u963b\u585e\u5728\u4e00\u4e2aM\u4e0a\u65f6\uff0cruntime\u4f1a\u65b0\u5efa\u4e00\u4e2aM\uff0c\u963b\u585eG\u6240\u5728\u7684P\u4f1a\u628a\u5176\u4ed6\u7684G \u6302\u8f7d\u5728\u65b0\u5efa\u7684M\u4e0a\u3002\u5f53\u65e7\u7684G\u963b\u585e\u5b8c\u6210\u6216\u8005\u8ba4\u4e3a\u5176\u5df2\u7ecf\u6b7b\u6389\u65f6\u56de\u6536\u65e7\u7684M<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">P\u7684\u4e2a\u6570\u662f\u901a\u8fc7runtime.GOMAXPROCS\u8bbe\u5b9a\uff08\u6700\u5927256\uff09\uff0cGo1.5\u7248\u672c\u4e4b\u540e\u9ed8\u8ba4\u4e3a\u7269\u7406\u7ebf\u7a0b\u6570<\/p>\n\n\n\n<p class=\"has-normal-font-size wp-block-paragraph\"><strong>1\u3001runtime\u5305<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1.1 <strong>runtime.GOMAXPROCS:  \u8c03\u6574\u5e76\u53d1\u7684\u8fd0\u884c\u6027\u80fd<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8fd0\u884c\u65f6\u7684\u8c03\u5ea6\u5668\u4f7f\u7528GOMAXPROCS\u53c2\u6570\u6765\u786e\u5b9a\u9700\u8981\u4f7f\u7528\u591a\u5c11\u4e2aOS\u7ebf\u7a0b\u6765\u540c\u65f6\u6267\u884cGo\u4ee3\u7801\uff0c\u9ed8\u8ba4\u503c\u662f\u673a\u5668\u4e0a\u7684CPU\u6838\u5fc3\u6570<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u4e2d\u53ef\u4ee5\u901a\u8fc7runtime.GOMAXPROCS()\u51fd\u6570\u8bbe\u7f6e\u5f53\u524d\u7a0b\u5e8f\u5e76\u53d1\u65f6\u5360\u7528\u7684CPU\u903b\u8f91\u6838\u5fc3\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4f8b\u5b50\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e24\u4e2a\u4efb\u52a1\uff0c\u4f46\u662f\u53ea\u6709\u4e00\u4e2a\u903b\u8f91\u6838\u5fc3\uff0c\u6b64\u65f6\u5c31\u662f\u505a\u5b8c\u4e00\u4e2a\u4efb\u52a1\u518d\u505a\u53e6\u4e00\u4e2a\u4efb\u52a1\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"482\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/14-1.png\" alt=\"\" class=\"wp-image-6911\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/14-1.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/14-1-300x204.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/14-1-600x408.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"382\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/16-1.png\" alt=\"\" class=\"wp-image-6915\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/16-1.png 707w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/16-1-300x162.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/16-1-600x324.png 600w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c06\u903b\u8f91\u6838\u5fc3\u4fee\u6539\u4e3a2\u6216\u8005\u66f4\u591a\uff0c\u6b64\u65f6\u4e24\u4e2a\u4efb\u52a1\u5e76\u884c\u6267\u884c\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"480\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/17-1.png\" alt=\"\" class=\"wp-image-6917\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/17-1.png 710w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/17-1-300x203.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/17-1-600x406.png 600w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"376\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/18-1.png\" alt=\"\" class=\"wp-image-6919\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/18-1.png 706w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/18-1-300x160.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/18-1-600x320.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u9664\u4e86\u663e\u793a\u7684\u8bbe\u7f6ecpu\u7684\u903b\u8f91\u6838\u5fc3\uff0c\u8fd8\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7runtime.NumCPU()\u67e5\u8be2cpu\u6570\u91cf\uff0c\u5e76\u4f7f\u7528e.GOMAXPROCS()\u8bbe\u7f6e\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"461\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/19-1.png\" alt=\"\" class=\"wp-image-6922\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/19-1.png 707w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/19-1-300x196.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/19-1-600x391.png 600w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"371\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/20-1.png\" alt=\"\" class=\"wp-image-6924\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/20-1.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/20-1-300x157.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/20-1-600x314.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 runtime.NumCPU() \u81ea\u52a8\u67e5\u8be2cpu\u6570\u91cf\uff0c\u6700\u5927\u6548\u7387\u7684\u5229\u7528cpu<\/p>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">GO \u5728GOMAXPROCS \u6570\u91cf\u4e0e\u4efb\u52a1\u6570\u91cf\u76f8\u7b49\u65f6\uff0c\u53ef\u4ee5\u505a\u5230\u5e76\u884c\u6267\u884c\uff0c\u4f46\u4e00\u822c\u60c5\u51b5\u4e0b\u90fd\u662f\u5e76\u53d1\u6267\u884c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1.2 runtime.Gosched() :  \u8ba9\u51facpu\u65f6\u95f4\u7247<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u51fd\u6570\u7684\u4f5c\u7528\u662f\u8ba9\u5f53\u524dgoroutine\u8ba9\u51faCPU\uff0c\u597d\u8ba9\u5176\u5b83\u7684goroutine\u83b7\u5f97\u6267\u884c\u7684\u673a\u4f1a\u3002\u540c\u65f6\uff0c\u5f53\u524d\u7684goroutine\u4e5f\u4f1a\u5728\u672a\u6765\u7684\u67d0\u4e2a\u65f6\u95f4\u70b9\u7ee7\u7eed\u8fd0\u884c\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"354\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/21-1.png\" alt=\"\" class=\"wp-image-6927\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/21-1.png 707w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/21-1-300x150.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/21-1-600x300.png 600w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"257\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/22-1.png\" alt=\"\" class=\"wp-image-6928\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/22-1.png 707w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/22-1-300x109.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/22-1-600x218.png 600w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">1<strong>.3 runtime.Goexit()\uff1a\u9000\u51fa\u5f53\u524d\u534f\u7a0b<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u51fd\u6570\u7684\u4f5c\u7528\u662f\u7ec8\u6b62\u5f53\u524d\u8fd0\u884c\u7684goruntine\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"522\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1-4.png\" alt=\"\" class=\"wp-image-6930\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1-4.png 706w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1-4-300x222.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1-4-600x444.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"114\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2-4.png\" alt=\"\" class=\"wp-image-6932\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2-4.png 706w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2-4-300x48.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2-4-600x97.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u8fd0\u884c\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa\uff0cdefer\u8bed\u53e5\u4f1a\u88ab\u6b63\u5e38\u6267\u884c\uff0c\u56e0\u4e3a<strong>runtime.Goexit\u51fd\u6570\u5728\u7ec8\u6b62\u8c03\u7528\u5b83\u7684Goroutine\u7684\u8fd0\u884c\u4e4b\u524d\u4f1a\u5148\u6267\u884c\u8be5Groution\u4e2d\u8fd8\u6ca1\u6709\u6267\u884c\u7684defer\u8bed\u53e5<\/strong><\/p>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\u610f: goroutine\u662f\u4e0d\u80fd\u76f4\u63a5\u63a5\u6536\u51fd\u6570\u8fd4\u56de\u503c\u7684\uff0cres := go hello() \u662f\u4e0d\u6210\u7acb\u7684\uff0c\u56e0\u6b64\u5982\u679c\u8981\u63a5\u6536\u8fd4\u56de\u503c\uff0c\u9700\u8981\u4f7f\u7528\u901a\u9053<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>\u4e09\u3001\u901a\u9053(channel)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u9053\u53ef\u4ee5\u7406\u89e3\u4e3a\u5728\u591a\u4e2agoroutine\u95f4\u901a\u4fe1\u7684\u7ba1\u9053<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u5355\u7eaf\u5730\u5c06\u51fd\u6570\u5e76\u53d1\u6267\u884c\u662f\u6ca1\u6709\u610f\u4e49\u7684\u3002\u51fd\u6570\u4e0e\u51fd\u6570\u95f4\u9700\u8981\u4ea4\u6362\u6570\u636e\u624d\u80fd\u4f53\u73b0\u5e76\u53d1\u6267\u884c\u51fd\u6570\u7684\u610f\u4e49<\/mark><\/strong>\u3002\u867d\u7136\u53ef\u4ee5\u4f7f\u7528\u5171\u4eab\u5185\u5b58\u8fdb\u884c\u6570\u636e\u4ea4\u6362\uff0c\u4f46\u662f\u5171\u4eab\u5185\u5b58\u5728\u4e0d\u540c\u7684goroutine \u4e2d\u5bb9\u6613\u53d1\u751f\u7ade\u6001\u95ee\u9898\u3002\u4e3a\u4e86\u4fdd\u8bc1\u6570\u636e\u4ea4\u6362\u7684\u6b63\u786e\u6027\uff0c\u5fc5\u987b\u4f7f\u7528\u4e92\u65a5\u91cf\u5bf9\u5185\u5b58\u8fdb\u884c\u52a0\u9501\uff0c\u8fd9\u79cd\u505a\u6cd5\u4f1a\u9020\u6210\u6027\u80fd\u95ee\u9898<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u8bed\u8a00\u7684\u5e76\u53d1\u6a21\u578b\u662fCSP(Communicating Sequential Processes)\u63d0\u5021\u4f7f\u7528\u901a\u4fe1\u7684\u65b9\u6cd5\u4ee3\u66ff\u5171\u4eab\u5185\u5b58\uff0c\u8fd9\u91cc\u901a\u4fe1\u7684\u65b9\u6cd5\u5c31\u662f\u4f7f\u7528\u901a\u9053\uff08 channel\uff09\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"370\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/3-4.png\" alt=\"\" class=\"wp-image-6936\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/3-4.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/3-4-300x157.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/3-4-600x313.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1\u3001\u901a\u9053\u7684\u7279\u6027\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go \u8bed\u8a00\u4e2d\u7684\u901a\u9053\uff08 channel \uff09\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u7c7b\u578b\u3002\u5728\u4efb\u4f55\u65f6\u5019\uff0c\u540c\u65f6\u53ea\u80fd\u6709\u4e00\u4e2agoroutine\u8bbf\u95ee\u901a\u9053\u8fdb\u884c\u53d1\u9001\u548c\u83b7\u53d6\u6570\u636e\u3002goroutine \u95f4\u901a\u8fc7\u901a\u9053\u5c31\u53ef\u4ee5\u901a\u4fe1\uff0cchannel\u662f\u53ef\u4ee5\u8ba9\u4e00\u4e2agoroutine\u53d1\u9001\u7279\u5b9a\u503c\u5230\u53e6\u4e00\u4e2agoroutine\u7684\u901a\u4fe1\u673a\u5236\u3002<\/li>\n\n\n\n<li>channel\u662f\u4e00\u79cd\u961f\u5217\u4e00\u6837\u7684\u7ed3\u6784\uff0c\u4f7f\u7528\u961f\u5217\u53ef\u4ee5\u907f\u514dgoroutine\u4e89\u62a2\u6570\u636e\uff0c\u63d0\u9ad8\u6267\u884c\u6548\u7387\uff0c\u5e76\u4e14\u603b\u662f\u9075\u5faa\u5148\u5165\u5148\u51fa\uff08 First In First Out \uff09 \u7684\u89c4\u5219\uff0c\u4fdd\u8bc1\u6536\u53d1\u6570\u636e\u7684\u987a\u5e8f\u3002\u6bcf\u4e00\u4e2a\u901a\u9053\u90fd\u662f\u4e00\u4e2a\u5177\u4f53\u7c7b\u578b\u7684\u5bfc\u7ba1\uff0c\u4e5f\u5c31\u662f\u58f0\u660echannel\u7684\u65f6\u5019\u9700\u8981\u4e3a\u5176\u6307\u5b9a\u5143\u7d20\u7c7b\u578b<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2\u3001\u901a\u9053(channel) \u7c7b\u578b\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">channel\u662f\u4e00\u79cd\u7c7b\u578b\uff0c\u4e00\u79cd\u5f15\u7528\u7c7b\u578b\u3002\u58f0\u660e\u901a\u9053\u7c7b\u578b\u7684\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var \u53d8\u91cf chan  \u901a\u9053\u7c7b\u578b\n\n#\u901a\u9053\u7c7b\u578b\uff1a\u901a\u9053\u5185\u7684\u6570\u636e\u7c7b\u578b\n#\u901a\u9053\u53d8\u91cf\uff1a\u4fdd\u5b58\u901a\u9053\u7684\u53d8\u91cf<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var ch1 chan int       \/\/ \u58f0\u660e\u4e00\u4e2a\u4f20\u9012\u6574\u578b\u7684\u901a\u9053\nvar ch2 chan bool      \/\/ \u58f0\u660e\u4e00\u4e2a\u4f20\u9012\u5e03\u5c14\u578b\u7684\u901a\u9053\nvar ch3 chan &#91;]int     \/\/ \u58f0\u660e\u4e00\u4e2a\u4f20\u9012int\u5207\u7247\u7684\u901a\u9053<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">channel\u7c7b\u578b\u7684\u7a7a\u503c\u662fnil \uff0c<strong>\u58f0\u660e\u540e\u9700\u8981\u914d\u5408make \u540e\u624d\u80fd\u4f7f\u7528<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3<strong>\u3001\u521b\u5efa\u901a\u9053<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u9053\u662f\u5f15\u7528\u7c7b\u578b\uff0c \u58f0\u660e\u901a\u9053\u540e\u9700\u8981\u4f7f\u7528make \u8fdb\u884c\u521d\u59cb\u5316\u624d\u80fd\u4f7f\u7528\uff0c \u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u901a\u9053\u5b9e\u4f8b := make(chan \u6570\u636e\u7c7b\u578b)<\/strong><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6570\u636e\u7c7b\u578b\uff1a \u901a\u9053\u5185\u4f20\u8f93\u7684\u5143\u7d20\u7c7b\u578b<\/li>\n\n\n\n<li>\u901a\u9053\u5b9e\u4f8b\uff1a \u901a\u8fc7make \u521b\u5efa\u7684\u901a\u9053\u53e5\u67c4\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ch1 := make(chan int)           \/\/\u521b\u5efa\u4e00\u4e2a\u6574\u6570\u7c7b\u578b\u7684\u901a\u9053\nch2 := make(chan interface{})   \/\/\u521b\u5efa\u4e00\u4e2a\u7a7a\u63a5\u53e3\u7c7b\u578b\u7684\u901a\u9053\uff0c\u53ef\u5b58\u653e\u4efb\u610f\u683c\u5f0f\nch3 := make(chan *abcd)         \/\/\u521b\u5efa\u6307\u9488\u7c7b\u578b\u7684\u901a\u9053\uff0c\u53ef\u4ee5\u5b58\u5728\u6307\u9488\u7c7b\u578b\u7684\u503c\uff0c\u53ef\u914d\u7f6e\u7ed3\u6784\u4f53\u4f7f\u7528<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4<strong>\u3001\u4f7f\u7528\u901a\u9053\u53d1\u9001\u6570\u636e<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u9053\u6709\u53d1\u9001\uff08send\uff09\u3001\u63a5\u6536(receive\uff09\u548c\u5173\u95ed\uff08close\uff09\u4e09\u79cd\u64cd\u4f5c\uff0c\u521b\u5efa\u540e\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u901a\u9053\u8fdb\u884c\u53d1\u9001\u548c\u63a5\u6536\u64cd\u4f5c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u9053\u7684\u53d1\u9001\u4f7f\u7528\u7279\u6b8a\u7684\u64cd\u4f5c\u7b26\u201c&lt;-\u201d\uff0c\u5c06\u6570\u636e\u901a\u8fc7\u901a\u9053\u53d1\u9001\u7684\u683c\u5f0f\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u901a\u9053\u53d8\u91cf  &lt;- \u503c     \/\/\u8868\u793a\u5c06\u503c\u53d1\u9001\u5230\u901a\u9053\u4e2d<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ch := make(chan interface{})     \/\/\u521b\u5efa\u4e00\u4e2a\u7a7a\u63a5\u53e3\u7c7b\u578b\u7684\u901a\u9053,\u7a7a\u63a5\u53e3\u53ef\u63a5\u6536\u4efb\u610f\u7c7b\u578b\u7684\u503c\nch &lt;- 0                          \/\/\u5c060\u653e\u5165\u901a\u9053\u4e2d\uff0c\u4e5f\u53ef\u4ee5\u7406\u89e3\u4e3a\u5c060\u53d1\u9001\u5230\u901a\u9053\u4e2d\nch &lt;- \"hello\"                    \/\/\u5c06\u5b57\u7b26\u4e32hello\u653e\u5165\u901a\u9053\u4e2d\uff0c\u5c06\u5b57\u7b26\u4e32\u53d1\u9001\u5230\u901a\u9053\u4e2d<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>5\u3001\u4f7f\u7528\u901a\u9053\u63a5\u6536\u6570\u636e<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u9053\u63a5\u6536\u540c\u6837\u4f7f\u7528 &#8220;&lt;-&#8221; \u64cd\u4f5c\u7b26\uff0c\u901a\u9053\u63a5\u6536\u6709\u5982\u4e0b\u7279\u6027\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u901a\u9053\u7684\u6536\u53d1\u64cd\u4f5c\u5728\u4e0d\u540c\u7684\u4e24\u4e2agoroutine \u95f4\u8fdb\u884c<\/mark><\/strong>\uff0c\u7531\u4e8e\u901a\u9053\u7684\u6570\u636e\u5728\u6ca1\u6709\u63a5\u6536\u65b9\u5904\u7406\u65f6\uff0c\u6570\u636e\u53d1\u9001\u65b9\u4f1a\u6301\u7eed\u963b\u585e\uff0c\u56e0\u6b64\u901a\u9053\u7684\u63a5\u6536\u5fc5\u5b9a\u5728\u53e6\u5916\u4e00\u4e2agoroutine \u4e2d\u8fdb\u884c<\/li>\n\n\n\n<li>\u63a5\u6536\u5c06\u6301\u7eed\u963b\u585e\u76f4\u5230\u53d1\u9001\u65b9\u53d1\u9001\u6570\u636e\uff0c\u5982\u679c\u63a5\u6536\u65b9\u63a5\u6536\u65f6\uff0c \u901a\u9053\u4e2d\u6ca1\u6709\u53d1\u9001\u65b9\u53d1\u9001\u6570\u636e\uff0c \u63a5\u6536\u65b9\u4e5f\u4f1a\u53d1\u751f\u963b\u585e\uff0c\u76f4\u5230\u53d1\u9001\u65b9\u53d1\u9001\u6570\u636e\u4e3a\u6b62<\/li>\n\n\n\n<li>\u6bcf\u6b21\u63a5\u6536\u4e00\u4e2a\u5143\u7d20\uff0c\u901a\u9053\u4e00\u6b21\u53ea\u80fd\u63a5\u6536\u4e00\u4e2a\u6570\u636e\u5143\u7d20<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u901a\u9053\u7684\u6570\u636e\u63a5\u6536\u4e00\u5171\u6709\u4ee5\u4e0b4 \u79cd\u5199\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<p class=\"has-vivid-purple-color has-text-color wp-block-paragraph\">\u963b\u585e\u63a5\u6536\u6570\u636e\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u963b\u585e\u6a21\u5f0f\u63a5\u6536\u6570\u636e\u65f6\uff0c\u5c06\u63a5\u6536\u53d8\u91cf\u4f5c\u4e3a \u201c&lt;-\u201d \u64cd\u4f5c\u7b26\u7684\u5de6\u503c\uff0c\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data  := &lt;- ch                 \/\/\u4ecech\u4e2d\u63a5\u6536\u503c\u5e76\u8d4b\u503c\u7ed9\u53d8\u91cfdata<\/code><\/pre>\n\n\n\n<p class=\"has-vivid-purple-color has-text-color wp-block-paragraph\">\u975e\u963b\u585e\u63a5\u6536\u6570\u636e\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528\u975e\u963b\u585e\u65b9\u5f0f\u4ece\u901a\u9053\u63a5\u6536\u6570\u636e\u65f6\uff0c\u8bed\u53e5\u4e0d\u4f1a\u53d1\u751f\u963b\u585e\uff0c\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data , ok := &lt;- ch            \/\/\u5982\u679c\u672a\u80fd\u63a5\u6536\u5230\u6570\u636e\uff0cdata\u4e3a\u901a\u9053\u7c7b\u578b\u7684\u96f6\u503c\uff0cok\u8868\u793a\u662f\u5426\u63a5\u6536\u5230\u6570\u636e<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u975e\u963b\u585e\u7684\u901a\u9053\u63a5\u6536\u65b9\u6cd5\u53ef\u80fd\u9020\u6210\u9ad8\u7684CPU \u5360\u7528\uff0c\u56e0\u6b64\u4f7f\u7528\u975e\u5e38\u5c11\uff0c\u5982\u679c\u9700\u8981\u5b9e\u73b0\u63a5\u6536\u8d85\u65f6\u68c0\u6d4b\uff0c \u53ef\u4ee5\u914d\u5408select\u548c\u8ba1\u65f6\u5668channel \u8fdb\u884c<\/p>\n\n\n\n<p class=\"has-vivid-purple-color has-text-color wp-block-paragraph\">\u63a5\u6536\u4efb\u610f\u6570\u636e\uff0c \u5ffd\u7565\u63a5\u6536\u7684\u6570\u636e\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u963b\u585e\u63a5\u6536\u6570\u636e\u540e\uff0c \u5ffd\u7565\u4ece\u901a\u9053\u8fd4\u56de\u7684\u6570\u636e\uff0c\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;- ch<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6267\u884c\u8be5\u8bed\u53e5\u65f6\u5c06\u4f1a\u53d1\u751f\u963b\u585e\uff0c \u76f4\u5230\u63a5\u6536\u5230\u6570\u636e\uff0c \u4f46\u63a5\u6536\u5230\u7684\u6570\u636e\u4f1a\u88ab\u5ffd\u7565\uff0c\u8fd9\u4e2a\u65b9\u5f0f\u5b9e\u9645\u4e0a\u53ea\u662f\u901a\u8fc7\u901a\u9053\u5728goroutine\u95f4\u963b\u585e\u6536\u53d1\u5b9e\u73b0\u5e76\u53d1\u540c\u6b65<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u7684\u4f8b\u5b50\u8868\u793a\u5f00\u542f\u4e00\u4e2a\u5e76\u53d1\u533f\u540d\u51fd\u6570\uff0c\u4f46\u662f\u7531\u4e8emain()\u51fd\u6570\u7684\u9ed8\u8ba4goroutine\u6267\u884c\u5b8c\u5c31\u9000\u51fa\u4e86\uff0c\u56e0\u6b64go func\u7684\u7ed3\u679c\u4e5f\u4e0d\u4f1a\u6267\u884c\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"248\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4-4.png\" alt=\"\" class=\"wp-image-6963\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4-4.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4-4-300x105.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4-4-600x210.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff0c\u53ea\u6253\u5370\u4e86main()\u51fd\u6570\u4e2d\u7684\u5185\u5bb9\uff0c\u548cgoroutine\u5f00\u5934\u7684\u4f8b\u5b50\u4e00\u6837\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"69\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6-4.png\" alt=\"\" class=\"wp-image-6966\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6-4.png 706w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6-4-300x29.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6-4-600x59.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u73b0\u5728\u6211\u4eec\u901a\u8fc7\u901a\u9053\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u533f\u540d\u51fd\u6570\u7684goroutine\u5e76\u53d1\u8fd0\u884c\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"369\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-4.png\" alt=\"\" class=\"wp-image-6969\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-4.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-4-300x156.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-4-600x312.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u7528\u4e86\u901a\u9053\u540e\uff0c\u533f\u540dgoroutine\u548cmain()\u7684\u9ed8\u8ba4goroutine \u53ef\u4ee5\u5b9e\u73b0\u5e76\u53d1\u8fd0\u884c(\u4e0d\u5728\u4f7f\u7528time.Sleep\u4e5f\u53ef\u4ee5\u5566)\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"87\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/8-4.png\" alt=\"\" class=\"wp-image-6970\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/8-4.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/8-4-300x37.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/8-4-600x74.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\uff1a\u4e0a\u56fe\u4e2d\u7684go func()\u8868\u793a\u4e00\u4e2agoroutine\uff0cmain()\u4e3a\u53e6\u4e00\u4e2agoroutine\uff0c\u901a\u9053\u7684\u53d1\u9001\u548c\u63a5\u6536\u9700\u8981\u5728\u4e24\u4e2a\u4e0d\u540c\u7684goroutine\u4e2d\u8fdb\u884c<\/p>\n\n\n\n<p class=\"has-vivid-purple-color has-text-color wp-block-paragraph\">\u5faa\u73af\u63a5\u6536\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u9053\u7684\u6570\u636e\u63a5\u6536\u53ef\u4ee5\u501f\u7528for range \u8bed\u53e5\u8fdb\u884c\u591a\u4e2a\u5143\u7d20\u7684\u63a5\u6536\u64cd\u4f5c\uff0c\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for data := range ch {\n\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u9053ch \u662f\u53ef\u4ee5\u8fdb\u884c\u904d\u5386\u7684\uff0c\u904d\u5386\u7684\u7ed3\u679c\u5c31\u662f\u63a5\u6536\u5230\u7684\u6570\u636e\uff0c\u6570\u636e\u7c7b\u578b\u5c31\u662f\u901a\u9053\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5728\u53d1\u9001\u5b8c\u6570\u636e\u540e\u9700\u8981\u5173\u95ed\u901a\u9053\uff0c\u5426\u5219\u5faa\u73af\u904d\u5386\u5c06\u4f1a\u62a5\u6b7b\u9501\u9519\u8bef<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4f8b\u5b50\u8868\u793a\u901a\u8fc7\u5faa\u73af\u904d\u5386\u901a\u9053\u4e2d\u7684\u6570\u636e\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"415\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/8.png\" alt=\"\" class=\"wp-image-10486\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/8.png 710w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/8-300x175.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/8-600x351.png 600w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"161\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/9.png\" alt=\"\" class=\"wp-image-10488\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/9.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/9-300x68.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/9-600x136.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c15\u884c\uff1aclose()\u4e00\u5b9a\u8981\u653e\u5728func()\u51fd\u6570\u5185\u90e8\uff0cfor\u5faa\u73af\u5916\uff0c\u5f53for\u5faa\u73af\u6267\u884c\u5b8c\u6210\u540e\u5173\u95ed\u901a\u9053\uff0c\u5982\u679cclose\u653e\u5728func()\u4e4b\u5916\uff0c\u90a3\u4e48go func()\u548cmain\u81ea\u5e26\u7684goroutine\u540c\u65f6\u8fd0\u884c\uff0c\u53ef\u80fd\u51fa\u73b0\u8fd8\u6ca1\u5199\u6570\u636e\u901a\u9053\u5c31\u5173\u95ed\u7684\u60c5\u51b5\uff0c\u6267\u884c\u4f1a\u51fa\u73b0\u6b7b\u9501<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ce8\u610f\uff1a\u7b2c15\u884c\u5728\u5faa\u73af\u53d1\u9001\u6570\u636e\u901a\u9053\u7ed3\u675f\u540e\u9700\u8981\u5173\u95ed\u901a\u9053\uff0c\u56e0\u4e3a\u6570\u636e\u5df2\u7ecf\u90fd\u53d1\u9001\u5230\u901a\u9053\u4e2d\uff0c\u6ca1\u6709\u6570\u636e\u7ee7\u7eed\u53d1\u9001\u4e86\uff0c\u4f46\u662f\u5916\u5c42main\u7684goroutine\u8fd8\u5728\u7b49\u5f85\u904d\u5386\u901a\u9053\u4e2d\u7684\u6570\u636e\uff0c\u8fd9\u5c31\u9020\u6210\u4e86\u963b\u585e\uff0c\u6700\u540e\u62a5\u51fa\u6b7b\u9501\u7684\u9519\u8bef\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"237\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/11-2.png\" alt=\"\" class=\"wp-image-6975\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/11-2.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/11-2-300x100.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/11-2-600x201.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u7684\u4f8b\u5b50\u521b\u5efa\u7684\u90fd\u662f\u65e0\u7f13\u51b2\u901a\u9053(\u53d1\u9001\u6570\u636e\u7684\u65f6\u5019\u5fc5\u987b\u540c\u6b65\u6709\u63a5\u6536\u8005\uff0c\u5426\u5219\u5c31\u4f1a\u963b\u585e\u5bfc\u81f4\u6b7b\u9501)\uff0c\u4f7f\u7528\u65e0\u7f13\u51b2\u901a\u9053\u5f80\u91cc\u9762\u88c5\u5165\u6570\u636e\u65f6\uff0c\u88c5\u5165\u65b9\u5c06\u88ab\u963b\u585e\uff0c \u76f4\u5230\u53e6\u5916\u901a\u9053\u5728\u53e6\u5916\u4e00\u4e2agoroutine \u4e2d\u88ab\u53d6\u51fa\uff0c\u540c\u6837\uff0c\u5982\u679c\u901a\u9053\u4e2d\u6ca1\u6709\u653e\u5165\u4efb\u4f55\u6570\u636e\uff0c \u63a5\u6536\u65b9\u8bd5\u56fe\u4ece\u901a\u9053\u4e2d\u83b7\u53d6\u6570\u636e\u65f6\uff0c\u540c\u6837\u4e5f\u662f\u963b\u585e\u3002\u53d1\u9001\u548c\u63a5\u6536\u7684\u64cd\u4f5c\u662f\u540c\u6b65\u5b8c\u6210\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4f8b\u5b50\u5c06goroutine\u548cchannel\u653e\u5728\u4e00\u8d77\uff0c\u5c55\u793a\u7528\u6cd5\uff0c\u4e5f\u662f\u65e0\u7f13\u51b2\u901a\u9053\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"523\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/12-2.png\" alt=\"\" class=\"wp-image-6978\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/12-2.png 706w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/12-2-300x222.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/12-2-600x444.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c21\u884c\uff1ago hello(ch)\uff0c\u521b\u5efagoroutine\uff0c\u5c06\u901a\u9053\u53d1\u9001\u8fc7\u53bb\uff0c\u4e00\u5b9a\u8981\u653e\u5728for\u5faa\u73af\u7684\u524d\u9762\uff0c\u56e0\u4e3a\u53d1\u9001\u6570\u636e\u548c\u63a5\u6536\u6570\u636e\u662f\u540c\u6b65\u8fdb\u884c\u7684\uff0c\u5982\u679c\u653e\u5728for\u5faa\u73af\u540e\u9762\uff0cfor\u5faa\u73af\u6267\u884c\u53d1\u9001\u6570\u636e\u540e\uff0c\u6ca1\u6709\u63a5\u6536\u65b9\uff0c\u4f1a\u51fa\u73b0\u6b7b\u9501\u9519\u8bef<\/li>\n\n\n\n<li>\u7b2c25\u884c\uff1a100\u8fd9\u4e2a\u6570\u636e\u53ef\u4ee5\u81ea\u5b9a\u4e49\uff0c\u6b64\u884c\u4ee3\u7801\u7684\u529f\u80fd\u5c31\u662f\u7ed9\u63a5\u6536\u65b9\u63d0\u4f9b\u5224\u65ad\u6761\u4ef6\uff0c\u5728for\u5faa\u73af\u53d1\u9001\u5b8c\u6570\u636e\u540e\u6267\u884c<\/li>\n\n\n\n<li>\u7b2c12\u884c\uff1a\u6839\u636e\u53d1\u9001\u65b9\u53d1\u9001\u8fc7\u6765\u7684\u6570\u636e100\u8fdb\u884c\u5224\u65ad\uff0c\u5982\u679c\u7b49\u4e8e100\uff0c\u8bf4\u660e\u53d1\u9001\u65b9\u7684\u6570\u636e\u5df2\u7ecf\u53d1\u9001\u5b8c\u6bd5\uff0c\u81ea\u5df1\u4e5f\u63a5\u6536\u5b8c\u6bd5\uff0c\u53ef\u4ee5\u505c\u6b62\u63a5\u6536\uff0c\u4ee5\u514d\u9020\u6210\u963b\u585e\u6b7b\u9501<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>6\u3001\u5355\u5411\u901a\u9053&#8212;\u901a\u9053\u4e2d\u7684\u5355\u884c\u9053<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u7684\u901a\u9053\u53ef\u4ee5\u5728\u58f0\u660e\u65f6\u7ea6\u675f\u5176\u64cd\u4f5c\u65b9\u5411\uff0c\u5982\u53ea\u53d1\u9001\u6216\u662f\u53ea\u63a5\u6536\u3002\u8fd9\u79cd\u88ab\u7ea6\u675f\u65b9\u5411\u7684\u901a\u9053\u88ab\u79f0\u505a\u5355\u5411\u901a\u9053<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ea\u80fd\u53d1\u9001\u7684\u901a\u9053\u7c7b\u578b\u4e3achan &lt;- \uff0c\u53ea\u80fd\u63a5\u6536\u7684\u901a\u9053\u7c7b\u578b\u4e3a\uff1c -chan \uff0c \u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var \u901a\u9053\u5b9e\u4f8b chan&lt;-  \u5143\u7d20\u7c7b\u578b          \/\/\u53ea\u80fd\u53d1\u9001\u901a\u9053\nvar \u901a\u9053\u5b9e\u4f8b &lt;-chan  \u5143\u7d20\u7c7b\u578b          \/\/\u53ea\u80fd\u63a5\u6536\u901a\u9053<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\u4f8b\u5982\uff1a\nch := make(chan&lt;- int)    \/\/\u53ea\u80fd\u53d1\u9001\u6570\u636e\u7684\u901a\u9053\nch := make(&lt;-chan int)   \/\/\u53ea\u80fd\u63a5\u6536\u6570\u636e\u7684\u901a\u9053<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4f8b\u5b50\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u53ea\u80fd\u53d1\u9001\u7684\u901a\u9053\u7c7b\u578b\uff0c\u5982\u679c\u63a5\u6536\u5c31\u4f1a\u51fa\u73b0\u9519\u8bef\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"286\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/13-3.png\" alt=\"\" class=\"wp-image-6984\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/13-3.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/13-3-300x121.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/13-3-600x242.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u9519\u8bef\u63d0\u793a\u53ea\u80fd\u53d1\u9001\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"100\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/14-2.png\" alt=\"\" class=\"wp-image-6985\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/14-2.png 707w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/14-2-300x42.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/14-2-600x85.png 600w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4f8b\u5b50\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u53ea\u80fd\u63a5\u6536\u6570\u636e\u7684\u901a\u9053\uff0c\u5982\u679c\u5411\u5176\u5185\u90e8\u53d1\u9001\u6570\u636e\u5c31\u4f1a\u63d0\u793a\u9519\u8bef\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"269\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/15-2.png\" alt=\"\" class=\"wp-image-6988\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/15-2.png 706w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/15-2-300x114.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/15-2-600x229.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u9519\u8bef\u63d0\u793a\u53ea\u80fd\u63a5\u6536\u6570\u636e\uff0c\u4e0d\u80fd\u53d1\u9001\u6570\u636e\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"705\" height=\"94\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/16-2.png\" alt=\"\" class=\"wp-image-6989\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/16-2.png 705w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/16-2-300x40.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/16-2-600x80.png 600w\" sizes=\"auto, (max-width: 705px) 100vw, 705px\" \/><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u4e00\u4e2a\u4e0d\u80fd\u586b\u5145\u6570\u636e\uff08\u53d1\u9001\uff09\u53ea\u80fd\u8bfb\u53d6\u7684\u901a\u9053\u662f\u6beb\u65e0\u610f\u4e49\u7684<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">7<strong>\u3001\u5e26\u7f13\u51b2\u7684\u901a\u9053<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u65e0\u7f13\u51b2\u901a\u9053\u7684\u57fa\u7840\u4e0a\uff0c\u4e3a\u901a\u9053\u589e\u52a0\u4e00\u4e2a\u6709\u9650\u5927\u5c0f\u7684\u5b58\u50a8\u7a7a\u95f4\u5f62\u6210\u5e26\u7f13\u51b2\u901a\u9053\u3002\u5e26\u7f13\u51b2\u901a\u9053\u5728\u53d1\u9001\u65f6\u65e0\u9700\u7b49\u5f85\u63a5\u6536\u65b9\u63a5\u6536\u5373\u53ef\u5b8c\u6210\u53d1\u9001\u8fc7\u7a0b\uff0c \u5e76\u4e14\u4e0d\u4f1a\u53d1\u751f\u963b\u585e\uff0c\u53ea\u6709\u5f53\u5b58\u50a8\u7a7a\u95f4\u6ee1\u65f6\u624d\u4f1a\u53d1\u751f\u963b\u585e\u3002\u540c\u7406\uff0c\u5982\u679c\u7f13\u51b2\u901a\u9053\u4e2d\u6709\u6570\u636e\uff0c\u63a5\u6536\u65f6\u5c06\u4e0d\u4f1a\u53d1\u751f\u963b\u585e\uff0c\u76f4\u5230\u901a\u9053\u4e2d\u6ca1\u6709\u6570\u636e\u53ef\u8bfb\u65f6\uff0c\u901a\u9053\u5c06\u4f1a\u518d\u5ea6\u963b\u585e(\u6ca1\u6709\u6570\u636e\u4f46\u662f\u8fd8\u5728\u63a5\u6536)<\/p>\n\n\n\n<p class=\"has-vivid-purple-color has-text-color wp-block-paragraph\"><strong>\u521b\u5efa\u5e26\u7f13\u51b2\u901a\u9053\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u901a\u9053\u5b9e\u4f8b := make(chan \u901a\u9053\u7c7b\u578b,\u7f13\u51b2\u5927\u5c0f)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u901a\u9053\u7c7b\u578b\uff1a\u548c\u65e0\u7f13\u51b2\u901a\u9053\u7528\u6cd5\u4e00\u81f4\uff0c\u5f71\u54cd\u901a\u9053\u53d1\u9001\u548c\u63a5\u6536\u7684\u6570\u636e\u7c7b\u578b<\/li>\n\n\n\n<li>\u7f13\u51b2\u5927\u5c0f\uff1a\u51b3\u5b9a\u901a\u9053\u6700\u591a\u53ef\u4ee5\u4fdd\u5b58\u7684\u5143\u7d20\u6570\u91cf<\/li>\n\n\n\n<li>\u901a\u9053\u5b9e\u4f8b\uff1a \u88ab\u521b\u5efa\u51fa\u7684\u901a\u9053\u5b9e\u4f8b<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4f8b\u5b50\u8868\u793a\u521b\u5efa\u5e26\u67095\u4e2a\u5143\u7d20\u7f13\u51b2\u5927\u5c0f\u7684\u6574\u6570\u7c7b\u578b\u901a\u9053\uff0c\u53d1\u9001\u6570\u636e\u524d\u540e\u6253\u5370\u7f13\u51b2\u533a\u5927\u5c0f\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"274\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/17-2.png\" alt=\"\" class=\"wp-image-6995\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/17-2.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/17-2-300x116.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/17-2-600x232.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"95\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/18-2.png\" alt=\"\" class=\"wp-image-6996\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/18-2.png 707w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/18-2-300x40.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/18-2-600x81.png 600w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u4f8b\u5b50\u4e2d\u53ea\u6709\u6570\u636e\u53d1\u9001\u5230\u901a\u9053\uff0c\u5e76\u6ca1\u6709\u63a5\u6536\u8005\uff0c\u53d1\u9001\u8005\u4e5f\u4e0d\u4f1a\u53d1\u751f\u963b\u585e\uff0c\u56e0\u4e3a\u6709\u7f13\u51b2\u533a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e26\u7f13\u51b2\u901a\u9053\u5728\u4e0b\u9762\u5217\u4e3e\u7684\u60c5\u51b5\u4e0b\u4f9d\u7136\u4f1a\u53d1\u751f\u963b\u585e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5e26\u7f13\u51b2\u901a\u9053\u88ab\u586b\u6ee1\u65f6\uff0c \u5c1d\u8bd5\u518d\u6b21\u53d1\u9001\u6570\u636e\u65f6\u53d1\u751f\u963b\u585e<\/li>\n\n\n\n<li>\u5e26\u7f13\u51b2\u901a\u9053\u4e3a\u7a7a\u65f6\uff0c\u5c1d\u8bd5\u63a5\u6536\u6570\u636e\u65f6\u53d1\u751f\u963b\u585e<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4ec0\u4e48Go\u8bed\u8a00\u5bf9\u901a\u9053\u8981\u9650\u5236\u957f\u5ea6\u800c\u4e0d\u63d0\u4f9b\u65e0\u9650\u957f\u5ea6\u7684\u901a\u9053\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u9053(channel)\u662f\u5728\u4e24\u4e2agoroutine \u95f4\u901a\u4fe1\u7684\u6865\u6881\u3002\u4f7f\u7528goroutine \u7684\u4ee3\u7801\u5fc5\u7136\u6709\u4e00\u65b9\u63d0\u4f9b\u6570\u636e\uff0c\u4e00\u65b9\u6d88\u8d39\u6570\u636e\u3002\u5f53\u63d0\u4f9b\u6570\u636e\u4e00\u65b9\u7684\u6570\u636e\u4f9b\u7ed9\u901f\u5ea6\u5927\u4e8e\u6d88\u8d39\u65b9\u7684\u6570\u636e\u5904\u7406\u901f\u5ea6\u65f6\uff0c\u5982\u679c\u901a\u9053\u4e0d\u9650\u5236\u957f\u5ea6\uff0c\u90a3\u4e48\u5185\u5b58\u5c06\u4e0d\u65ad\u81a8\u80c0\u76f4\u5230\u5e94\u7528\u5d29\u6e83\u3002\u56e0\u6b64\uff0c\u9650\u5236\u901a\u9053\u7684\u957f\u5ea6\u6709\u5229\u4e8e\u7ea6\u675f\u6570\u636e\u63d0\u4f9b\u65b9\u7684\u4f9b\u7ed9\u901f\u5ea6\uff0c\u4f9b\u7ed9\u6570\u636e\u91cf\u5fc5\u987b\u5728\u6d88\u8d39\u65b9\u5904\u7406\u91cf\uff0b\u901a\u9053\u957f\u5ea6\u7684\u8303\u56f4\u5185\uff0c\u624d\u80fd\u6b63\u5e38\u5730\u5904\u7406\u6570\u636e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>8\u3001\u901a\u9053\u7684\u591a\u8def\u590d\u7528&#8212;-\u540c\u65f6\u5904\u7406\u63a5\u6536\u548c\u53d1\u9001\u591a\u4e2a\u901a\u9053\u7684\u6570\u636e(select\u8bed\u53e5)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u591a\u8def\u590d\u7528\u901a\u5e38\u8868\u793a\u5728\u4e00\u4e2a\u4fe1\u9053\u4e0a\u4f20\u8f93\u591a\u8def\u4fe1\u53f7\u6216\u6570\u636e\u6d41\u7684\u8fc7\u7a0b\u548c\u6280\u672f\uff0c\u4f8b\u5982\uff1a\u7535\u8bdd\uff0c\u7f51\u7ebf\uff0c\u5149\u7ea4\u90fd\u5c5e\u4e8e\u591a\u8def\u590d\u7528\uff0c\u652f\u6301\u591a\u4eba\u540c\u65f6\u6536\u53d1\u6570\u636e<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u8bed\u8a00\u4e2d\u63d0\u4f9b\u4e86select\u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u540c\u65f6\u54cd\u5e94\u591a\u4e2a\u901a\u9053\u7684\u64cd\u4f5c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">select \u5c31\u662f\u7528\u6765\u76d1\u542c\u548c channel \u6709\u5173\u7684 IO \u64cd\u4f5c\uff0c\u5f53 IO \u64cd\u4f5c\u53d1\u751f\u65f6\uff0c\u89e6\u53d1\u76f8\u5e94\u7684\u52a8\u4f5c\u3002<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">select \u53ea\u80fd\u5e94\u7528\u4e8e channel \u7684\u64cd\u4f5c<\/mark><\/strong>\uff0c\u65e2\u53ef\u4ee5\u7528\u4e8e channel \u7684\u6570\u636e\u63a5\u6536\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e channel \u7684\u6570\u636e\u53d1\u9001\u3002\u5982\u679c select \u7684\u591a\u4e2a\u5206\u652f\u90fd\u6ee1\u8db3\u6761\u4ef6\uff0c\u5219\u4f1a\u968f\u673a\u7684\u9009\u53d6\u5176\u4e2d\u4e00\u4e2a\u6ee1\u8db3\u6761\u4ef6\u7684\u5206\u652f\u6267\u884c\uff0c\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select {\n\tcase &lt;- chan1:\n\t\t\/\/ \u5982\u679c chan1 \u6210\u529f\u8bfb\u5230\u6570\u636e\uff0c\u5219\u6267\u884c\u8be5 case \u5904\u7406\u8bed\u53e5\n\tcase chan2 &lt;- 2:\n\t\t\/\/ \u5982\u679c\u6210\u529f\u5411 chan2 \u5199\u5165\u6570\u636e\uff0c\u5219\u6267\u884c\u8be5 case \u5904\u7406\u8bed\u53e5\n\tdefault:\n\t\t\/\/ \u5982\u679c\u4e0a\u9762\u90fd\u6ca1\u6709\u6210\u529f\uff0c\u5219\u8fdb\u5165default\u5904\u7406\u6d41\u7a0b\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u9053\u6536\u53d1\u8bed\u53e5\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">\u64cd\u4f5c<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u8bed\u53e5\u793a\u4f8b<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">\u63a5\u6536\u4efb\u610f\u6570\u636e<\/td><td class=\"has-text-align-center\" data-align=\"center\">case &lt;- ch:<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">\u63a5\u6536\u53d8\u91cf<\/td><td class=\"has-text-align-center\" data-align=\"center\">case d := &lt;-ch :<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">\u53d1\u9001\u6570\u636e<\/td><td class=\"has-text-align-center\" data-align=\"center\">case ch &lt;- 100:<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u670d\u52a1\u5668\u5f00\u53d1\u4e2d\u4f1a\u4f7f\u7528RPC ( Remote Procedure Call \uff0c\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\uff09\u7b80\u5316\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u8fc7\u7a0b\u3002RPC \u80fd\u6709\u6548\u5730\u5c01\u88c5\u901a\u4fe1\u8fc7\u7a0b\uff0c\u8ba9\u8fdc\u7a0b\u7684\u6570\u636e\u6536\u53d1\u901a\u4fe1\u8fc7\u7a0b\u770b\u8d77\u6765\u5c31\u50cf\u672c\u5730\u7684\u51fd\u6570\u8c03\u7528\u4e00\u6837<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">(1)\u3001\u4e0b\u9762\u4f8b\u5b50\u4f7f\u7528\u901a\u9053\u4ee3\u7406Socket\u5b9e\u73b0RPC\u7684\u8fc7\u7a0b\uff0c\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u5728\u4e24\u4e2agoroutine \u4e2d\u8fd0\u884c\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"407\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/19-2.png\" alt=\"\" class=\"wp-image-7005\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/19-2.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/19-2-300x172.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/19-2-600x345.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c11\u884c\uff1a\u4f7f\u7528select\u5f00\u59cb\u505a\u591a\u8def\u590d\u7528<\/li>\n\n\n\n<li>\u7b2c12\u884c\u548c\u7b2c14\u884c\u7684\u901a\u9053\u4f1a\u540c\u65f6\u5f00\u542f\uff0c\u5982\u679c\u7b2c12\u884c\u7684\u901a\u9053\u5148\u8fd4\u56de\uff0c\u5219\u6267\u884c\u7b2c13\u884c\u7684\u903b\u8f91\uff0c\u5982\u679c\u7b2c14\u884c\u7684\u901a\u9053\u5148\u8fd4\u56de\u5219\u6267\u884c\u7b2c15\u884c\u7684\u903b\u8f91\uff0c\u8868\u793a\u8bf7\u6c42\u8d85\u65f6\uff0c\u629b\u51fa\u9519\u8bef<\/li>\n\n\n\n<li>time.After\u4e3atime\u5305\u63d0\u4f9b\u7684\u51fd\u6570After()\uff0c\u6b64\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u901a\u9053\uff0c\u5728\u6307\u5b9a\u65f6\u95f4\u540e(time.Second)\uff0c\u901a\u8fc7\u901a\u9053\u8fd4\u56de\u5f53\u524d\u65f6\u95f4<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"92\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/20-2.png\" alt=\"\" class=\"wp-image-7009\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/20-2.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/20-2-300x39.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/20-2-600x78.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">(2)\u3001\u4e0b\u9762\u4f8b\u5b50\u4e2d\uff0c\u5411\u901a\u9053\u4e2d\u53d1\u9001\u4e86\u6570\u636e\uff0c\u56e0\u6b64\u7b2c\u4e00\u4e2acase\u6ee1\u8db3\u6761\u4ef6\uff0c\u5c06\u4f1a\u6267\u884c\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"348\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/16.png\" alt=\"\" class=\"wp-image-10455\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/16.png 710w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/16-300x147.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/16-600x294.png 600w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"79\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/17.png\" alt=\"\" class=\"wp-image-10456\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/17.png 710w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/17-300x33.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/17-600x67.png 600w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">(3)\u3001\u4e0b\u9762\u4f8b\u5b50\u5c06ch &lt;- 100\u6ce8\u91ca\uff0c\u6b64\u65f6\u7b2c\u4e8c\u4e2acase\u6ee1\u8db3\u6761\u4ef6\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"362\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/1.png\" alt=\"\" class=\"wp-image-10458\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/1.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/1-300x153.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/1-600x307.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"70\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/2.png\" alt=\"\" class=\"wp-image-10459\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/2.png 710w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/2-300x30.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/2-600x59.png 600w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">(4)\u3001\u4e0a\u8ff0\u6761\u4ef6\u90fd\u4e0d\u6ee1\u8db3\uff0c\u6267\u884cdefault\u8bed\u53e5\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"319\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/2-1.png\" alt=\"\" class=\"wp-image-10461\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/2-1.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/2-1-300x135.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/2-1-600x270.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"63\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/3.png\" alt=\"\" class=\"wp-image-10462\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/3.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/3-300x27.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/3-600x53.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">(5)\u3001\u5982\u679c\u6709\u591a\u4e2acase\u6ee1\u8db3\u6761\u4ef6\uff0c\u5219\u968f\u673a\u6267\u884c\u4e00\u4e2a\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"344\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/4.png\" alt=\"\" class=\"wp-image-10464\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/4.png 710w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/4-300x145.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/4-600x291.png 600w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"215\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/5.png\" alt=\"\" class=\"wp-image-10465\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/5.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/5-300x91.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/5-600x182.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">(6)\u3001\u5982\u679cselect\u4e2d\u6ca1\u6709\u4efb\u4f55\u53c2\u6570\uff0c\u6b64\u65f6\u8868\u793a\u963b\u585e\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"590\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/1-1.png\" alt=\"\" class=\"wp-image-10467\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/1-1.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/1-1-300x250.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/1-1-600x499.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u56fe\u4e2d\u4e3a\u4e00\u4e2a\u5b9a\u65f6\u4efb\u52a1\u7a0b\u5e8f,\u6bcf\u96945s\u8fd0\u884c\u4e00\u6b21\uff0c\u901a\u8fc7select{}\u963b\u585e\u8ba9\u7a0b\u5e8f\u5b9a\u65f6\u6267\u884c\uff0c\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"225\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/2-2.png\" alt=\"\" class=\"wp-image-10468\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/2-2.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/2-2-300x95.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/2-2-600x190.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\uff1aselect{}\u4e2d\u4e3a\u7a7a\u6b64\u65f6\u4e3a\u963b\u585e\uff0c\u7b49\u5f85\u4e0b\u4e00\u8f6e\u5b9a\u65f6\u4efb\u52a1\u6267\u884c,\u5982\u679c\u8981\u904d\u5386\u901a\u9053\u4e2d\u7684\u5185\u5bb9\uff0c\u9700\u8981\u5c06\u904d\u5386\u8bed\u53e5\u653e\u5728select{}\u524d\u9762\uff0c\u5982\u679c\u653e\u5728\u540e\u9762\uff0c\u90a3\u4e48select{}\u963b\u585e\u4e86\uff0c\u5c31\u6c38\u8fdc\u65e0\u6cd5\u6267\u884c\u540e\u9762\u7684\u4ee3\u7801\uff0c\u56e0\u6b64\u8981\u653e\u5728\u524d\u9762<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>9\u3001\u5173\u95ed\u901a\u9053\u540e\u7ee7\u7eed\u4f7f\u7528\u901a\u9053<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u9053\u662f\u4e00\u4e2a\u5f15\u7528\u5bf9\u8c61\uff0c\u548cmap\u7c7b\u4f3c ,  map \u5728\u6ca1\u6709\u4efb\u4f55\u5916\u90e8\u5f15\u7528\u65f6\uff0c Go \u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6(runtime) \u4f1a\u81ea\u52a8\u5bf9\u5185\u5b58\u8fdb\u884c\u5783\u573e\u56de\u6536\uff08 Garbage Collection , G C \uff09\uff0c\u901a\u9053\u4e5f\u53ef\u4ee5\u88ab\u5783\u573e\u56de\u6536\uff0c\u4f46\u662f\u901a\u9053\u4e5f\u53ef\u4ee5\u88ab\u4e3b\u52a8\u5173\u95ed<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528close()\u6765\u5173\u95ed\u4e00\u4e2a\u901a\u9053\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>close(ch)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5173\u95ed\u7684\u901a\u9053\u4f9d\u7136\u53ef\u4ee5\u88ab\u8bbf\u95ee\uff0c\u8bbf\u95ee\u88ab\u5173\u95ed\u7684\u901a\u9053\u5c06\u4f1a\u53d1\u751f\u4e00\u4e9b\u95ee\u9898\uff0c\u88ab\u5173\u95ed\u7684\u901a\u9053\u4e0d\u4f1a\u88ab\u7f6e\u4e3anil<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5bf9\u5df1\u7ecf\u5173\u95ed\u7684\u901a\u9053\u8fdb\u884c\u53d1\u9001\uff0c\u5c06\u4f1a\u89e6\u53d1\u5ca9\u673a\uff0c\u5982\u56fe\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"252\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/21-2.png\" alt=\"\" class=\"wp-image-7013\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/21-2.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/21-2-300x107.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/21-2-600x214.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5173\u95ed\u901a\u9053\u540e\uff0c\u518d\u6b21\u5411\u901a\u9053\u4e2d\u5199\u5165\u6570\u636e\uff0c\u5c06\u4f1a\u89e6\u53d1\u5b95\u673a\uff0c\u4e0a\u8ff0\u4ee3\u7801\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"186\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/22-2.png\" alt=\"\" class=\"wp-image-7014\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/22-2.png 707w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/22-2-300x79.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/22-2-600x158.png 600w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u5df1\u5173\u95ed\u7684\u901a\u9053\u63a5\u6536\u6570\u636e\u65f6\u5c06\u4e0d\u4f1a\u53d1\u751f\u963b\u585e\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"291\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1-5.png\" alt=\"\" class=\"wp-image-7016\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1-5.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1-5-300x123.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1-5-600x247.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"114\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2-5.png\" alt=\"\" class=\"wp-image-7018\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2-5.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2-5-300x48.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2-5-600x97.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c6\u884c\uff1a\u5b9a\u4e49\u4fdd\u5b583\u4e2a\u5143\u7d20\u7684\u7f13\u51b2\u901a\u9053\uff0c\u7c7b\u578b\u6211\u6574\u578b<\/li>\n\n\n\n<li>\u7b2c7-\u7b2c8\u884c\uff1a\u7ed9\u901a\u9053\u53d1\u90012\u4e2a\u6570\u636e<\/li>\n\n\n\n<li>\u7b2c9\u884c\uff1a\u5173\u95ed\u901a\u9053<\/li>\n\n\n\n<li>\u7b2c10\u884c\uff1a\u901a\u8fc7for\u5faa\u73af\u904d\u5386\u901a\u9053\uff0ccap()\u51fd\u6570\u53ef\u4ee5\u83b7\u53d6\u4e00\u4e2a\u5bf9\u8c61\u7684\u5bb9\u91cf<\/li>\n\n\n\n<li>\u7b2c11\u884c\uff1a\u4ece\u901a\u9053\u4e2d\u83b7\u53d6\u503c<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u8fd0\u884c\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa\uff0c\u901a\u9053\u4e2d\u53ef\u5bb9\u7eb33\u4e2a\u5143\u7d20\uff0c\u5b9e\u9645\u53ea\u5b58\u653e\u4e862\u4e2a\uff0c\u901a\u9053\u5173\u95ed\u540e\u4f9d\u7136\u53ef\u4ee5\u6b63\u5e38\u83b7\u53d6\u901a\u9053\u4e2d\u7684\u503c\uff0c\u4f46\u662f\u904d\u5386\u5230\u7b2c3\u4e2a\u7684\u65f6\u5019\uff0c\u7ed3\u679c\u4e3a0\uff0c0\u8868\u793a\u901a\u9053\u7684\u9ed8\u8ba4\u503c\uff0c\u56e0\u4e3a\uff0c\u5f53\u901a\u9053\u5173\u95ed\u540e\uff0c\u53ca\u65f6\u6ca1\u6709\u6570\u636e\uff0c\u5728\u83b7\u53d6\u503c\u4e5f\u4e0d\u4f1a\u53d1\u751f\u963b\u585e\uff0c\u53d6\u7684\u503c\u4e3a\u9ed8\u8ba4\u503c<\/p>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\u610f:\u5173\u95ed\u5df2\u7ecf\u5173\u95ed\u7684channel\u4e5f\u4f1a\u5f15\u53d1panic<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>\u56db\u3001\u540c\u6b65\u4e00\u4e00\u4fdd\u8bc1\u5e76\u53d1\u73af\u5883\u4e0b\u6570\u636e\u8bbf\u95ee\u7684\u6b63\u786e\u6027(\u5e76\u53d1\u5b89\u5168)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e76\u53d1\u5b89\u5168\uff1a\u5728\u5e76\u53d1\u6267\u884c\u7684\u73af\u5883\u4e2d\uff0c\u5171\u4eab\u6570\u636e\u53ef\u4ee5\u88ab\u6b63\u786e\u6267\u884c\uff0c\u4e0d\u4f1a\u53d1\u751f\u7d0a\u4e71\uff0c\u6b64\u65f6\u5373\u4e3a\u5e76\u53d1\u5b89\u5168\uff0c\u5426\u5219\u5e76\u53d1\u4e0d\u5b89\u5168<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u7a0b\u5e8f\u53ef\u4ee5\u4f7f\u7528\u901a\u9053\u8fdb\u884c\u591a\u4e2agoroutine \u95f4\u7684\u6570\u636e\u4ea4\u6362\uff0c\u4f46\u8fd9\u4ec5\u4ec5\u662f\u6570\u636e\u540c\u6b65\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\u3002\u901a\u9053\u5185\u90e8\u7684\u5b9e\u73b0\u4f9d\u7136\u4f7f\u7528\u4e86\u5404\u79cd\u9501\uff0c\u56e0\u6b64\u4f18\u96c5\u4ee3\u7801\u7684\u4ee3\u4ef7\u662f\u6027\u80fd\u3002\u5728\u67d0\u4e9b\u8f7b\u91cf\u7ea7\u7684\u573a\u5408\uff0c\u539f\u5b50\u8bbf\u95ee\uff08 atomic \u5305\uff09 \u3001\u4e92\u65a5\u9501\uff08 sync .Mutex \uff09\u4ee5\u53ca\u7b49\u5f85\u7ec4\uff08 sync.WaitGroup )\u80fd\u6700\u5927\u7a0b\u5ea6\u6ee1\u8db3\u9700\u6c42<\/p>\n\n\n\n<p class=\"has-vivid-purple-color has-text-color wp-block-paragraph\">1\u3001<strong>\u7ade\u6001\u68c0\u6d4b\u4e00\u4e00\u68c0\u6d4b\u4ee3\u7801\u5728\u5e76\u53d1\u73af\u5883\u4e0b\u53ef\u80fd\u51fa\u73b0\u7684\u95ee\u9898<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u591a\u7ebf\u7a0b\u5e76\u53d1\u8fd0\u884c\u7684\u7a0b\u5e8f\u7ade\u4e89\u8bbf\u95ee\u548c\u4fee\u6539\u540c\u4e00\u5757\u8d44\u6e90\u65f6\uff0c\u4f1a\u53d1\u751f\u7ade\u6001\u95ee\u9898(\u5e76\u53d1\u4e0d\u5b89\u5168)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">(1)\u3001\u4e0b\u9762\u4f8b\u5b50\u5e76\u53d1\u6267\u884cadd()\u51fd\u6570\uff0c\u5bf9\u540c\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u64cd\u4f5c\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"539\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/1-4.png\" alt=\"\" class=\"wp-image-10535\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/1-4.png 710w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/1-4-300x228.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/1-4-600x455.png 600w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>WaitGroup\uff1a\u7b49\u5f85\u7ec4\uff0c\u5728\u6587\u7ae0\u6700\u540e\u9762\u4f1a\u8bb2\u7528\u9014\u662f\u4ec0\u4e48<\/li>\n\n\n\n<li>go add()\uff1a\u5e76\u53d1\u6267\u884c\u8fd9\u4e24\u4e2a\u51fd\u6570\uff0c\u5982\u679c\u5f53\u524d\u7cfb\u7edfcpu\u6838\u5fc3\u4e3a1\u4e2a\uff0c\u90a3\u4e48\u770b\u4e0d\u51fa\u5e76\u53d1\u6548\u679c\uff0c\u8fd8\u662f\u4e00\u4e2a\u4e00\u4e2a\u6267\u884c\uff0c\u5982\u679c\u662f\u591a\u4e2acpu\uff0c\u5c31\u53ef\u4ee5\u770b\u51fa\u6548\u679c<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u9884\u671f\u7684\u7ed3\u679c\u662f10000\uff0c\u4f46\u5b9e\u9645\u6709\u65f6\u5019\u4e0d\u7b26\u5408\u9884\u671f\uff0c\u5982\u4e0b(\u57282\u9897cpu\u673a\u5668\u4e0a\u6267\u884c)\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"253\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/2-5.png\" alt=\"\" class=\"wp-image-10538\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/2-5.png 710w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/2-5-300x107.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/2-5-600x214.png 600w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\u4e24\u4e2ago add\u5e76\u53d1\u6267\u884c\uff0c\u90fd\u53bb\u4fee\u6539\u5168\u5c40\u53d8\u91cfx\uff0c\u5c31\u4f1a\u9020\u6210\u7ade\u6001\u95ee\u9898\uff0c\u5bfc\u81f4\u7ed3\u679c\u4e0d\u7b26\u5408\u9884\u671f\uff0c\u4e5f\u53ef\u4ee5\u7406\u89e3\u4e3a<strong>\u5e76\u53d1\u4e0d\u5b89\u5168<\/strong>\uff0c\u7ed3\u679c\u7d0a\u4e71\uff0c\u901a\u8fc7\u547d\u4ee4go run -face\u53ef\u4ee5\u67e5\u770b\u5230\u9759\u6001\u95ee\u9898\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"712\" height=\"374\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/6-1.png\" alt=\"\" class=\"wp-image-10555\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/6-1.png 712w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/6-1-300x158.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/6-1-600x315.png 600w\" sizes=\"auto, (max-width: 712px) 100vw, 712px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">2\u3001\u539f\u5b50\u64cd\u4f5c(atomic)<\/mark><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">(1)\u3001\u4e0b\u9762\u901a\u8fc7\u539f\u5b50\u64cd\u4f5c(atomic)\u89e3\u51b3\u4e0a\u8ff0\u7684\u9759\u6001\u95ee\u9898\uff0cAddInt64()\u51fd\u6570\u5f3a\u5236\u540c\u4e00\u65f6\u523b\u53ea\u80fd\u6709\u4e00\u4e2agorountie\u8fd0\u884c\u5e76\u5b8c\u6210\u8fd9\u4e2a\u52a0\u6cd5\u64cd\u4f5c\u3002\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"548\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/8-1.png\" alt=\"\" class=\"wp-image-10558\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/8-1.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/8-1-300x232.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/8-1-600x464.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u518d\u6b21\u6267\u884cgo run -race bing.go\u5c06\u4e0d\u4f1a\u51fa\u73b0\u7ade\u6001\u95ee\u9898\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"80\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/9-1.png\" alt=\"\" class=\"wp-image-10559\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/9-1.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/9-1-300x34.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/9-1-600x68.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\uff1a\u5bf9\u53d8\u91cf\u8fdb\u884c\u589e\u51cf\u64cd\u4f5c\uff0c\u867d\u7136\u53ef\u4ee5\u4f7f\u7528\u4e92\u65a5\u9501\uff08sync. Mutex\uff09\u89e3\u51b3\u7ade\u6001\u95ee\u9898\uff0c\u4f46\u662f\u5bf9\u6027\u80fd\u6d88\u8017\u8f83\u5927\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u63a8\u8350\u4f7f\u7528\u539f\u5b50\u64cd\u4f5c\uff08 atomic \uff09\u8fdb\u884c\u53d8\u91cf\u64cd\u4f5c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3\u3001<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u4e92\u65a5\u9501(sync. Mutex)\u4e00\u4e00\u4fdd\u8bc1\u540c\u65f6\u53ea\u6709\u4e00\u4e2agoroutine\u53ef\u4ee5\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90<\/mark><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9664\u4e86\u539f\u5b50\u64cd\u4f5c\uff0c\u4e92\u65a5\u9501\u662f\u4e00\u79cd\u5e38\u7528\u7684\u63a7\u5236\u5171\u4eab\u8d44\u6e90\u8bbf\u95ee\u7684\u65b9\u6cd5(sync.Mutex)\uff0c\u5b83\u80fd\u591f\u4fdd\u8bc1\u540c\u65f6\u53ea\u6709\u4e00\u4e2agoroutine\u53ef\u4ee5\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\uff0cGo\u8bed\u8a00\u4e2d\u4f7f\u7528sync\u5305\u7684Mutex\u7c7b\u578b\u6765\u5b9e\u73b0\u4e92\u65a5\u9501<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">(1)\u3001\u901a\u8fc7\u4e92\u65a5\u9501\u5bf9\u4e0a\u9762\u4f8b\u5b50\u4e2d\u53d8\u91cf\u8fdb\u884c\u52a0\u9501\u548c\u89e3\u9501\uff0c\u5b9e\u73b0\u5e76\u53d1\u5b89\u5168\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"584\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/3-3.png\" alt=\"\" class=\"wp-image-10545\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/3-3.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/3-3-300x247.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/3-3-600x495.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\u7b26\u5408\u9884\u671f\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"289\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/4-3.png\" alt=\"\" class=\"wp-image-10546\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/4-3.png 710w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/4-3-300x122.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/4-3-600x244.png 600w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\u610f\uff1a\u4f7f\u7528\u4e92\u65a5\u9501\u80fd\u591f\u4fdd\u8bc1\u540c\u4e00\u65f6\u95f4\u6709\u4e14\u53ea\u6709\u4e00\u4e2agoroutine\u8fdb\u5165\u4e34\u754c\u533a\uff0c\u5176\u4ed6\u7684goroutine\u5219\u5728\u7b49\u5f85\u9501\uff1b\u5f53\u4e92\u65a5\u9501\u91ca\u653e\u540e\uff0c\u7b49\u5f85\u7684goroutine\u624d\u53ef\u4ee5\u83b7\u53d6\u9501\u8fdb\u5165\u4e34\u754c\u533a\uff0c\u591a\u4e2agoroutine\u540c\u65f6\u7b49\u5f85\u4e00\u4e2a\u9501\u65f6\uff0c\u5524\u9192\u7684\u7b56\u7565\u662f\u968f\u673a\u7684<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">(2)\u3001\u4e0b\u9762\u4f8b\u5b50\u4e2d\u901a\u8fc7\u4e92\u65a5\u9501\u5bf9\u53d8\u91cf\u8fdb\u884c\u52a0\u9501\u548c\u89e3\u9501\u7684\u64cd\u4f5c\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"578\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-5.png\" alt=\"\" class=\"wp-image-7031\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-5.png 707w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-5-300x245.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-5-600x491.png 600w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"705\" height=\"83\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/8-5.png\" alt=\"\" class=\"wp-image-7033\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/8-5.png 705w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/8-5-300x35.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/8-5-600x71.png 600w\" sizes=\"auto, (max-width: 705px) 100vw, 705px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c18\u884c\uff1a\u5bf9\u4e92\u65a5\u91cf\u8fdb\u884c\u52a0\u9501\uff0c\u4e00\u65e6\u53d1\u751f\u52a0\u9501\uff0c\u5982\u679c\u53e6\u4e00\u4e2agoroutine\u5c1d\u8bd5\u7ee7\u7eed\u52a0\u9501\u5c31\u4f1a\u53d1\u751f\u963b\u585e\uff0c\u76f4\u5230\u8fd9\u4e2acountGuard\u88ab\u89e3\u9501\uff0c\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u4fdd\u8bc1\u5bf9\u53d8\u91cf\u7684\u64cd\u4f5c\u4e3a\u539f\u5b50\u64cd\u4f5c(\u539f\u5b50\u64cd\u4f5c\u5373\u662f\u8fdb\u884c\u8fc7\u7a0b\u4e2d\u4e0d\u80fd\u88ab\u4e2d\u65ad\u7684\u64cd\u4f5c)<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\uff083\uff09\u3001\u7ed9goroutine\u52a0\u4e92\u65a5\u9501\uff0cmain\u5148\u62ff\u5230\u9501\u7684\u5c06\u4f1a\u5148\u6267\u884c\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"645\" height=\"519\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/9-1.png\" alt=\"\" class=\"wp-image-18156\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/9-1.png 645w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/9-1-300x241.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/9-1-600x483.png 600w\" sizes=\"auto, (max-width: 645px) 100vw, 645px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"648\" height=\"136\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/10.png\" alt=\"\" class=\"wp-image-18157\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/10.png 648w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/10-300x63.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/10-600x126.png 600w\" sizes=\"auto, (max-width: 648px) 100vw, 648px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>4\u3001<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u8bfb\u5199\u4e92\u65a5\u9501(sync.RWMutex)&#8211;\u5728\u8bfb\u6bd4\u5199\u591a\u7684\u73af\u5883\u4e0b\u6bd4\u4e92\u65a5\u9501\u66f4\u9ad8\u6548<\/mark><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e92\u65a5\u9501\u662f\u5b8c\u5168\u4e92\u65a5\u7684\uff0c\u4f46\u662f\u6709\u5f88\u591a\u5b9e\u9645\u7684\u573a\u666f\u4e0b\u662f\u8bfb\u591a\u5199\u5c11\u7684\uff0c\u5f53\u6211\u4eec\u5e76\u53d1\u7684\u53bb\u8bfb\u53d6\u4e00\u4e2a\u8d44\u6e90\u4e0d\u6d89\u53ca\u8d44\u6e90\u4fee\u6539\u7684\u65f6\u5019\u662f\u6ca1\u6709\u5fc5\u8981\u52a0\u9501\u7684\uff0c\u8fd9\u79cd\u573a\u666f\u4e0b\u4f7f\u7528\u8bfb\u5199\u9501\u662f\u66f4\u597d\u7684\u4e00\u79cd\u9009\u62e9\u3002\u8bfb\u5199\u9501\u5728Go\u8bed\u8a00\u4e2d\u4f7f\u7528sync\u5305\u4e2d\u7684RWMutex\u7c7b\u578b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"705\" height=\"584\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/11-3.png\" alt=\"\" class=\"wp-image-7046\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/11-3.png 705w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/11-3-300x249.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/11-3-600x497.png 600w\" sizes=\"auto, (max-width: 705px) 100vw, 705px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c14\u884c\uff1a\u5c06sync.Mutex\u4e92\u65a5\u9501\u6539\u4e3async.RWMutex\u8bfb\u5199\u4e92\u65a5\u9501<\/li>\n\n\n\n<li>\u7b2c18\u884c\uff1a\u83b7\u53d6count\u7684\u8fc7\u7a0b\u662f\u4e00\u4e2a\u8bfb\u53d6count\u6570\u636e\u7684\u8fc7\u7a0b\uff0c\u9002\u7528\u4e8e\u8bfb\u5199\u4e92\u65a5\u9501\uff0c\u5c06countGuard.Lock()\u6539\u4e3acountGuard.RLock()\uff0c\u5c06\u8bfb\u5199\u4e92\u65a5\u9501\u6807\u8bb0\u4e3a\u8bfb\u72b6\u6001\uff0c\u5982\u679c\u6b64\u65f6\u53e6\u5916\u4e00\u4e2agoroutine \u5e76\u53d1\u8bbf\u95ee\u4e86countGuard \uff0c \u540c\u65f6\u4e5f\u8c03\u7528\u4e86countGuard.RLock(),\u5e76\u4e0d\u4f1a\u53d1\u751f\u963b\u585e<\/li>\n\n\n\n<li>\u7b2c19\u884c\uff1a\u4f7f\u7528\u8bfb\u6a21\u5f0f\u89e3\u9501<\/li>\n\n\n\n<li>\u7b2c23\u884c\uff1a\u52a0\u5199\u9501<\/li>\n\n\n\n<li>\u7b2c25\u884c\uff1a\u89e3\u5199\u9501<\/li>\n<\/ul>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u8bfb\u53d6\u4e00\u4e2a\u8d44\u6e90\u4e0d\u6d89\u53ca\u8d44\u6e90\u4fee\u6539\u7684\u65f6\u5019\u662f\u6ca1\u6709\u5fc5\u8981\u52a0\u9501\u7684\uff0c\u662f\u8bfb\u5199\u9501\u975e\u5e38\u9002\u5408\u8bfb\u591a\u5199\u5c11\u7684\u573a\u666f\uff0c\u5982\u679c\u8bfb\u548c\u5199\u7684\u64cd\u4f5c\u5dee\u522b\u4e0d\u5927\uff0c\u8bfb\u5199\u9501\u7684\u4f18\u52bf\u5c31\u53d1\u6325\u4e0d\u51fa\u6765<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bfb\u5199\u9501\u5206\u4e3a\u4e24\u79cd\uff1a\u8bfb\u9501\u548c\u5199\u9501\u3002\u5f53\u4e00\u4e2agoroutine\u83b7\u53d6\u8bfb\u9501\u4e4b\u540e\uff0c\u5176\u4ed6\u7684goroutine\u5982\u679c\u662f\u83b7\u53d6\u8bfb\u9501\u4f1a\u7ee7\u7eed\u83b7\u5f97\u9501\uff0c\u5982\u679c\u662f\u83b7\u53d6\u5199\u9501\u5c31\u4f1a\u7b49\u5f85\uff1b\u5f53\u4e00\u4e2agoroutine\u83b7\u53d6\u5199\u9501\u4e4b\u540e\uff0c\u5176\u4ed6\u7684goroutine\u65e0\u8bba\u662f\u83b7\u53d6\u8bfb\u9501\u8fd8\u662f\u5199\u9501\u90fd\u4f1a\u7b49\u5f85<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ce8\uff1a\u5728\u4f7f\u7528RLock()\u548cRUnlock()\u3001Lock()\u548cUnlock()\u7b49\u51fd\u6570\u65f6\uff0c\u5b83\u4eec\u5fc5\u987b\u6210\u5bf9\u51fa\u73b0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">5\u3001\u7b49\u5f85\u7ec4(sync.WaitGroup)&#8212;\u4fdd\u8bc1\u5728\u5e76\u53d1\u73af\u5883\u4e2d\u5b8c\u6210\u6307\u5b9a\u6570\u91cf\u7684\u4efb\u52a1<\/mark><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9664\u4e86\u53ef\u4ee5\u4f7f\u7528\u901a\u9053\uff08 channel \uff09\u548c\u4e92\u65a5\u9501\u8fdb\u884c\u4e24\u4e2a\u5e76\u53d1\u7a0b\u5e8f\u95f4\u7684\u540c\u6b65\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7b49\u5f85\u7ec4\u8fdb\u884c\u591a\u4e2a\u4efb\u52a1\u7684\u540c\u6b65<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b49\u5f85\u7ec4\u6709\u4e0b\u9762\u51e0\u4e2a\u65b9\u6cd5\u53ef\u7528,\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">\u65b9\u6cd5\u540d<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u529f\u80fd<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">(wg *WaitGroup) Add( delta int)<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u7b49\u5f85\u7ec4\u7684\u8ba1\u6570\u5668\uff0b 1<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">(wg *WaitGroup) Done()<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u7b49\u5f85\u7ec4\u7684\u8ba1\u6570\u5668\uff0d 1<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">(wg *WaitGroup) Wait()<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5f53\u7b49\u5f85\u7ec4\u8ba1\u6570\u5668\u4e0d\u7b49\u4e8e0\u65f6\u963b\u585e\u76f4\u5230\u53d80 <\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b49\u5f85\u7ec4\u5185\u90e8\u62e5\u6709\u4e00\u4e2a\u8ba1\u6570\u5668\uff0c\u8ba1\u6570\u5668\u7684\u503c\u53ef\u4ee5\u901a\u8fc7\u65b9\u6cd5\u8c03\u7528\u5b9e\u73b0\u8ba1\u6570\u5668\u7684\u589e\u52a0\u548c\u51cf\u5c11\u3002\u5f53\u6211\u4eec\u6dfb\u52a0\u4e86N \u4e2a\u5e76\u53d1\u4efb\u52a1\u8fdb\u884c\u5de5\u4f5c\u65f6\uff0c\u5c31\u5c06\u7b49\u5f85\u7ec4\u7684\u8ba1\u6570\u5668\u503c\u589e\u52a0N\u3002\u6bcf\u4e2a\u4efb\u52a1\u5b8c\u6210\u65f6\uff0c\u8fd9\u4e2a\u503c\u51cf1\uff0c\u540c\u65f6\uff0c\u5728\u53e6\u5916\u4e00\u4e2agoroutine \u4e2d\u7b49\u5f85\u8fd9\u4e2a\u7b49\u5f85\u7ec4\u7684\u8ba1\u6570\u5668\u503c\u4e3a0 \u65f6\uff0c \u8868\u793a\u6240\u6709\u4efb\u52a1\u5df1\u7ecf\u5b8c\u6210<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">(1)\u3001\u4e0b\u9762\u4f8b\u5b50\u4e2d\u963b\u585emain\u81ea\u5e26\u7684goroutine\uff0c\u76f4\u5230\u4e24\u4e2ago add\u6267\u884c\u5b8c\u6bd5\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"591\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/5-2.png\" alt=\"\" class=\"wp-image-10549\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/5-2.png 710w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/5-2-300x250.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/5-2-600x499.png 600w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u56fe\u53ef\u4ee5\u7406\u89e3\u4e3a\uff1a\u5728main\u51fd\u6570\u7684goroutine\u4e4b\u524d\u4e4b\u524d\u5148\u7b49\u5f85go add()\u8fd9\u4e24\u4e2a\u534f\u7a0b\u6267\u884c\uff0c\u56e0\u6b64\u8bbe\u7f6ewg.Add()\u4e3a2\uff0c\u6bcf\u6b21\u6267\u884c\u5b8c\u4e00\u4e2a\u5199\u6210\u540e\uff0cwg.Done\u51cf\u53bb1\uff0cwg.Wait()\u7ee7\u7eed\u963b\u585e\u76f4\u5230\u534f\u7a0b\u6570\u91cf\u4e3a0\uff0c\u6b64\u65f6\u8ba4\u4e3a\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\u518d\u6267\u884cmain\u51fd\u6570\u4e2d\u7684goroutine\uff0c<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\"><strong>\u5982\u679c\u4e0d\u4f7f\u7528WaitGroup\uff0c\u90a3\u4e48main\u6240\u5728goroutine\u4f1a\u5728\u5176\u4f59\u7684goroutine\u4e4b\u524d\u7ed3\u675f\u524d\u5148\u6267\u884c\u5b8c\uff0c\u4f1a\u5f97\u5230\u4e0d\u7b26\u5408\u8981\u6c42\u7684\u7ed3\u679c\uff0c\u751a\u81f3\u63d0\u524d\u7ed3\u675f\u8fdb\u7a0b<\/strong><\/mark><\/p>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\u610f\uff1async.WaitGroup\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53\uff0c\u5982\u679c\u5b83\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7684\u65f6\u5019\u8981\u4f20\u9012\u6307\u9488\uff0c\u4e0d\u80fd\u4f7f\u7528time.Sleep\u6765\u66ff\u4ee3WaitGroup\uff0c\u56e0\u4e3atime.Sleep\u662f\u5728\u8d4c\u65f6\u95f4\uff0c\u5982\u679c\u65f6\u95f4\u8bbe\u7f6e\u7684\u4e0d\u591f\uff0c\u4f1a\u5bfc\u81f4\u7ed3\u679c\u8d85\u51fa\u9884\u671f\uff0c\u5728\u6b63\u5f0f\u4ee3\u7801\u91cc\uff0c<strong>\u6c38\u8fdc\u4e0d\u8981<\/strong>\u7528 <code>Sleep<\/code> \u6765\u540c\u6b65 goroutine \u7684\u7ed3\u675f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fdb\u7a0b\u548c\u7ebf\u7a0b\uff1a \u5e76\u53d1\u548c\u5e76\u884c\uff1a \u534f\u7a0b\u548c\u7ebf\u7a0b\uff1a \u5e76\u53d1\u4e0d\u662f\u5e76\u884c\uff1a\u5e76\u53d1\u4e3b\u8981\u7531\u5207\u6362\u65f6\u95f4\u7247\u6765\u5b9e\u73b0&#8221;\u540c\u65f6&#038;#822 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":6858,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111,23],"tags":[],"class_list":["post-6856","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go","category-rong"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"_links":{"self":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/6856","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/comments?post=6856"}],"version-history":[{"count":187,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/6856\/revisions"}],"predecessor-version":[{"id":18914,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/6856\/revisions\/18914"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media\/6858"}],"wp:attachment":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media?parent=6856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/categories?post=6856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/tags?post=6856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}