{"id":205977,"date":"2025-06-12T10:42:26","date_gmt":"2025-06-12T02:42:26","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205977\/"},"modified":"2025-06-12T10:42:26","modified_gmt":"2025-06-12T02:42:26","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-go-%e8%af%ad%e8%a8%80%e9%99%90%e5%88%b6%e5%b9%b6%e5%8f%91%e6%89%a7%e8%a1%8c%e7%9a%84%e4%bb%bb%e5%8a%a1%e6%95%b0%e9%87%8f%ef%bc%8c%e5%b9%b6%e4%bf%9d%e8%af%81","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205977\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Go \u8bed\u8a00\u9650\u5236\u5e76\u53d1\u6267\u884c\u7684\u4efb\u52a1\u6570\u91cf\uff0c\u5e76\u4fdd\u8bc1\u6bcf\u6b21\u6700\u591a\u53ea\u6267\u884c 40 \u4e2a\u4efb\u52a1\uff1f"},"content":{"rendered":"<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528 Go \u8bed\u8a00\u9650\u5236\u5e76\u53d1\u6267\u884c\u7684\u4efb\u52a1\u6570\u91cf\uff0c\u5e76\u4fdd\u8bc1\u6bcf\u6b21\u6700\u591a\u53ea\u6267\u884c 40 \u4e2a\u4efb\u52a1\uff1f\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241030\/17302712876721d8376785c.jpg\" class=\"aligncenter\">,<br \/>\n<strong>\u4f7f\u7528 go \u9650\u5236\u5e76\u53d1\u6267\u884c\u7684\u4efb\u52a1<\/strong>,\u5982\u4f55\u5728 go \u4e2d\u5e76\u53d1\u6267\u884c\u4efb\u52a1\uff0c\u540c\u65f6\u63a7\u5236\u5e76\u53d1\u6570\u91cf\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u3002\u8fd9\u4e2a\u95ee\u9898\u8981\u6c42\u9650\u5236\u5e76\u53d1\u6267\u884c 100 \u4e2a\u4efb\u52a1\u7684\u6570\u91cf\uff0c\u6bcf\u6b21\u6700\u591a\u53ea\u6267\u884c 40 \u4e2a\u3002,<br \/>\n<strong>\u534f\u7a0b\u6c60\u89e3\u51b3\u65b9\u6848<\/strong>,\u4e00\u4e2a\u53ef\u884c\u7684\u89e3\u51b3\u65b9\u6848\u662f\u4f7f\u7528\u534f\u7a0b\u6c60\u3002\u534f\u7a0b\u6c60\u662f\u4e00\u4e2a\u9884\u5206\u914d\u534f\u7a0b\u96c6\u5408\uff0c\u53ef\u4ee5\u7528\u6765\u5e76\u884c\u6267\u884c\u4efb\u52a1\u3002go \u6807\u51c6\u5e93\u4e2d\u6ca1\u6709\u76f4\u63a5\u63d0\u4f9b\u534f\u7a0b\u6c60\uff0c\u4f46\u662f\u6709\u5f88\u591a\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u3002,<br \/>\n<strong>\u975e\u534f\u7a0b\u6c60\u89e3\u51b3\u65b9\u6848<\/strong>,\u5982\u679c\u4f60\u4e0d\u60f3\u4f7f\u7528\u7b2c\u4e09\u65b9\u534f\u7a0b\u6c60\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u901a\u9053\u548c\u540c\u6b65\u539f\u8bed\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a,\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff1a,\u7a0b\u5e8f\u7684\u5de5\u4f5c\u539f\u7406\u5982\u4e0b\uff1a,\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u5982\u4f55\u4f7f\u7528 Go \u8bed\u8a00\u9650\u5236\u5e76\u53d1\u6267\u884c\u7684\u4efb\u52a1\u6570\u91cf\uff0c\u5e76\u4fdd\u8bc1\u6bcf\u6b21\u6700\u591a\u53ea\u6267\u884c 40 \u4e2a\u4efb\u52a1\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002,tasks: \u6307\u5b9a\u8981\u6267\u884c\u7684\u4efb\u52a1\u603b\u6570\u3002,max: \u6307\u5b9a\u5e76\u53d1\u534f\u7a0b\u7684\u6700\u5927\u6570\u91cf\u3002,wg: \u7b49\u5f85\u7ec4\uff0c\u7528\u4e8e\u8ddf\u8e2a\u6b63\u5728\u8fd0\u884c\u7684\u534f\u7a0b\u6570\u3002,\u4f7f\u7528 go \u9650\u5236\u5e76\u53d1\u6267\u884c\u7684\u4efb\u52a1,\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528 Go \u8bed\u8a00\u9650\u5236\u5e76\u53d1\u6267\u884c\u7684\u4efb\u52a1\u6570\u91cf\uff0c\u5e76\u4fdd\u8bc1\u6bcf\u6b21\u6700\u591a\u53ea\u6267\u884c 40 \u4e2a\u4efb\u52a1\uff1f\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01,<br \/>\n<b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u5982\u4f55\u4f7f\u7528 Go \u8bed\u8a00\u9650\u5236\u5e76\u53d1\u6267\u884c\u7684\u4efb\u52a1\u6570\u91cf\uff0c\u5e76\u4fdd\u8bc1\u6bcf\u6b21\u6700\u591a\u53ea\u6267\u884c 40 \u4e2a\u4efb\u52a1\uff1f<\/span><\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 Go \u8bed\u8a00\u9650\u5236\u5e76\u53d1\u6267\u884c\u7684\u4efb\u52a1\u6570\u91cf\uff0c\u5e76\u4fdd\u8bc1\u6bcf\u6b21\u6700\u591a\u53ea\u6267\u884c 40 \u4e2a\u4efb\u52a1\uff1f<\/h1>\n<p><span>2024-10-30 14:55:03<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528 Go \u8bed\u8a00\u9650\u5236\u5e76\u53d1\u6267\u884c\u7684\u4efb\u52a1\u6570\u91cf\uff0c\u5e76\u4fdd\u8bc1\u6bcf\u6b21\u6700\u591a\u53ea\u6267\u884c 40 \u4e2a\u4efb\u52a1\uff1f\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241030\/17302712876721d8376785c.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u4f7f\u7528 go \u9650\u5236\u5e76\u53d1\u6267\u884c\u7684\u4efb\u52a1<\/strong><\/p>\n<p>\u5982\u4f55\u5728 go \u4e2d\u5e76\u53d1\u6267\u884c\u4efb\u52a1\uff0c\u540c\u65f6\u63a7\u5236\u5e76\u53d1\u6570\u91cf\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u3002\u8fd9\u4e2a\u95ee\u9898\u8981\u6c42\u9650\u5236\u5e76\u53d1\u6267\u884c 100 \u4e2a\u4efb\u52a1\u7684\u6570\u91cf\uff0c\u6bcf\u6b21\u6700\u591a\u53ea\u6267\u884c 40 \u4e2a\u3002<\/p>\n<p><strong>\u534f\u7a0b\u6c60\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u4e00\u4e2a\u53ef\u884c\u7684\u89e3\u51b3\u65b9\u6848\u662f\u4f7f\u7528\u534f\u7a0b\u6c60\u3002\u534f\u7a0b\u6c60\u662f\u4e00\u4e2a\u9884\u5206\u914d\u534f\u7a0b\u96c6\u5408\uff0c\u53ef\u4ee5\u7528\u6765\u5e76\u884c\u6267\u884c\u4efb\u52a1\u3002go \u6807\u51c6\u5e93\u4e2d\u6ca1\u6709\u76f4\u63a5\u63d0\u4f9b\u534f\u7a0b\u6c60\uff0c\u4f46\u662f\u6709\u5f88\u591a\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u3002<\/p>\n<p><strong>\u975e\u534f\u7a0b\u6c60\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u4e0d\u60f3\u4f7f\u7528\u7b2c\u4e09\u65b9\u534f\u7a0b\u6c60\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u901a\u9053\u548c\u540c\u6b65\u539f\u8bed\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\nfunc main() {\n    max := 40    \/\/ \u6700\u5927\u534f\u7a0b\u6570\u91cf\u9650\u5236\n    tasks := 100 \/\/ \u4efb\u52a1\u6570\u91cf\n    wg := sync.WaitGroup{}\n    ch := make(chan struct{}, max)\n    for i := 0; i &lt; tasks; i++ {\n        wg.Add(1)        \/\/ \u6dfb\u52a0\u8ba1\u6570\n        ch &lt;- struct{}{} \/\/ \u5199\u4e00\u4e2a\u6807\u8bb0\u5230 chan\uff0cchan\u7f13\u5b58\u6ee1\u65f6\u4f1a\u963b\u585e\n        go func(j int) {\n            defer func() {\n                wg.Done() \/\/ \u5c06\u8ba1\u6570\u51cf1\n                &lt;-ch      \/\/ \u8bfb\u53d6chan\n            }()\n\n            fmt.Println(j)\n\n        }(i)\n    }\n    wg.Wait() \/\/ \u7b49\u5f85\u52a0\u5165\u7684\u534f\u7a0b\u5168\u90e8\u5b8c\u6210\n    fmt.Println(\"done...\")\n}<\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff1a<\/p>\n<ul>\n<li>max: \u6307\u5b9a\u5e76\u53d1\u534f\u7a0b\u7684\u6700\u5927\u6570\u91cf\u3002<\/li>\n<li>tasks: \u6307\u5b9a\u8981\u6267\u884c\u7684\u4efb\u52a1\u603b\u6570\u3002<\/li>\n<li>wg: \u7b49\u5f85\u7ec4\uff0c\u7528\u4e8e\u8ddf\u8e2a\u6b63\u5728\u8fd0\u884c\u7684\u534f\u7a0b\u6570\u3002<\/li>\n<li>ch\uff1a\u4e00\u4e2a\u5bb9\u91cf\u4e3a max \u7684\u901a\u9053\uff0c\u7528\u4e8e\u9650\u5236\u5e76\u53d1\u534f\u7a0b\u6570\u91cf\u3002<\/li>\n<\/ul>\n<p>\u7a0b\u5e8f\u7684\u5de5\u4f5c\u539f\u7406\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u4e3b\u534f\u7a0b\u521b\u5efa ch \u901a\u9053\u5e76\u8bbe\u7f6e\u5bb9\u91cf\u4e3a max\u3002<\/li>\n<li>\u4e3b\u534f\u7a0b\u5faa\u73af\u521b\u5efa\u4efb\u52a1\uff0c\u6bcf\u4e2a\u4efb\u52a1\u90fd\u7528\u4e00\u4e2a\u72ec\u7acb\u7684\u534f\u7a0b\u8fd0\u884c\u3002<\/li>\n<li>\u5728\u521b\u5efa\u6bcf\u4e2a\u4efb\u52a1\u65f6\uff0c\u4e3b\u534f\u7a0b\u5c06\u4e00\u4e2a\u6807\u8bb0\u5199\u5165 ch \u901a\u9053\u3002<\/li>\n<li>\u5982\u679c ch \u901a\u9053\u5df2\u6ee1\uff0c\u53d1\u9001\u5230 ch \u7684\u64cd\u4f5c\u5c06\u4f1a\u88ab\u963b\u585e\uff0c\u76f4\u5230\u6709\u7a7a\u4f4d\u53ef\u7528\u3002<\/li>\n<li>\u6bcf\u4e2a\u4efb\u52a1\u534f\u7a0b\u5b8c\u6210\u5176\u4efb\u52a1\u540e\uff0c\u5b83\u4f1a\u4ece wg \u4e2d\u51cf\u53bb 1\uff0c\u5e76\u4ece ch \u4e2d\u8bfb\u53d6\u4e00\u4e2a\u6807\u8bb0\u3002<\/li>\n<li>\u4e3b\u534f\u7a0b\u7b49\u5f85 wg \u8fbe\u5230 0\uff0c\u8fd9\u8868\u793a\u6240\u6709\u4efb\u52a1\u90fd\u5df2\u5b8c\u6210\u3002<\/li>\n<\/ol>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u5982\u4f55\u4f7f\u7528 Go \u8bed\u8a00\u9650\u5236\u5e76\u53d1\u6267\u884c\u7684\u4efb\u52a1\u6570\u91cf\uff0c\u5e76\u4fdd\u8bc1\u6bcf\u6b21\u6700\u591a\u53ea\u6267\u884c 40 \u4e2a\u4efb\u52a1\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5982\u4f55\u4f7f\u7528 Python \u81ea\u52a8\u5316\u65e5\u5e38\u4efb\u52a1\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u4e3a\u4ec0\u4e48 Redis \u96c6\u7fa4\u7684\u8bfb\u53d6\u6570\u636e\u6027\u80fd\u4f1a\u8fd9\u4e48\u5dee\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9&#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-205977","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205977","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=205977"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205977\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205977"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}