{"id":300,"date":"2026-10-30T11:30:21","date_gmt":"2026-10-30T03:30:21","guid":{"rendered":"https:\/\/yykcj.com\/?p=300"},"modified":"2026-10-30T11:30:21","modified_gmt":"2026-10-30T03:30:21","slug":"go-%e8%af%ad%e8%a8%80%e5%b9%b6%e5%8f%91%e7%bc%96%e7%a8%8b%e5%ae%9e%e6%88%98%ef%bc%9a%e4%bb%8e-goroutine-%e5%88%b0-channel-%e7%9a%84%e6%b7%b1%e5%ba%a6%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=300","title":{"rendered":"Go \u8bed\u8a00\u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\uff1a\u4ece Goroutine \u5230 Channel \u7684\u6df1\u5ea6\u6307\u5357"},"content":{"rendered":"<h2>\u5f15\u8a00<\/h2>\n<p>Go \u8bed\u8a00\u51ed\u501f\u5176\u8f7b\u91cf\u7ea7\u7684 Goroutine \u548c\u5f3a\u5927\u7684 Channel\uff0c\u6210\u4e3a\u4e86\u5e76\u53d1\u7f16\u7a0b\u7684\u9996\u9009\u8bed\u8a00\u4e4b\u4e00\u3002\u7136\u800c\uff0c\u7f16\u5199\u6b63\u786e\u7684\u5e76\u53d1\u7a0b\u5e8f\u5e76\u4e0d\u5bb9\u6613\uff0c\u6570\u636e\u7ade\u4e89\u3001\u6b7b\u9501\u3001\u8d44\u6e90\u6cc4\u6f0f\u7b49\u95ee\u9898\u5e38\u5e38\u56f0\u6270\u5f00\u53d1\u8005\u3002\u672c\u6587\u5c06\u4ece\u5b9e\u6218\u89d2\u5ea6\u51fa\u53d1\uff0c\u5e26\u4f60\u4e00\u6b65\u6b65\u6784\u5efa\u9ad8\u5e76\u53d1\u7684 Worker Pool \u548c\u9650\u6d41\u5668\uff0c\u5e76\u5206\u4eab\u4e00\u4e9b\u72ec\u7279\u7684\u89c1\u89e3\u548c\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<h2>1. Goroutine \u57fa\u7840\u4e0e\u9677\u9631<\/h2>\n<p>Goroutine \u662f Go \u5e76\u53d1\u7684\u57fa\u77f3\uff0c\u4f46\u6ee5\u7528\u4f1a\u5bfc\u81f4\u95ee\u9898\u3002\u5148\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-go\">\npackage main\n\nimport (\n    &quot;fmt&quot;\n    &quot;time&quot;\n)\n\nfunc main() {\n    for i := 0; i &lt; 5; i++ {\n        go func() {\n            fmt.Println(i)\n        }()\n    }\n    time.Sleep(time.Second)\n}\n<\/code><\/pre>\n<p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\uff0c\u4f60\u53ef\u80fd\u4f1a\u770b\u5230\u91cd\u590d\u7684\u6570\u5b57\uff0c\u56e0\u4e3a\u95ed\u5305\u6355\u83b7\u4e86\u5faa\u73af\u53d8\u91cf <code>i<\/code>\u3002\u6b63\u786e\u7684\u505a\u6cd5\u662f\u4f20\u9012\u53c2\u6570\uff1a<\/p>\n<pre><code class=\"language-go\">\ngo func(i int) {\n    fmt.Println(i)\n}(i)\n<\/code><\/pre>\n<p>\ud83d\udca1 <strong>\u6ce8\u610f<\/strong>\uff1a\u5728 Go 1.22 \u4e4b\u524d\uff0c\u5faa\u73af\u53d8\u91cf\u662f\u5171\u4eab\u7684\uff0c1.22 \u4e4b\u540e\u6bcf\u4e2a\u8fed\u4ee3\u90fd\u6709\u65b0\u7684\u53d8\u91cf\uff0c\u4f46\u663e\u5f0f\u4f20\u9012\u53c2\u6570\u4ecd\u662f\u597d\u4e60\u60ef\u3002<\/p>\n<h2>2. Channel\uff1a\u5e76\u53d1\u5b89\u5168\u7684\u901a\u4fe1\u65b9\u5f0f<\/h2>\n<p>Channel \u662f Goroutine \u4e4b\u95f4\u901a\u4fe1\u7684\u7ba1\u9053\u3002\u4f7f\u7528 Channel \u53ef\u4ee5\u907f\u514d\u663e\u5f0f\u7684\u9501\uff0c\u9075\u5faa\u201c\u4e0d\u8981\u901a\u8fc7\u5171\u4eab\u5185\u5b58\u6765\u901a\u4fe1\uff0c\u800c\u8981\u901a\u8fc7\u901a\u4fe1\u6765\u5171\u4eab\u5185\u5b58\u201d\u7684\u539f\u5219\u3002<\/p>\n<h3>2.1 \u65e0\u7f13\u51b2 Channel \u4e0e\u540c\u6b65<\/h3>\n<pre><code class=\"language-go\">\nch := make(chan int)\ngo func() {\n    ch &lt;- 42\n}()\nfmt.Println(&lt;-ch) \/\/ \u8f93\u51fa 42\n<\/code><\/pre>\n<p>\u65e0\u7f13\u51b2 Channel \u7684\u53d1\u9001\u548c\u63a5\u6536\u662f\u540c\u6b65\u7684\uff0c\u5e38\u7528\u4e8e\u786e\u4fdd Goroutine \u5b8c\u6210\u3002<\/p>\n<h3>2.2 \u5e26\u7f13\u51b2 Channel \u4e0e\u89e3\u8026<\/h3>\n<pre><code class=\"language-go\">\nch := make(chan int, 3)\nch &lt;- 1\nch &lt;- 2\nch &lt;- 3\nclose(ch)\nfor v := range ch {\n    fmt.Println(v)\n}\n<\/code><\/pre>\n<p>\u5e26\u7f13\u51b2 Channel \u53ef\u4ee5\u5728\u7f13\u51b2\u533a\u672a\u6ee1\u65f6\u5f02\u6b65\u53d1\u9001\uff0c\u63d0\u9ad8\u541e\u5410\u91cf\u3002<\/p>\n<h2>3. Select \u591a\u8def\u590d\u7528<\/h2>\n<p><code>select<\/code> \u8bed\u53e5\u7528\u4e8e\u540c\u65f6\u7b49\u5f85\u591a\u4e2a Channel \u64cd\u4f5c\uff0c\u7c7b\u4f3c\u4e8e <code>switch<\/code>\uff0c\u4f46\u6bcf\u4e2a case \u90fd\u662f\u901a\u4fe1\u64cd\u4f5c\u3002<\/p>\n<pre><code class=\"language-go\">\nselect {\ncase msg1 := &lt;-ch1:\n    fmt.Println(&quot;received&quot;, msg1)\ncase ch2 &lt;- msg2:\n    fmt.Println(&quot;sent&quot;, msg2)\ncase &lt;-time.After(time.Second):\n    fmt.Println(&quot;timeout&quot;)\n}\n<\/code><\/pre>\n<p><code>select<\/code> \u5e38\u7528\u4e8e\u5b9e\u73b0\u8d85\u65f6\u63a7\u5236\u548c\u975e\u963b\u585e\u901a\u4fe1\u3002<\/p>\n<h2>4. \u5b9e\u6218\uff1a\u6784\u5efa Worker Pool<\/h2>\n<p>Worker Pool \u662f\u9650\u5236\u5e76\u53d1\u6570\u7684\u7ecf\u5178\u6a21\u5f0f\u3002\u4e0b\u9762\u5b9e\u73b0\u4e00\u4e2a\u53ef\u52a8\u6001\u8c03\u6574 worker \u6570\u91cf\u7684\u6c60\u5b50\u3002<\/p>\n<pre><code class=\"language-go\">\ntype WorkerPool struct {\n    tasks   chan func()\n    workers int\n    wg      sync.WaitGroup\n}\n\nfunc NewWorkerPool(workers int) *WorkerPool {\n    p := &amp;WorkerPool{\n        tasks:   make(chan func()),\n        workers: workers,\n    }\n    p.start()\n    return p\n}\n\nfunc (p *WorkerPool) start() {\n    for i := 0; i &lt; p.workers; i++ {\n        p.wg.Add(1)\n        go func() {\n            defer p.wg.Done()\n            for task := range p.tasks {\n                task()\n            }\n        }()\n    }\n}\n\nfunc (p *WorkerPool) Submit(task func()) {\n    p.tasks &lt;- task\n}\n\nfunc (p *WorkerPool) Stop() {\n    close(p.tasks)\n    p.wg.Wait()\n}\n<\/code><\/pre>\n<p>\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-go\">\npool := NewWorkerPool(3)\nfor i := 0; i &lt; 10; i++ {\n    i := i\n    pool.Submit(func() {\n        fmt.Println(&quot;task&quot;, i)\n    })\n}\npool.Stop()\n<\/code><\/pre>\n<p>\ud83d\udca1 <strong>\u6ce8\u610f<\/strong>\uff1a<code>Submit<\/code> \u5728\u6c60\u505c\u6b62\u540e\u4f1a panic\uff0c\u5b9e\u9645\u4f7f\u7528\u4e2d\u5e94\u589e\u52a0\u5173\u95ed\u72b6\u6001\u68c0\u67e5\u3002<\/p>\n<h2>5. \u5b9e\u6218\uff1a\u57fa\u4e8e Channel \u7684\u9650\u6d41\u5668<\/h2>\n<p>\u9650\u6d41\u5668\u7528\u4e8e\u63a7\u5236\u8bf7\u6c42\u901f\u7387\u3002\u5229\u7528\u5e26\u7f13\u51b2 Channel \u5b9e\u73b0\u4ee4\u724c\u6876\uff1a<\/p>\n<pre><code class=\"language-go\">\ntype RateLimiter struct {\n    tokens chan struct{}\n    tick   *time.Ticker\n    done   chan struct{}\n}\n\nfunc NewRateLimiter(rate int, burst int) *RateLimiter {\n    rl := &amp;RateLimiter{\n        tokens: make(chan struct{}, burst),\n        tick:   time.NewTicker(time.Second \/ time.Duration(rate)),\n        done:   make(chan struct{}),\n    }\n    \/\/ \u521d\u59cb\u586b\u5145\n    for i := 0; i &lt; burst; i++ {\n        rl.tokens &lt;- struct{}{}\n    }\n    go rl.fill()\n    return rl\n}\n\nfunc (rl *RateLimiter) fill() {\n    for {\n        select {\n        case &lt;-rl.tick.C:\n            select {\n            case rl.tokens &lt;- struct{}{}:\n            default: \/\/ \u6876\u6ee1\u5219\u4e22\u5f03\n            }\n        case &lt;-rl.done:\n            rl.tick.Stop()\n            return\n        }\n    }\n}\n\nfunc (rl *RateLimiter) Allow() bool {\n    select {\n    case &lt;-rl.tokens:\n        return true\n    default:\n        return false\n    }\n}\n\nfunc (rl *RateLimiter) Stop() {\n    close(rl.done)\n}\n<\/code><\/pre>\n<p>\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-go\">\nrl := NewRateLimiter(10, 5) \/\/ \u6bcf\u79d210\u4e2a\u4ee4\u724c\uff0c\u6876\u5bb9\u91cf5\ndefer rl.Stop()\nfor i := 0; i &lt; 20; i++ {\n    if rl.Allow() {\n        fmt.Println(&quot;request&quot;, i, &quot;allowed&quot;)\n    } else {\n        fmt.Println(&quot;request&quot;, i, &quot;denied&quot;)\n    }\n    time.Sleep(50 * time.Millisecond)\n}\n<\/code><\/pre>\n<h2>6. Context\uff1a\u5e76\u53d1\u63a7\u5236\u7684\u745e\u58eb\u519b\u5200<\/h2>\n<p><code>context<\/code> \u7528\u4e8e\u5728 Goroutine \u4e4b\u95f4\u4f20\u9012\u53d6\u6d88\u4fe1\u53f7\u548c\u8d85\u65f6\u3002<\/p>\n<pre><code class=\"language-go\">\nctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)\ndefer cancel()\n\ngo func() {\n    select {\n    case &lt;-ctx.Done():\n        fmt.Println(&quot;canceled:&quot;, ctx.Err())\n    }\n}()\n\ntime.Sleep(3 * time.Second)\n<\/code><\/pre>\n<p>\ud83d\udca1 <strong>\u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a\u5c06 <code>context<\/code> \u4f5c\u4e3a\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u4e14\u4e0d\u8981\u5b58\u50a8\u5728\u7ed3\u6784\u4f53\u4e2d\u3002<\/p>\n<h2>7. \u5e38\u89c1\u9677\u9631\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h2>\n<ul>\n<li><strong>\u6570\u636e\u7ade\u4e89<\/strong>\uff1a\u4f7f\u7528 <code>go run -race<\/code> \u68c0\u6d4b\u7ade\u4e89\u6761\u4ef6\u3002<\/li>\n<li><strong>Goroutine \u6cc4\u6f0f<\/strong>\uff1a\u786e\u4fdd\u6bcf\u4e2a\u542f\u52a8\u7684 Goroutine \u90fd\u6709\u9000\u51fa\u7684\u8def\u5f84\uff0c\u5e38\u7528 <code>context<\/code> \u6216 <code>done<\/code> channel\u3002<\/li>\n<li><strong>Channel \u6b7b\u9501<\/strong>\uff1a\u907f\u514d\u5728\u540c\u4e00\u4e2a Goroutine \u4e2d\u540c\u65f6\u8bfb\u5199\u65e0\u7f13\u51b2 Channel\u3002<\/li>\n<li><strong>\u8fc7\u5ea6\u5e76\u53d1<\/strong>\uff1a\u4f7f\u7528 Worker Pool \u6216\u4fe1\u53f7\u91cf\u9650\u5236\u5e76\u53d1\u6570\uff0c\u9632\u6b62\u8d44\u6e90\u8017\u5c3d\u3002<\/li>\n<\/ul>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u672c\u6587\u4ece Goroutine \u57fa\u7840\u5230 Channel\u3001Select\u3001Context\uff0c\u9010\u6b65\u6784\u5efa\u4e86 Worker Pool \u548c\u9650\u6d41\u5668\u3002\u638c\u63e1\u8fd9\u4e9b\u6a21\u5f0f\u540e\uff0c\u4f60\u53ef\u4ee5\u66f4\u81ea\u4fe1\u5730\u5904\u7406\u5e76\u53d1\u4efb\u52a1\u3002\u8bb0\u4f4f\uff1a\u5e76\u53d1\u4e0d\u662f\u94f6\u5f39\uff0c\u6b63\u786e\u7684\u540c\u6b65\u548c\u8d44\u6e90\u7ba1\u7406\u624d\u662f\u5173\u952e\u3002<\/p>\n<p><strong>\u5ef6\u4f38\u9605\u8bfb<\/strong>\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/go.dev\/blog\/codelab-share\">Go \u5b98\u65b9\u535a\u5ba2\uff1aShare Memory By Communicating<\/a><\/li>\n<li><a href=\"https:\/\/go.dev\/blog\/pipelines\">Go \u5e76\u53d1\u6a21\u5f0f\uff1aPipelines and Cancellation<\/a><\/li>\n<li>\u4e66\u7c4d\u300aGo \u8bed\u8a00\u9ad8\u7ea7\u7f16\u7a0b\u300b\u5e76\u53d1\u7ae0\u8282<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u901a\u8fc7\u5b9e\u6218\u6848\u4f8b\u6df1\u5165\u8bb2\u89e3 Go \u5e76\u53d1\u7f16\u7a0b\u7684\u6838\u5fc3\uff1aGoroutine\u3001Channel\u3001Select \u4e0e Context\u3002\u4f60\u5c06\u5b66\u4f1a\u6784\u5efa\u9ad8\u5e76\u53d1\u7684 Worker Pool \u548c\u9650\u6d41\u5668\uff0c\u5e76\u638c\u63e1\u907f\u514d\u5e38\u89c1\u9677\u9631\u7684\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[230,295,92,229,228],"class_list":["post-300","post","type-post","status-publish","format-standard","hentry","category-4","tag-channel","tag-context","tag-go","tag-goroutine","tag-228"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=300"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/300\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}