{"id":207789,"date":"2025-07-08T11:55:36","date_gmt":"2025-07-08T03:55:36","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207789\/"},"modified":"2025-07-08T11:55:36","modified_gmt":"2025-07-08T03:55:36","slug":"golang-%e5%b7%a5%e4%bd%9c%e6%b1%a0%e5%ae%9e%e7%8e%b0%e6%84%8f%e5%a4%96%e5%b7%a5%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207789\/","title":{"rendered":"Golang \u5de5\u4f5c\u6c60\u5b9e\u73b0\u610f\u5916\u5de5\u4f5c"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>Golang \u5de5\u4f5c\u6c60\u5b9e\u73b0\u610f\u5916\u5de5\u4f5c<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-25 17:09:35<\/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\u662f\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\">\u300aGolang \u5de5\u4f5c\u6c60\u5b9e\u73b0\u610f\u5916\u5de5\u4f5c\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> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u5df2\u7ecf\u5b9e\u73b0\u4e86\u4e00\u4e2a golang \u5de5\u4f5c\u6c60\uff0c\u5982\u4e0b\u6240\u793a\uff0c\u5176\u4e2d sem \u548c work \u662f\u901a\u9053\u3002 sem \u662f\u4e00\u4e2a\u8ddf\u8e2a\u5f53\u524d\u6d3b\u52a8\u7684\u5de5\u4eba\uff08goroutines\uff09\u6570\u91cf\u7684\u6e20\u9053\u3002 work \u662f\u5c06\u529f\u80fd\u4f20\u9012\u7ed9\u6d3b\u8dc3\u5de5\u4f5c\u4eba\u5458\u6267\u884c\u7684\u901a\u9053\u3002\u8d85\u65f6\u5c06\u4f7f\u4efb\u4f55\u5de5\u4f5c\u7ebf\u7a0b\u5728\u8d85\u65f6\u65f6\u95f4\u5185\u5904\u4e8e\u7a7a\u95f2\u72b6\u6001\u3002<\/p>\n<pre>package main\n\nimport (\n    \"time\"\n)\n\ntype pool struct {\n    sem chan struct{}\n    work chan func()\n    timeout time.duration\n}\n\nfunc newpool(max, size, spawn int, timeout time.duration) *pool {\n    if spawn &lt;= 0 {\n        panic(\"workpool spawn is &lt;= 0\")\n    }\n    if spawn &gt; max {\n        panic(\"workpool spawn &gt; max workers\")\n    }\n    p := &amp;pool{\n        sem: make(chan struct{}, max),\n        work: make(chan func(), size),\n        timeout: timeout,\n    }\n    for i := 0; i &lt; spawn; i++ {\n        p.sem &lt;- struct{}{}\n        go p.worker(func() {})\n    }\n    return p\n\n}\n\nfunc (p *pool) addtask(task func()) {\n    select {\n        case p.work &lt;- task:\n            return\n        case p.sem &lt;- struct{}{}:\n            go p.worker(task)\n            return\n    }\n}\n\nfunc (p *pool) worker(task func()) {\n    t := time.newtimer(p.timeout)\n    defer func() {\n        t.stop()\n        &lt;- p.sem\n    }()\n\n    task()\n\n    for {\n        select {\n            case task := &lt;- p.work:\n                t.reset(p.timeout)\n                task()\n            case &lt;- t.c:\n                return\n        }\n    }\n}<\/pre>\n<p>\u6211\u6b63\u5728\u901a\u8fc7\u5728 for \u5faa\u73af\u4e2d\u6253\u5370 i \u7684\u503c\u6765\u8fdb\u884c\u6d4b\u8bd5\uff0c\u65b9\u6cd5\u662f\u5c06\u5176\u4f20\u9012\u5230\u5c01\u88c5\u5728\u533f\u540d\u51fd\u6570\u4e2d\u7684\u6c60\u4e2d\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"time\"\n)\n\nfunc main() {\n    fmt.println(\"hello, world!\")\n\n    p := newpool(3, 10, 1, time.duration(5) * time.second)\n\n    for i:=0; i&lt;30; i++ {\n        p.addtask(func () {\n            fmt.print(i, \" \")\n        })\n    }\n    time.sleep(10 * time.second)\n    fmt.println(\"end\")\n\n}<\/pre>\n<p>\u9884\u671f\u8f93\u51fa\u5e94\u8be5\u662f\u4ece 0 \u5230 29 \u7684\u5e8f\u5217\u53f7\uff0c\u4f46\u5b9e\u9645\u8f93\u51fa\u662f<\/p>\n<pre>Hello, world!\n12 12 12 12 12 12 12 12 12 12 12 12 13 25 25 25 25 25 25 25 25 25 25 25 26 25 30 30 30 30 End<\/pre>\n<p>\u6211\u4e0d\u660e\u767d\u4e3a\u4ec0\u4e48\u8f93\u51fa\u662f\u8fd9\u6837\u7684\u3002<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u7684\u51fd\u6570\u95ed\u5305\u90fd\u5f15\u7528\u76f8\u540c\u7684 <code>i<\/code> \u503c\u3002\u8fd9\u4f1a\u4ea7\u751f\u7ade\u4e89\u6761\u4ef6\uff0c\u5f53\u51fd\u6570\u88ab\u8c03\u5ea6\u65f6\uff0c\u5b83\u4eec\u6b63\u5728\u8bfb\u53d6\u4e0d\u65ad\u53d8\u5316\u7684\u503c &#8211; \u56e0\u6b64\u60a8\u4f1a\u770b\u5230\u4e0d\u53ef\u9884\u6d4b\u7684\u8f93\u51fa\u3002<\/p>\n<p>\u4e3a\u4e86\u786e\u4fdd\u95ed\u5305\u83b7\u5f97\u552f\u4e00\u503c\uff0c\u8bf7\u5728\u5faa\u73af\u5185\u58f0\u660e\u53d8\u91cf\u3002\u4e00\u4e2a\u7b80\u5355\u7684\u6280\u5de7\u662f\u901a\u8fc7\u5f71\u5b50\u58f0\u660e\u76f8\u540c\u7684\u53d8\u91cf\u540d <code>i := i<\/code><\/p>\n<pre>for i:=0; i&lt;30; i++ {\n    i:= i                \/\/ &lt;- add this\n    p.addtask(func () {\n        fmt.print(i, \" \")\n    })\n}<\/pre>\n<\/p>\n<p>\u987a\u4fbf\u8bf4\u4e00\u53e5\uff0c \u6587\u6863\u4e2d\u4ecb\u7ecd\u4e86\u6b64\u6280\u672f\uff0c\u8bf7\u53c2\u9605\u672c\u8282\uff1a<\/p>\n<p>\u5199\u8d77\u6765\u53ef\u80fd\u770b\u8d77\u6765\u5f88\u5947\u602a<\/p>\n<pre>req := req<\/pre>\n<p>\u4f46\u5728 go \u4e2d\u8fd9\u6837\u505a\u662f\u5408\u6cd5\u4e14\u60ef\u7528\u7684\u3002\u60a8\u5c06\u83b7\u5f97\u5168\u65b0\u7248\u672c \u5177\u6709\u76f8\u540c\u540d\u79f0\u7684\u53d8\u91cf\uff0c\u6545\u610f\u9690\u85cf\u5faa\u73af \u672c\u5730\u53d8\u91cf\uff0c\u4f46\u6bcf\u4e2a goroutine \u90fd\u662f\u552f\u4e00\u7684\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aGolang \u5de5\u4f5c\u6c60\u5b9e\u73b0\u610f\u5916\u5de5\u4f5c\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#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-207789","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207789","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=207789"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207789\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}