{"id":1441,"date":"2025-02-08T23:51:17","date_gmt":"2025-02-08T15:51:17","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1441"},"modified":"2025-02-08T23:51:18","modified_gmt":"2025-02-08T15:51:18","slug":"go%e8%af%ad%e8%a8%80%e5%b9%b6%e5%8f%91%e4%b8%8e%e5%b9%b6%e8%a1%8c%e7%bc%96%e7%a8%8b%ef%bc%9a%e6%8e%8c%e6%8f%a1goroutine%e4%b8%8echannel%e7%9a%84%e9%ab%98%e6%95%88%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/08\/go%e8%af%ad%e8%a8%80%e5%b9%b6%e5%8f%91%e4%b8%8e%e5%b9%b6%e8%a1%8c%e7%bc%96%e7%a8%8b%ef%bc%9a%e6%8e%8c%e6%8f%a1goroutine%e4%b8%8echannel%e7%9a%84%e9%ab%98%e6%95%88%e4%bd%bf%e7%94%a8\/","title":{"rendered":"Go\u8bed\u8a00\u5e76\u53d1\u4e0e\u5e76\u884c\u7f16\u7a0b\uff1a\u638c\u63e1goroutine\u4e0echannel\u7684\u9ad8\u6548\u4f7f\u7528"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u4ee5\u5176\u5f3a\u5927\u7684\u5e76\u53d1\u652f\u6301\u800c\u95fb\u540d\uff0c\u5b83\u901a\u8fc7<strong>goroutines<\/strong>\u548c<strong>channels<\/strong>\u63d0\u4f9b\u4e86\u8f7b\u91cf\u7ea7\u7684\u5e76\u53d1\u7f16\u7a0b\u6a21\u578b\uff0c\u8fd9\u4f7f\u5f97\u5e76\u53d1\u64cd\u4f5c\u6bd4\u4f20\u7edf\u7ebf\u7a0b\u6c60\u66f4\u52a0\u7b80\u6d01\u548c\u9ad8\u6548\u3002\u672c\u7ae0\u5c06\u6df1\u5165\u63a2\u8ba8Go\u8bed\u8a00\u4e2d\u7684\u5e76\u53d1\u4e0e\u5e76\u884c\u6982\u5ff5\uff0c\u5305\u62ecgoroutine\u7684\u57fa\u672c\u4f7f\u7528\u3001Channel\u7684\u540c\u6b65\u673a\u5236\u3001select\u8bed\u53e5\u3001\u591a\u8def\u590d\u7528\u4ee5\u53ca\u9ad8\u6548\u7684\u5e76\u53d1\u8bbe\u8ba1\u6a21\u5f0f\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6.1 <strong>goroutine\u7684\u57fa\u672c\u4f7f\u7528<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">6.1.1 <strong>\u4ec0\u4e48\u662fgoroutine\uff1f<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728Go\u8bed\u8a00\u4e2d\uff0c<strong>goroutine<\/strong>\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u6267\u884c\u5355\u5143\uff0c\u5b83\u7c7b\u4f3c\u4e8e\u7ebf\u7a0b\uff0c\u4f46\u6bd4\u7ebf\u7a0b\u66f4\u52a0\u8f7b\u4fbf\u548c\u9ad8\u6548\u3002Go\u7684\u8c03\u5ea6\u5668\u4f1a\u81ea\u52a8\u7ba1\u7406goroutine\u7684\u521b\u5efa\u548c\u9500\u6bc1\uff0c\u5f00\u53d1\u8005\u65e0\u9700\u5173\u6ce8\u4f4e\u7ea7\u522b\u7684\u7ebf\u7a0b\u7ba1\u7406\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u4e2agoroutine\u901a\u8fc7\u5728\u51fd\u6570\u8c03\u7528\u524d\u52a0\u4e0a<code>go<\/code>\u5173\u952e\u5b57\u6765\u542f\u52a8\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a\u542f\u52a8\u4e00\u4e2agoroutine<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"time\"\n)\n\nfunc printMessage() {\n    fmt.Println(\"Hello from goroutine!\")\n}\n\nfunc main() {\n    go printMessage()  \/\/ \u542f\u52a8\u4e00\u4e2a\u65b0\u7684goroutine\n    time.Sleep(time.Second) \/\/ \u7b49\u5f85goroutine\u5b8c\u6210\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>go printMessage()<\/code>\u542f\u52a8\u4e86\u4e00\u4e2a\u65b0\u7684goroutine\uff0c\u800c<code>time.Sleep<\/code>\u7528\u4e8e\u8ba9\u4e3b\u7ebf\u7a0b\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\uff0c\u786e\u4fddgoroutine\u6709\u8db3\u591f\u7684\u65f6\u95f4\u6267\u884c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">6.1.2 <strong>goroutine\u7684\u7279\u70b9<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f7b\u91cf\u7ea7<\/strong>\uff1agoroutine\u7684\u5185\u5b58\u5f00\u9500\u975e\u5e38\u5c0f\uff08\u901a\u5e38\u53ea\u9700\u8981\u51e0KB\u5185\u5b58\uff09\uff0c\u53ef\u4ee5\u540c\u65f6\u542f\u52a8\u6210\u5343\u4e0a\u4e07\u4e2agoroutine\u3002<\/li>\n\n\n\n<li><strong>\u5e76\u53d1\u8c03\u5ea6<\/strong>\uff1aGo\u7684\u8c03\u5ea6\u5668\u4f1a\u5c06goroutine\u8c03\u5ea6\u5230\u64cd\u4f5c\u7cfb\u7edf\u7ebf\u7a0b\u4e0a\u6267\u884c\uff0c\u652f\u6301\u591a\u6838\u5e76\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6.2 <strong>Channel\u7684\u540c\u6b65\u673a\u5236<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">6.2.1 <strong>\u4ec0\u4e48\u662fChannel\uff1f<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Channel<\/strong>\u662fGo\u8bed\u8a00\u7528\u4e8e\u5728\u591a\u4e2agoroutine\u4e4b\u95f4\u8fdb\u884c\u901a\u4fe1\u7684\u7ba1\u9053\u3002\u901a\u8fc7channel\uff0c\u6570\u636e\u53ef\u4ee5\u4ece\u4e00\u4e2agoroutine\u4f20\u9012\u5230\u53e6\u4e00\u4e2agoroutine\uff0c\u5e76\u4e14Go\u7684\u8c03\u5ea6\u5668\u4f1a\u786e\u4fdd\u6570\u636e\u4f20\u9012\u7684\u540c\u6b65\u3002Channel\u7684\u4f7f\u7528\u4f7f\u5f97\u5e76\u53d1\u7f16\u7a0b\u4e2d\u7684\u540c\u6b65\u53d8\u5f97\u66f4\u52a0\u76f4\u89c2\u548c\u7b80\u6d01\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a\u4f7f\u7528Channel\u8fdb\u884c\u901a\u4fe1<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n)\n\nfunc sendData(ch chan string) {\n    ch &lt;- \"Hello from goroutine!\" \/\/ \u5c06\u6570\u636e\u53d1\u9001\u5230channel\n}\n\nfunc main() {\n    ch := make(chan string) \/\/ \u521b\u5efa\u4e00\u4e2achannel\n    go sendData(ch) \/\/ \u542f\u52a8goroutine\n    message := &lt;-ch \/\/ \u4ecechannel\u63a5\u6536\u6570\u636e\n    fmt.Println(message) \/\/ \u8f93\u51fa\u7ed3\u679c\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">6.2.2 <strong>Channel\u7684\u5173\u95ed<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Channel\u5728\u5b8c\u6210\u6570\u636e\u4f20\u9012\u540e\u53ef\u4ee5\u5173\u95ed\u3002\u5173\u95ed\u7684channel\u4e0d\u4f1a\u518d\u63a5\u6536\u65b0\u7684\u6570\u636e\u3002\u5173\u95edchannel\u53ef\u4ee5\u9632\u6b62\u6570\u636e\u4e22\u5931\uff0c\u5e76\u4e14\u80fd\u591f\u901a\u77e5\u63a5\u6536\u65b9\u6570\u636e\u5df2\u7ed3\u675f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a\u5173\u95edChannel<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n)\n\nfunc sendData(ch chan string) {\n    ch &lt;- \"Hello from goroutine!\"\n    close(ch) \/\/ \u5173\u95edchannel\n}\n\nfunc main() {\n    ch := make(chan string)\n    go sendData(ch)\n\n    msg, ok := &lt;-ch\n    if ok {\n        fmt.Println(\"Received:\", msg)\n    } else {\n        fmt.Println(\"Channel is closed\")\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>close(ch)<\/code>\u5173\u95ed\u4e86channel\uff0c\u5f53\u63a5\u6536\u7aef\u5c1d\u8bd5\u8bfb\u53d6channel\u65f6\uff0c\u4f1a\u53d1\u73b0\u5b83\u5df2\u5173\u95ed\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u7b2c\u4e8c\u4e2a\u8fd4\u56de\u503c\uff08<code>ok<\/code>\uff09\u5224\u65ad\u662f\u5426\u6210\u529f\u63a5\u6536\u5230\u6570\u636e\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">6.2.3 <strong>Buffered Channels\uff08\u7f13\u51b2Channel\uff09<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Channel\u53ef\u4ee5\u662f\u7f13\u51b2\u7684\uff0c\u5373\u5b83\u53ef\u4ee5\u5b58\u50a8\u4e00\u5b9a\u6570\u91cf\u7684\u6570\u636e\u3002\u5f53channel\u7f13\u51b2\u533a\u6ee1\u65f6\uff0c\u53d1\u9001\u6570\u636e\u7684goroutine\u4f1a\u963b\u585e\uff0c\u76f4\u5230\u6709\u7a7a\u95f4\u53ef\u4ee5\u5199\u5165\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a\u7f13\u51b2Channel<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n)\n\nfunc main() {\n    ch := make(chan string, 2) \/\/ \u521b\u5efa\u4e00\u4e2a\u7f13\u51b2\u533a\u5927\u5c0f\u4e3a2\u7684channel\n    ch &lt;- \"Message 1\"\n    ch &lt;- \"Message 2\"\n\n    fmt.Println(&lt;-ch) \/\/ \u8f93\u51fa: Message 1\n    fmt.Println(&lt;-ch) \/\/ \u8f93\u51fa: Message 2\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7f13\u51b2Channel\u901a\u5e38\u7528\u4e8e\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u5f0f\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6.3 <strong>select\u548c\u591a\u8def\u590d\u7528<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">6.3.1 <strong>select\u8bed\u53e5<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u7684<code>select<\/code>\u8bed\u53e5\u5141\u8bb8goroutine\u540c\u65f6\u7b49\u5f85\u591a\u4e2achannel\u64cd\u4f5c\uff0c\u5e76\u4e14\u4f1a\u963b\u585e\u76f4\u5230\u5176\u4e2d\u4e00\u4e2achannel\u51c6\u5907\u597d\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u3002<code>select<\/code>\u8bed\u53e5\u662fGo\u5b9e\u73b0\u591a\u8def\u590d\u7528\u7684\u6838\u5fc3\u5de5\u5177\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1aselect\u8bed\u53e5<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"time\"\n)\n\nfunc sendData(ch chan string, msg string) {\n    time.Sleep(time.Second)\n    ch &lt;- msg\n}\n\nfunc main() {\n    ch1 := make(chan string)\n    ch2 := make(chan string)\n\n    go sendData(ch1, \"Message from channel 1\")\n    go sendData(ch2, \"Message from channel 2\")\n\n    \/\/ \u4f7f\u7528select\u8bed\u53e5\u7b49\u5f85\u63a5\u6536\u6d88\u606f\n    select {\n    case msg1 := &lt;-ch1:\n        fmt.Println(\"Received from ch1:\", msg1)\n    case msg2 := &lt;-ch2:\n        fmt.Println(\"Received from ch2:\", msg2)\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>select<\/code>\u7b49\u5f85\u4e24\u4e2achannel\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u6d88\u606f\uff0c\u54ea\u4e2a\u5148\u5230\u8fbe\u54ea\u4e2acase\u4f1a\u6267\u884c\u3002<code>select<\/code>\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5904\u7406\u591a\u4e2agoroutine\u4e4b\u95f4\u7684\u5e76\u53d1\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">6.3.2 <strong>default\u8bed\u53e5<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>select<\/code>\u8bed\u53e5\u8fd8\u652f\u6301<code>default<\/code>\u5206\u652f\uff0c\u5b83\u5728\u6ca1\u6709channel\u53ef\u64cd\u4f5c\u65f6\u7acb\u5373\u6267\u884c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a\u4f7f\u7528default\u907f\u514d\u963b\u585e<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n)\n\nfunc main() {\n    ch := make(chan string)\n\n    select {\n    case msg := &lt;-ch:\n        fmt.Println(\"Received:\", msg)\n    default:\n        fmt.Println(\"No message received\")\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53channel\u6ca1\u6709\u51c6\u5907\u597d\u65f6\uff0c<code>select<\/code>\u5c06\u6267\u884c<code>default<\/code>\u5206\u652f\uff0c\u4ece\u800c\u907f\u514d\u4e86\u963b\u585e\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6.4 <strong>Go\u7684\u5e76\u53d1\u6a21\u578b\u4e0e\u8c03\u5ea6\u5668<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u7684\u5e76\u53d1\u6a21\u578b\u57fa\u4e8e<strong>goroutine<\/strong>\u548c<strong>channel<\/strong>\uff0c\u5b83\u901a\u8fc7\u72ec\u7acb\u7684<strong>\u8c03\u5ea6\u5668<\/strong>\u6765\u7ba1\u7406goroutine\u7684\u751f\u547d\u5468\u671f\u3002Go\u8bed\u8a00\u4f7f\u7528\u7684\u662f<strong>M:N<\/strong>\u8c03\u5ea6\u6a21\u578b\uff0c\u5373\u591a\u4e2agoroutine\u4f1a\u88ab\u8c03\u5ea6\u5230\u8f83\u5c11\u6570\u91cf\u7684\u64cd\u4f5c\u7cfb\u7edf\u7ebf\u7a0b\u4e0a\u3002Go\u7684\u8c03\u5ea6\u5668\u4f1a\u5c06goroutines\u9ad8\u6548\u5730\u5206\u914d\u5230\u591a\u4e2aCPU\u6838\u5fc3\uff0c\u5145\u5206\u5229\u7528\u5e76\u884c\u8ba1\u7b97\u80fd\u529b\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8c03\u5ea6\u5668<\/strong>\u4f1a\u901a\u8fc7\u5185\u5b58\u4e2d\u8c03\u5ea6\u8868\u6765\u7ba1\u7406goroutine\u7684\u6267\u884c\u3002<\/li>\n\n\n\n<li><strong>goroutine<\/strong>\u7684\u8c03\u5ea6\u662f\u7531Go\u7684\u8fd0\u884c\u65f6\uff08runtime\uff09\u81ea\u52a8\u5904\u7406\u7684\uff0c\u5f00\u53d1\u8005\u65e0\u9700\u624b\u52a8\u7ba1\u7406\u3002<\/li>\n\n\n\n<li><strong>\u5de5\u4f5c\u7a83\u53d6\u7b97\u6cd5<\/strong>\uff08work stealing\uff09\u5141\u8bb8Go\u8c03\u5ea6\u5668\u5c06\u5de5\u4f5c\u4ece\u7e41\u5fd9\u7684CPU\u6838\u5fc3\u79fb\u52a8\u5230\u7a7a\u95f2\u7684CPU\u6838\u5fc3\uff0c\u4ece\u800c\u4fdd\u6301\u8d1f\u8f7d\u5747\u8861\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u7684\u8c03\u5ea6\u5668\u4f7f\u5f97goroutines\u5728\u591a\u4e2a\u6838\u5fc3\u4e0a\u5e76\u884c\u6267\u884c\u65f6\u65e2\u9ad8\u6548\u53c8\u7a33\u5b9a\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6.5 <strong>\u9ad8\u6548\u7684\u5e76\u53d1\u8bbe\u8ba1\u6a21\u5f0f<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">6.5.1 <strong>\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u5f0f<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728Go\u4e2d\uff0c\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u5f0f\u901a\u5e38\u901a\u8fc7goroutine\u548cchannel\u6765\u5b9e\u73b0\u3002\u751f\u4ea7\u8005\u4e0d\u65ad\u751f\u6210\u6570\u636e\u5e76\u53d1\u9001\u5230channel\uff0c\u6d88\u8d39\u8005\u4ecechannel\u63a5\u6536\u6570\u636e\u5e76\u5904\u7406\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u5f0f<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"time\"\n)\n\nfunc producer(ch chan string) {\n    for i := 0; i &lt; 5; i++ {\n        msg := fmt.Sprintf(\"Message %d\", i)\n        ch &lt;- msg\n        time.Sleep(time.Millisecond * 100)\n    }\n    close(ch)\n}\n\nfunc consumer(ch chan string) {\n    for msg := range ch {\n        fmt.Println(\"Consumed:\", msg)\n    }\n}\n\nfunc main() {\n    ch := make(chan string)\n    go producer(ch)\n    go consumer(ch)\n    time.Sleep(time.Second * 1)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u751f\u4ea7\u8005\u4f1a\u53d1\u9001\u4e94\u6761\u6d88\u606f\u5230channel\uff0c\u800c\u6d88\u8d39\u8005\u4ecechannel\u4e2d\u83b7\u53d6\u5e76\u5904\u7406\u8fd9\u4e9b\u6d88\u606f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">6.5.2 <strong>\u5de5\u4f5c\u6c60\u6a21\u5f0f<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5de5\u4f5c\u6c60\u6a21\u5f0f\u7528\u4e8e\u7ba1\u7406\u56fa\u5b9a\u6570\u91cf\u7684goroutines\uff0c\u901a\u5e38\u7528\u4e8e\u9ad8\u5e76\u53d1\u573a\u666f\u3002\u4efb\u52a1\u88ab\u5206\u53d1\u5230goroutines\u8fdb\u884c\u5e76\u53d1\u5904\u7406\uff0c\u907f\u514d\u4e86\u56e0\u8fc7\u591a\u7684goroutines\u800c\u5bfc\u81f4\u7684\u8d44\u6e90\u6d6a\u8d39\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a\u5de5\u4f5c\u6c60\u6a21\u5f0f<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\nfunc worker(id int, wg *sync.WaitGroup) {\n    defer wg.Done()\n    fmt.Printf(\"Worker %d started\\n\", id)\n}\n\nfunc main() {\n    var wg sync.WaitGroup\n    poolSize := 3\n    for i := 1; i &lt;= poolSize; i++ {\n        wg.Add(1)\n        go worker(i, &amp;wg)\n    }\n    wg.Wait()  \/\/ \u7b49\u5f85\u6240\u6709\u5de5\u4f5c\u5b8c\u6210\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4e09\u4e2a\u5de5\u4f5cgoroutine\u88ab\u521b\u5efa\u5e76\u5904\u7406\u4efb\u52a1\uff0c<code>sync.WaitGroup<\/code>\u786e\u4fdd\u6240\u6709\u5de5\u4f5c\u5b8c\u6210\u540e\u4e3b\u7ebf\u7a0b\u624d\u9000\u51fa\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u7684\u5e76\u53d1\u6a21\u578b\u901a\u8fc7goroutine\u3001channel\u548cselect\u8bed\u53e5\u63d0\u4f9b\u4e86\u9ad8\u6548\u800c\u7b80\u6d01\u7684\u5e76\u53d1\u7f16\u7a0b\u5de5\u5177\u3002\u5b83\u4f7f\u5f97\u5f00\u53d1\u8005\u80fd\u591f\u8f7b\u677e\u5b9e\u73b0\u9ad8\u5e76\u53d1\u548c\u5e76\u884c\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u901a\u8fc7\u4f7f\u7528Go\u7684\u5e76\u53d1\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5982\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u548c\u5de5\u4f5c\u6c60\u6a21\u5f0f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u5347\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u53ef\u6269\u5c55\u6027\u3002\u638c\u63e1Go\u8bed\u8a00\u7684\u5e76\u53d1\u4e0e\u5e76\u884c\u673a\u5236\u662f\u5f00\u53d1\u9ad8\u6548\u3001\u591a\u4efb\u52a1\u7cfb\u7edf\u7684\u5173\u952e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go\u8bed\u8a00\u4ee5\u5176\u5f3a\u5927\u7684\u5e76\u53d1\u652f\u6301\u800c\u95fb\u540d\uff0c\u5b83\u901a\u8fc7goroutines\u548cchannels\u63d0\u4f9b\u4e86\u8f7b\u91cf\u7ea7\u7684\u5e76\u53d1\u7f16\u7a0b\u6a21\u578b\uff0c\u8fd9\u4f7f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[],"class_list":["post-1441","post","type-post","status-publish","format-standard","hentry","category-golang"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1441"}],"version-history":[{"count":2,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1441\/revisions"}],"predecessor-version":[{"id":1458,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1441\/revisions\/1458"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}