{"id":208338,"date":"2025-07-08T16:14:52","date_gmt":"2025-07-08T08:14:52","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208338\/"},"modified":"2025-07-08T16:14:52","modified_gmt":"2025-07-08T08:14:52","slug":"%e6%a0%b9%e6%8d%ae%e5%81%9c%e6%ad%a2%e6%9d%a1%e4%bb%b6%ef%bc%8cgo-%e4%be%8b%e7%a8%8b%e6%b0%b8%e8%bf%9c%e4%b8%8d%e4%bc%9a%e9%80%80%e5%87%ba-%e6%97%a0%e6%b3%95%e6%89%be%e5%88%b0%e5%8e%9f%e5%9b%a0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208338\/","title":{"rendered":"\u6839\u636e\u505c\u6b62\u6761\u4ef6\uff0cGO \u4f8b\u7a0b\u6c38\u8fdc\u4e0d\u4f1a\u9000\u51fa &#8211; \u65e0\u6cd5\u627e\u5230\u539f\u56e0"},"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>\u6839\u636e\u505c\u6b62\u6761\u4ef6\uff0cGO \u4f8b\u7a0b\u6c38\u8fdc\u4e0d\u4f1a\u9000\u51fa &#8211; \u65e0\u6cd5\u627e\u5230\u539f\u56e0<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-05-01 22:48:37<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9Golang\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300a\u6839\u636e\u505c\u6b62\u6761\u4ef6\uff0cGO \u4f8b\u7a0b\u6c38\u8fdc\u4e0d\u4f1a\u9000\u51fa &#8211; \u65e0\u6cd5\u627e\u5230\u539f\u56e0\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u5de5\u4eba\u3002\u8fd9\u91cc\u7684\u60f3\u6cd5\u662f\u6839\u636e\u6761\u4ef6\u6a21\u62df\u6240\u6709 go \u4f8b\u7a0b\u7684 <code>clean shutdown<\/code> \u3002<\/p>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cgo \u4f8b\u7a0b\u4f1a\u6839\u636e\u5de5\u4eba\u6570\u91cf\u8fdb\u884c\u65cb\u8f6c\u3002\u6bcf\u4e2a go \u4f8b\u7a0b\u90fd\u4f1a\u8bfb\u53d6\u901a\u9053\uff0c\u6267\u884c\u4e00\u4e9b\u5de5\u4f5c\u5e76\u5c06\u8f93\u51fa\u53d1\u9001\u5230 <code>outputchannel<\/code>\u3002<\/p>\n<p>\u4e3b go \u4f8b\u7a0b\u8bfb\u53d6\u6b64\u8f93\u51fa\u5e76\u6253\u5370\u5b83\u3002\u4e3a\u4e86\u6a21\u62df\u505c\u6b62\u6761\u4ef6\uff0c\u5173\u95ed <code>donechannel<\/code>\u3002\u9884\u671f\u7684\u7ed3\u679c\u662f\u6bcf\u4e2a go \u4f8b\u7a0b\u4e2d\u7684 <code>select<\/code> \u90fd\u4f1a\u9009\u62e9\u5b83\u5e76\u6267\u884c <code>return<\/code> \uff0c\u800c <code>return<\/code> \u53c8\u4f1a\u8c03\u7528 <code>defer println<\/code> \u3002\u5b9e\u9645\u7684\u8f93\u51fa\u662f\u5b83\u6c38\u8fdc\u4e0d\u4f1a\u88ab\u8c03\u7528\u5e76\u4e14 main \u9000\u51fa\u3002<\/p>\n<p>\u4e0d\u77e5\u9053\u8fd9\u80cc\u540e\u7684\u539f\u56e0\u662f\u4ec0\u4e48\u3002<\/p>\n<pre>package main\n\nimport (\n    \"log\"\n    \"time\"\n)\n\nconst jobs = 100\nconst workers = 1\n\nvar timeout = time.After(5 * time.Second)\n\nfunc main() {\n\n    doneChannel := make(chan interface{})\n    outputChannel := make(chan int)\n\n    numberStream := generator()\n\n    for i := 1; i &lt;= workers; i++ {\n        go worker(doneChannel, numberStream, outputChannel)\n    }\n\n    \/\/ listen for output\nloop:\n    for {\n        select {\n        case i := &lt;-outputChannel:\n            log.Println(i)\n        case &lt;-timeout:\n            \/\/ before you timeout cleanup go routines\n            break loop\n        }\n    }\n\n    close(doneChannel)\n    time.Sleep(5 * time.Second)\n    log.Println(\"main exited\")\n\n}\n\nfunc generator() &lt;-chan int {\n    defer log.Println(\"generator completed !\")\n    c := make(chan int)\n    go func() {\n        for i := 1; i &lt;= jobs; i++ {\n            c &lt;- i\n        }\n\n        defer close(c)\n    }()\n    return c\n}\n\nfunc worker(done &lt;-chan interface{}, c &lt;-chan int, output chan&lt;- int) {\n    \/\/ this will be a go routine\n    \/\/ Do some work and send results to output Channel.\n    \/\/ Incase if the done channel is called kill the go routine.\n\n    defer log.Println(\"go routines exited\")\n\n    for {\n        select {\n        case &lt;-done:\n            log.Println(\"here\")\n            return\n        case i := &lt;-c:\n            time.Sleep(1 * time.Second) \/\/ worker delay\n            output &lt;- i * 100\n\n        }\n    }\n\n}\n<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u5f53\u4e3b\u5faa\u73af\u5728\u8d85\u65f6\u671f\u95f4\u5b8c\u6210\u65f6\uff0c\u60a8\u5c06\u7ee7\u7eed\u6267\u884c\u7a0b\u5e8f\u5e76<\/p>\n<ol>\n<li>\u5173\u95ed\u5df2\u5b8c\u6210\u9891\u9053<\/li>\n<li>\u6253\u5370\u6d88\u606f<\/li>\n<li>\u9000\u51fa<\/li>\n<\/ol>\n<p>\u6ca1\u6709\u7406\u7531\u7b49\u5f85\u4efb\u4f55 goroutine \u5904\u7406\u8be5\u901a\u9053\u7684\u4fe1\u53f7\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6dfb\u52a0\u4e00\u4e2a\u5c0f\u7761\u7720\uff0c\u60a8\u4f1a\u770b\u5230\u4e00\u4e9b\u6d88\u606f<\/p>\n<p>\u5728\u5b9e\u9645\u573a\u666f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u7b49\u5f85\u7ec4\u6765\u786e\u4fdd\u6240\u6709 goroutine \u6b63\u786e\u5b8c\u6210<\/p>\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\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/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-208338","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208338","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=208338"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208338\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}