{"id":40633,"date":"2024-12-01T11:02:21","date_gmt":"2024-12-01T03:02:21","guid":{"rendered":"https:\/\/fwq.ai\/blog\/40633\/"},"modified":"2024-12-01T11:02:21","modified_gmt":"2024-12-01T03:02:21","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-golang-%e5%87%bd%e6%95%b0%e4%b8%ad%e4%bc%98%e9%9b%85%e5%9c%b0%e5%a4%84%e7%90%86%e5%b9%b6%e5%8f%91-goroutine%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/40633\/","title":{"rendered":"\u5982\u4f55\u5728 Golang \u51fd\u6570\u4e2d\u4f18\u96c5\u5730\u5904\u7406\u5e76\u53d1 goroutine\uff1f"},"content":{"rendered":"<p>\u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5728 Golang \u51fd\u6570\u4e2d\u4f18\u96c5\u5730\u5904\u7406\u5e76\u53d1 goroutine\uff1f\u300b<\/span>\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u6216\u8005\u662f\u5bf9<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\uff01,\u5728 Golang \u51fd\u6570\u4e2d\u5904\u7406\u5e76\u53d1 goroutine \u7684\u4f18\u96c5\u65b9\u6cd5\uff1a\u4f7f\u7528 sync.WaitGroups\uff1a\u901a\u8fc7\u6307\u5b9a goroutine \u6570\u91cf\u5e76\u7b49\u5f85\u6bcf\u4e2a goroutine \u5b8c\u6210\u6765\u5b9e\u73b0\u540c\u6b65\u3002\u4f7f\u7528\u901a\u9053\uff1a\u901a\u8fc7\u521b\u5efa\u901a\u9053\u5e76\u4f7f\u7528 goroutine \u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\uff0c\u5b9e\u73b0\u901a\u4fe1\u548c\u540c\u6b65\u3002\u4f7f\u7528\u4e0a\u4e0b\u6587\uff1a\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a\u5e26\u6709\u53d6\u6d88\u529f\u80fd\u7684\u4e0a\u4e0b\u6587\uff0c\u5b9e\u73b0\u53d6\u6d88\u6216\u8d85\u65f6 goroutine \u7684\u529f\u80fd\u3002,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1729991390671d92de4060f.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728 Golang \u51fd\u6570\u4e2d\u4f18\u96c5\u5730\u5904\u7406\u5e76\u53d1 goroutine\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728 Golang \u51fd\u6570\u4e2d\u4f18\u96c5\u5730\u5904\u7406\u5e76\u53d1 goroutine\uff1f\u63d2\u56fe\" \/>,<br \/>\n<strong>\u5982\u4f55\u5728 Golang \u51fd\u6570\u4e2d\u4f18\u96c5\u5730\u5904\u7406\u5e76\u53d1 goroutine<\/strong>,\u5728 Golang \u4e2d\uff0cgoroutine \u6307\u7684\u662f\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u5e76\u53d1\u6267\u884c\u4efb\u52a1\u3002\u7136\u800c\uff0c\u7ba1\u7406\u5e76\u53d1 goroutine \u53ef\u80fd\u662f\u4e00\u9879\u8270\u5de8\u7684\u4efb\u52a1\uff0c\u5982\u679c\u5904\u7406\u4e0d\u5f53\uff0c\u4f1a\u5bfc\u81f4\u6b7b\u9501\u3001\u6570\u636e\u7ade\u4e89\u6216\u5185\u5b58\u6cc4\u6f0f\u3002\u672c\u6587\u5c06\u8ba8\u8bba\u5982\u4f55\u5728 Go \u51fd\u6570\u4e2d\u4f18\u96c5\u5730\u5904\u7406\u5e76\u53d1 goroutine\uff0c\u5e76\u901a\u8fc7\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u52a0\u4ee5\u9610\u8ff0\u3002,<br \/>\n<strong>\u4f7f\u7528 <code>sync.WaitGroup<\/code><\/strong>,<br \/>\n<code>sync.WaitGroup<\/code> \u662f\u4e00\u79cd\u540c\u6b65\u673a\u5236\uff0c\u53ef\u7528\u4e8e\u7b49\u5f85\u4e00\u7ec4 goroutine \u5b8c\u6210\u3002\u901a\u8fc7\u8c03\u7528<br \/>\n<code>Add(n)<\/code> \u65b9\u6cd5\u6307\u5b9a\u7b49\u5f85\u7684 goroutine \u6570\u91cf\uff0c\u5e76\u5728\u6bcf\u4e2a goroutine \u5b8c\u6210\u540e\u8c03\u7528<br \/>\n<code>Done()<\/code> \u65b9\u6cd5\u3002\u5f53<br \/>\n<code>Wait()<\/code> \u65b9\u6cd5\u8fd4\u56de\u65f6\uff0c\u8868\u660e\u6240\u6709 goroutine \u90fd\u5df2\u5b8c\u6210\u3002,<br \/>\n<strong>\u4f7f\u7528\u901a\u9053<\/strong>,\u901a\u9053\u662f\u4e00\u79cd\u7528\u4e8e\u5728 goroutine \u4e4b\u95f4\u901a\u4fe1\u548c\u540c\u6b65\u7684\u673a\u5236\u3002\u53ef\u4ee5\u901a\u8fc7<br \/>\n<code>make(chan T)<\/code> \u521b\u5efa\u4e00\u4e2a\u901a\u9053\uff0c\u5176\u4e2d<br \/>\n<code>T<\/code> \u662f\u901a\u9053\u5143\u7d20\u7684\u7c7b\u578b\u3002goroutine \u53ef\u4ee5\u4f7f\u7528<br \/>\n<code>&lt;-ch<\/code> \u8bfb\u53d6\u901a\u9053\uff0c\u4f7f\u7528<br \/>\n<code>ch &lt;- x<\/code> \u53d1\u9001\u6570\u636e\u5230\u901a\u9053\u3002,<br \/>\n<strong>\u4f7f\u7528\u4e0a\u4e0b\u6587<\/strong>,\u4e0a\u4e0b\u6587\u88ab\u7528\u4e8e\u5728 goroutine \u4e4b\u95f4\u4f20\u9012\u53d6\u6d88\u6216\u8d85\u65f6\u4fe1\u606f\u3002\u53ef\u4ee5\u4f7f\u7528<br \/>\n<code>context.Background()<\/code> \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4e0a\u4e0b\u6587\u5e76\u4f7f\u7528<br \/>\n<code>context.WithCancel()<\/code> \u521b\u5efa\u4e00\u4e2a\u5e26\u6709\u53d6\u6d88\u529f\u80fd\u7684\u6d3e\u751f\u4e0a\u4e0b\u6587\u3002\u8981\u53d6\u6d88\u4e0a\u4e0b\u6587\uff0c\u53ef\u4ee5\u8c03\u7528<br \/>\n<code>CancelFunc()<\/code> \u65b9\u6cd5\u3002,\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8golang\u5b66\u4e60\u7f51\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~,\u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5728 Golang \u51fd\u6570\u4e2d\u4f18\u96c5\u5730\u5904\u7406\u5e76\u53d1 goroutine\uff1f\u300b<\/span>\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u6216\u8005\u662f\u5bf9<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\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\u5728 Golang \u51fd\u6570\u4e2d\u4f18\u96c5\u5730\u5904\u7406\u5e76\u53d1 goroutine\uff1f<\/span><\/p>\n<h1>\u5982\u4f55\u5728 Golang \u51fd\u6570\u4e2d\u4f18\u96c5\u5730\u5904\u7406\u5e76\u53d1 goroutine\uff1f<\/h1>\n<p><span>2024-10-27 09:10:05<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5728 Golang \u51fd\u6570\u4e2d\u4f18\u96c5\u5730\u5904\u7406\u5e76\u53d1 goroutine\uff1f\u300b<\/span>\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u6216\u8005\u662f\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\uff01<\/p>\n<p>\u5728 Golang \u51fd\u6570\u4e2d\u5904\u7406\u5e76\u53d1 goroutine \u7684\u4f18\u96c5\u65b9\u6cd5\uff1a\u4f7f\u7528 sync.WaitGroups\uff1a\u901a\u8fc7\u6307\u5b9a goroutine \u6570\u91cf\u5e76\u7b49\u5f85\u6bcf\u4e2a goroutine \u5b8c\u6210\u6765\u5b9e\u73b0\u540c\u6b65\u3002\u4f7f\u7528\u901a\u9053\uff1a\u901a\u8fc7\u521b\u5efa\u901a\u9053\u5e76\u4f7f\u7528 goroutine \u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\uff0c\u5b9e\u73b0\u901a\u4fe1\u548c\u540c\u6b65\u3002\u4f7f\u7528\u4e0a\u4e0b\u6587\uff1a\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a\u5e26\u6709\u53d6\u6d88\u529f\u80fd\u7684\u4e0a\u4e0b\u6587\uff0c\u5b9e\u73b0\u53d6\u6d88\u6216\u8d85\u65f6 goroutine \u7684\u529f\u80fd\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1729991390671d92de4060f.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728 Golang \u51fd\u6570\u4e2d\u4f18\u96c5\u5730\u5904\u7406\u5e76\u53d1 goroutine\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728 Golang \u51fd\u6570\u4e2d\u4f18\u96c5\u5730\u5904\u7406\u5e76\u53d1 goroutine\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 Golang \u51fd\u6570\u4e2d\u4f18\u96c5\u5730\u5904\u7406\u5e76\u53d1 goroutine<\/strong><\/p>\n<p>\u5728 Golang \u4e2d\uff0cgoroutine \u6307\u7684\u662f\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u5e76\u53d1\u6267\u884c\u4efb\u52a1\u3002\u7136\u800c\uff0c\u7ba1\u7406\u5e76\u53d1 goroutine \u53ef\u80fd\u662f\u4e00\u9879\u8270\u5de8\u7684\u4efb\u52a1\uff0c\u5982\u679c\u5904\u7406\u4e0d\u5f53\uff0c\u4f1a\u5bfc\u81f4\u6b7b\u9501\u3001\u6570\u636e\u7ade\u4e89\u6216\u5185\u5b58\u6cc4\u6f0f\u3002\u672c\u6587\u5c06\u8ba8\u8bba\u5982\u4f55\u5728 Go \u51fd\u6570\u4e2d\u4f18\u96c5\u5730\u5904\u7406\u5e76\u53d1 goroutine\uff0c\u5e76\u901a\u8fc7\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u52a0\u4ee5\u9610\u8ff0\u3002<\/p>\n<p><strong>\u4f7f\u7528 <code>sync.WaitGroup<\/code><\/strong><\/p>\n<p><code>sync.WaitGroup<\/code> \u662f\u4e00\u79cd\u540c\u6b65\u673a\u5236\uff0c\u53ef\u7528\u4e8e\u7b49\u5f85\u4e00\u7ec4 goroutine \u5b8c\u6210\u3002\u901a\u8fc7\u8c03\u7528 <code>Add(n)<\/code> \u65b9\u6cd5\u6307\u5b9a\u7b49\u5f85\u7684 goroutine \u6570\u91cf\uff0c\u5e76\u5728\u6bcf\u4e2a goroutine \u5b8c\u6210\u540e\u8c03\u7528 <code>Done()<\/code> \u65b9\u6cd5\u3002\u5f53 <code>Wait()<\/code> \u65b9\u6cd5\u8fd4\u56de\u65f6\uff0c\u8868\u660e\u6240\u6709 goroutine \u90fd\u5df2\u5b8c\u6210\u3002<\/p>\n<pre>import (\n    \"sync\"\n    \"fmt\"\n)\n\nfunc main() {\n    var wg sync.WaitGroup\n    wg.Add(2)\n\n    go func() {\n        \/\/ \u6a21\u62df\u5f02\u6b65\u4efb\u52a1\n        fmt.Println(\"Goroutine 1 \u5b8c\u6210\")\n        wg.Done()\n    }()\n\n    go func() {\n        \/\/ \u6a21\u62df\u5f02\u6b65\u4efb\u52a1\n        fmt.Println(\"Goroutine 2 \u5b8c\u6210\")\n        wg.Done()\n    }()\n\n    wg.Wait()\n    fmt.Println(\"\u6240\u6709 goroutine \u5b8c\u6210\")\n}<\/pre>\n<p><strong>\u4f7f\u7528\u901a\u9053<\/strong><\/p>\n<p>\u901a\u9053\u662f\u4e00\u79cd\u7528\u4e8e\u5728 goroutine \u4e4b\u95f4\u901a\u4fe1\u548c\u540c\u6b65\u7684\u673a\u5236\u3002\u53ef\u4ee5\u901a\u8fc7 <code>make(chan T)<\/code> \u521b\u5efa\u4e00\u4e2a\u901a\u9053\uff0c\u5176\u4e2d <code>T<\/code> \u662f\u901a\u9053\u5143\u7d20\u7684\u7c7b\u578b\u3002goroutine \u53ef\u4ee5\u4f7f\u7528 <code>&lt;-ch<\/code> \u8bfb\u53d6\u901a\u9053\uff0c\u4f7f\u7528 <code>ch &lt;- x<\/code> \u53d1\u9001\u6570\u636e\u5230\u901a\u9053\u3002<\/p>\n<pre>import (\n    \"fmt\"\n)\n\nfunc main() {\n    ch := make(chan string)\n\n    go func() {\n        \/\/ \u6a21\u62df\u5f02\u6b65\u4efb\u52a1\n        ch &lt;- \"Goroutine 1 \u5b8c\u6210\"\n    }()\n\n    go func() {\n        \/\/ \u6a21\u62df\u5f02\u6b65\u4efb\u52a1\n        ch &lt;- \"Goroutine 2 \u5b8c\u6210\"\n    }()\n\n    for i := 0; i &lt; 2; i++ {\n        fmt.Println(&lt;-ch)\n    }\n}<\/pre>\n<p><strong>\u4f7f\u7528\u4e0a\u4e0b\u6587<\/strong><\/p>\n<p>\u4e0a\u4e0b\u6587\u88ab\u7528\u4e8e\u5728 goroutine \u4e4b\u95f4\u4f20\u9012\u53d6\u6d88\u6216\u8d85\u65f6\u4fe1\u606f\u3002\u53ef\u4ee5\u4f7f\u7528 <code>context.Background()<\/code> \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4e0a\u4e0b\u6587\u5e76\u4f7f\u7528 <code>context.WithCancel()<\/code> \u521b\u5efa\u4e00\u4e2a\u5e26\u6709\u53d6\u6d88\u529f\u80fd\u7684\u6d3e\u751f\u4e0a\u4e0b\u6587\u3002\u8981\u53d6\u6d88\u4e0a\u4e0b\u6587\uff0c\u53ef\u4ee5\u8c03\u7528 <code>CancelFunc()<\/code> \u65b9\u6cd5\u3002<\/p>\n<pre>import (\n    \"context\"\n    \"fmt\"\n    \"time\"\n)\n\nfunc main() {\n    ctx, cancel := context.WithCancel(context.Background())\n\n    go func() {\n        for {\n            select {\n            case &lt;-ctx.Done():\n                \/\/ \u4e0a\u4e0b\u6587\u5df2\u53d6\u6d88\uff0c\u505c\u6b62 goroutine\n                fmt.Println(\"Goroutine \u5df2\u53d6\u6d88\")\n                return\n            default:\n                \/\/ \u7ee7\u7eed\u6267\u884c goroutine\n                fmt.Println(\"Goroutine \u6b63\u5728\u8fd0\u884c\")\n                time.Sleep(time.Second)\n            }\n        }\n    }()\n\n    time.Sleep(5 * time.Second)\n    cancel()\n}<\/pre>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8golang\u5b66\u4e60\u7f51\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5982\u4f55\u81ea\u52a8\u5316PHP\u51fd\u6570\u6d4b\u8bd5\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Java\u51fd\u6570\u91cd\u8f7d\u5728\u7ee7\u627f\u4e2d\u7684\u5e94\u7528\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765 \u300a\u5982\u4f55\u5728 Golang \u51fd\u6570\u4e2d\u4f18\u96c5\u5730\u5904\u7406\u5e76\u53d1 goroutine\uff1f\u300b\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230 \u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60 Golang\uff0c\u6216\u8005\u662f\u5bf9 Golang\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\uff01,\u5728 Golang \u51fd\u6570\u4e2d\u5904\u7406\u5e76\u53d1 goroutine \u7684\u4f18\u96c5\u65b9\u6cd5\uff1a\u4f7f\u7528 sync.WaitGroups\uff1a\u901a\u8fc7\u6307\u5b9a goroutine \u6570\u91cf\u5e76\u7b49\u5f85\u6bcf\u4e2a goroutine \u5b8c\u6210\u6765\u5b9e\u73b0\u540c\u6b65\u3002\u4f7f\u7528\u901a\u9053\uff1a\u901a\u8fc7\u521b\u5efa\u901a\u9053\u5e76\u4f7f\u7528 goroutine \u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\uff0c\u5b9e\u73b0\u901a\u4fe1\u548c\u540c\u6b65\u3002\u4f7f\u7528\u4e0a\u4e0b\u6587\uff1a\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a\u5e26\u6709\u53d6\u6d88\u529f\u80fd\u7684\u4e0a\u4e0b\u6587\uff0c\u5b9e\u73b0\u53d6\u6d88\u6216\u8d85\u65f6 goroutine \u7684\u529f\u80fd\u3002, , \u5982\u4f55\u5728 Golang \u51fd\u6570\u4e2d\u4f18\u96c5\u5730\u5904\u7406\u5e76\u53d1 goroutine,\u5728 Golang \u4e2d\uff0cgoroutine \u6307\u7684\u662f\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u5e76\u53d1\u6267\u884c\u4efb\u52a1\u3002\u7136\u800c\uff0c\u7ba1\u7406\u5e76\u53d1 goroutine \u53ef\u80fd\u662f\u4e00\u9879\u8270\u5de8\u7684\u4efb\u52a1\uff0c\u5982\u679c\u5904\u7406\u4e0d\u5f53\uff0c\u4f1a\u5bfc\u81f4\u6b7b\u9501\u3001\u6570\u636e\u7ade\u4e89\u6216\u5185\u5b58\u6cc4\u6f0f\u3002\u672c\u6587\u5c06\u8ba8\u8bba\u5982\u4f55\u5728 Go \u51fd\u6570\u4e2d\u4f18\u96c5\u5730\u5904\u7406\u5e76\u53d1 goroutine\uff0c\u5e76\u901a\u8fc7\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u52a0\u4ee5\u9610\u8ff0\u3002, \u4f7f\u7528 sync.WaitGroup, sync.WaitGroup \u662f\u4e00\u79cd\u540c\u6b65\u673a\u5236\uff0c\u53ef\u7528\u4e8e\u7b49\u5f85\u4e00\u7ec4 goroutine \u5b8c\u6210\u3002\u901a\u8fc7\u8c03\u7528 Add(n) \u65b9\u6cd5\u6307\u5b9a\u7b49\u5f85\u7684 goroutine \u6570\u91cf\uff0c\u5e76\u5728\u6bcf\u4e2a goroutine \u5b8c\u6210\u540e\u8c03\u7528 Done() \u65b9\u6cd5\u3002\u5f53 Wait() \u65b9\u6cd5\u8fd4\u56de\u65f6\uff0c\u8868\u660e\u6240\u6709 goroutine \u90fd\u5df2\u5b8c\u6210\u3002, \u4f7f\u7528\u901a\u9053,\u901a\u9053\u662f\u4e00\u79cd\u7528\u4e8e\u5728 goroutine \u4e4b\u95f4\u901a\u4fe1\u548c\u540c\u6b65\u7684\u673a\u5236\u3002\u53ef\u4ee5\u901a\u8fc7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-40633","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=40633"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40633\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=40633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=40633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=40633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}