{"id":206697,"date":"2025-06-12T13:34:26","date_gmt":"2025-06-12T05:34:26","guid":{"rendered":"https:\/\/server.hk\/cnblog\/206697\/"},"modified":"2025-06-12T13:34:26","modified_gmt":"2025-06-12T05:34:26","slug":"%e5%a6%82%e4%bd%95%e4%bf%9d%e8%af%81-go-%e8%af%ad%e8%a8%80%e4%b8%ad%e7%9a%84-goroutine-%e6%8c%81%e7%bb%ad%e8%bf%90%e8%a1%8c%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/206697\/","title":{"rendered":"\u5982\u4f55\u4fdd\u8bc1 Go \u8bed\u8a00\u4e2d\u7684 Goroutine \u6301\u7eed\u8fd0\u884c\uff1f"},"content":{"rendered":"<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662fGOLANG\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4fdd\u8bc1 Go \u8bed\u8a00\u4e2d\u7684 Goroutine \u6301\u7eed\u8fd0\u884c\uff1f\u300b<\/span>\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241119\/1732018008673c7f5876ede.jpg\" class=\"aligncenter\">,<br \/>\n<strong>goroutine \u5982\u4f55\u786e\u4fdd\u6301\u7eed\u8fd0\u884c\uff1f<\/strong>,\u5728\u63d0\u4f9b\u7684\u4ee3\u7801\u4e2d\uff0cnewclientcodec() \u8fd4\u56de\u4e00\u4e2a client \u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u542f\u52a8\u4e00\u4e2a goroutine \u6765\u63a5\u53d7\u8bf7\u6c42\uff1ago client.receive()\u3002,\u8fd9\u4e2a\u95ee\u9898\u7684\u91cd\u70b9\u662f\uff0c\u4e3b\u7ebf\u7a0b\u6267\u884c\u5b8c\u540e\uff0cgoroutine \u5982\u4f55\u4fdd\u6301\u8fd0\u884c\u3002,\u7b54\u6848\u5f88\u7b80\u5355\uff0c\u53ea\u8981\u4e3b\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u6bd4 goroutine \u957f\u5373\u53ef\u3002\u8fd9\u91cc\u6709\u51e0\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a,<br \/>\n<strong>\u65b9\u6cd5 1\uff1a\u963b\u585e\u4e3b\u7ebf\u7a0b<\/strong>,\u4e3b\u7ebf\u7a0b\u53ef\u4ee5\u901a\u8fc7\u963b\u585e\u5728\u4fe1\u53f7\u6216\u5176\u4ed6\u8f93\u5165\u4e0a\uff0c\u4f8b\u5982\uff1a,<br \/>\n<strong>\u65b9\u6cd5 2\uff1a\u4f7f\u7528 sync.waitgroup<\/strong>,\u4e3b\u7ebf\u7a0b\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 sync.waitgroup\uff0c\u7b49\u5f85 goroutine \u5b8c\u6210\u5de5\u4f5c\u3002,<br \/>\n<strong>\u65b9\u6cd5 3\uff1a\u5229\u7528\u7f51\u7edc api \u963b\u585e<\/strong>,\u5982\u679c\u4e3b\u7ebf\u7a0b\u8c03\u7528\u4e86 listen \u7b49\u963b\u585e\u5f0f\u7684\u7f51\u7edc api\uff0c\u90a3\u4e48\u5b83\u5c06\u81ea\u7136\u5730\u4fdd\u6301\u8fd0\u884c\uff0c\u76f4\u5230 goroutine \u5b8c\u6210\u3002,<br \/>\n<strong>\u4f18\u96c5\u5730\u5173\u95ed<\/strong>,\u5bf9\u4e8e\u67d0\u4e9b\u60c5\u51b5\uff0c\u4f8b\u5982\u9700\u8981\u5728\u4e3b\u7ebf\u7a0b\u9000\u51fa\u524d\u4fdd\u5b58\u6570\u636e\uff0c\u4e3b\u7ebf\u7a0b\u5fc5\u987b\u4f18\u96c5\u5730\u5173\u95ed\u3002,\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4fe1\u53f7\u5904\u7406\u3001context \u6216 sync.waitgroup \u6765\u5141\u8bb8 goroutine \u5b8c\u6210\u5de5\u4f5c\uff0c\u7136\u540e\u4e3b\u7ebf\u7a0b\u624d\u80fd\u9000\u51fa\u3002,\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5982\u4f55\u4fdd\u8bc1 Go \u8bed\u8a00\u4e2d\u7684 Goroutine \u6301\u7eed\u8fd0\u884c\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\u5427\uff01,goroutine \u5982\u4f55\u786e\u4fdd\u6301\u7eed\u8fd0\u884c\uff1f,\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662fGOLANG\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4fdd\u8bc1 Go \u8bed\u8a00\u4e2d\u7684 Goroutine \u6301\u7eed\u8fd0\u884c\uff1f\u300b<\/span>\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01,<br \/>\n<b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u5982\u4f55\u4fdd\u8bc1 Go \u8bed\u8a00\u4e2d\u7684 Goroutine \u6301\u7eed\u8fd0\u884c\uff1f<\/span><\/p>\n<h1>\u5982\u4f55\u4fdd\u8bc1 Go \u8bed\u8a00\u4e2d\u7684 Goroutine \u6301\u7eed\u8fd0\u884c\uff1f<\/h1>\n<p><span>2024-11-19 20:07:04<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662fGOLANG\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4fdd\u8bc1 Go \u8bed\u8a00\u4e2d\u7684 Goroutine \u6301\u7eed\u8fd0\u884c\uff1f\u300b<\/span>\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241119\/1732018008673c7f5876ede.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>goroutine \u5982\u4f55\u786e\u4fdd\u6301\u7eed\u8fd0\u884c\uff1f<\/strong><\/p>\n<p>\u5728\u63d0\u4f9b\u7684\u4ee3\u7801\u4e2d\uff0cnewclientcodec() \u8fd4\u56de\u4e00\u4e2a client \u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u542f\u52a8\u4e00\u4e2a goroutine \u6765\u63a5\u53d7\u8bf7\u6c42\uff1ago client.receive()\u3002<\/p>\n<p>\u8fd9\u4e2a\u95ee\u9898\u7684\u91cd\u70b9\u662f\uff0c\u4e3b\u7ebf\u7a0b\u6267\u884c\u5b8c\u540e\uff0cgoroutine \u5982\u4f55\u4fdd\u6301\u8fd0\u884c\u3002<\/p>\n<p>\u7b54\u6848\u5f88\u7b80\u5355\uff0c\u53ea\u8981\u4e3b\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u6bd4 goroutine \u957f\u5373\u53ef\u3002\u8fd9\u91cc\u6709\u51e0\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<p><strong>\u65b9\u6cd5 1\uff1a\u963b\u585e\u4e3b\u7ebf\u7a0b<\/strong><\/p>\n<p>\u4e3b\u7ebf\u7a0b\u53ef\u4ee5\u901a\u8fc7\u963b\u585e\u5728\u4fe1\u53f7\u6216\u5176\u4ed6\u8f93\u5165\u4e0a\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>import (\n  \"os\"\n  \"os\/signal\"\n  \"syscall\"\n)\n\nfunc main() {\n  newClientCodec(...)\n  ch := make(chan os.Signal, 1)\n  signal.Notify(ch, syscall.SIGINT)\n  &lt;-ch\n}<\/pre>\n<p><strong>\u65b9\u6cd5 2\uff1a\u4f7f\u7528 sync.waitgroup<\/strong><\/p>\n<p>\u4e3b\u7ebf\u7a0b\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 sync.waitgroup\uff0c\u7b49\u5f85 goroutine \u5b8c\u6210\u5de5\u4f5c\u3002<\/p>\n<p><strong>\u65b9\u6cd5 3\uff1a\u5229\u7528\u7f51\u7edc api \u963b\u585e<\/strong><\/p>\n<p>\u5982\u679c\u4e3b\u7ebf\u7a0b\u8c03\u7528\u4e86 listen \u7b49\u963b\u585e\u5f0f\u7684\u7f51\u7edc api\uff0c\u90a3\u4e48\u5b83\u5c06\u81ea\u7136\u5730\u4fdd\u6301\u8fd0\u884c\uff0c\u76f4\u5230 goroutine \u5b8c\u6210\u3002<\/p>\n<p><strong>\u4f18\u96c5\u5730\u5173\u95ed<\/strong><\/p>\n<p>\u5bf9\u4e8e\u67d0\u4e9b\u60c5\u51b5\uff0c\u4f8b\u5982\u9700\u8981\u5728\u4e3b\u7ebf\u7a0b\u9000\u51fa\u524d\u4fdd\u5b58\u6570\u636e\uff0c\u4e3b\u7ebf\u7a0b\u5fc5\u987b\u4f18\u96c5\u5730\u5173\u95ed\u3002<\/p>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4fe1\u53f7\u5904\u7406\u3001context \u6216 sync.waitgroup \u6765\u5141\u8bb8 goroutine \u5b8c\u6210\u5de5\u4f5c\uff0c\u7136\u540e\u4e3b\u7ebf\u7a0b\u624d\u80fd\u9000\u51fa\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5982\u4f55\u4fdd\u8bc1 Go \u8bed\u8a00\u4e2d\u7684 Goroutine \u6301\u7eed\u8fd0\u884c\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u8bbe\u8ba1\u7ba1\u7406\u540e\u53f0\u9875\u9762\uff0c\u5e94\u8be5\u5982\u4f55\u5904\u7406\u8bbe\u8ba1\u56fe\u5c3a\u5bf8\u4e0e\u5b9e\u9645\u5c55\u793a\u5185\u5bb9\u7684\u5dee\u8ddd\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u6b63\u7248Win11\u6fc0\u6d3b\u7801\u600e\u4e48\u9886\u53d6 \u6b63\u7248Win11\u6fc0\u6d3b\u7801\u9886\u53d6\u6fc0\u6d3b\u65b9\u6cd5\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-206697","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206697","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=206697"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206697\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=206697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=206697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=206697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}