{"id":316,"date":"2026-11-14T12:01:30","date_gmt":"2026-11-14T04:01:30","guid":{"rendered":"https:\/\/yykcj.com\/?p=316"},"modified":"2026-11-14T12:01:30","modified_gmt":"2026-11-14T04:01:30","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%e8%bf%9b%e9%98%b6%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=316","title":{"rendered":"Go \u8bed\u8a00\u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\uff1a\u4ece Goroutine \u5230 Channel \u7684\u8fdb\u9636\u6307\u5357"},"content":{"rendered":"<h1>Go \u8bed\u8a00\u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\uff1a\u4ece Goroutine \u5230 Channel \u7684\u8fdb\u9636\u6307\u5357<\/h1>\n<p>Go \u8bed\u8a00\u51ed\u501f\u5176\u8f7b\u91cf\u7ea7\u7684 Goroutine \u548c\u5f3a\u5927\u7684 Channel\uff0c\u6210\u4e3a\u5e76\u53d1\u7f16\u7a0b\u7684\u70ed\u95e8\u9009\u62e9\u3002\u4f46\u5f88\u591a\u5f00\u53d1\u8005\u5728\u4f7f\u7528\u65f6\u4ecd\u4f1a\u9677\u5165\u6b7b\u9501\u3001\u8d44\u6e90\u6cc4\u6f0f\u7b49\u9677\u9631\u3002\u672c\u6587\u5c06\u4ece\u5b9e\u6218\u89d2\u5ea6\u51fa\u53d1\uff0c\u901a\u8fc7\u5b8c\u6574\u53ef\u8fd0\u884c\u7684\u793a\u4f8b\uff0c\u5e26\u4f60\u638c\u63e1 Go \u5e76\u53d1\u7f16\u7a0b\u7684\u7cbe\u9ad3\uff0c\u5e76\u5206\u4eab\u4e00\u4e9b\u5bb9\u6613\u88ab\u5ffd\u7565\u7684\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<h2>1. Goroutine\uff1a\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\u7684\u542f\u52a8\u4e0e\u751f\u547d\u5468\u671f<\/h2>\n<p>Goroutine \u662f Go \u5e76\u53d1\u7684\u57fa\u77f3\u3002\u542f\u52a8\u4e00\u4e2a Goroutine \u53ea\u9700\u5728\u51fd\u6570\u8c03\u7528\u524d\u52a0\u4e0a <code>go<\/code> \u5173\u952e\u5b57\uff1a<\/p>\n<pre><code class=\"language-go\">\npackage main\n\nimport (\n    &quot;fmt&quot;\n    &quot;time&quot;\n)\n\nfunc worker(id int) {\n    fmt.Printf(&quot;Worker %d starting\\n&quot;, id)\n    time.Sleep(time.Second)\n    fmt.Printf(&quot;Worker %d done\\n&quot;, id)\n}\n\nfunc main() {\n    for i := 1; i &lt;= 3; i++ {\n        go worker(i)\n    }\n    time.Sleep(2 * time.Second) \/\/ \u7b49\u5f85\u6240\u6709 worker \u5b8c\u6210\n}\n<\/code><\/pre>\n<p><strong>\u6ce8\u610f<\/strong>\uff1a\u4e3b\u51fd\u6570\u9000\u51fa\u65f6\uff0c\u6240\u6709\u672a\u5b8c\u6210\u7684 Goroutine \u4f1a\u88ab\u5f3a\u5236\u7ec8\u6b62\u3002\u56e0\u6b64\u9700\u8981\u540c\u6b65\u673a\u5236\uff08\u5982 <code>sync.WaitGroup<\/code>\uff09\u6765\u7b49\u5f85\u5b83\u4eec\u7ed3\u675f\u3002<\/p>\n<h3>\u4f7f\u7528 WaitGroup \u6b63\u786e\u7b49\u5f85<\/h3>\n<pre><code class=\"language-go\">\nvar wg sync.WaitGroup\nfor i := 1; i &lt;= 3; i++ {\n    wg.Add(1)\n    go func(id int) {\n        defer wg.Done()\n        worker(id)\n    }(i)\n}\nwg.Wait()\n<\/code><\/pre>\n<p>\ud83d\udca1 <strong>\u8e29\u5751\u63d0\u793a<\/strong>\uff1a\u5728\u5faa\u73af\u4e2d\u542f\u52a8 Goroutine \u65f6\uff0c\u52a1\u5fc5\u901a\u8fc7\u53c2\u6570\u4f20\u9012\u5faa\u73af\u53d8\u91cf\uff08\u5982\u4e0a\u9762\u7684 <code>i<\/code>\uff09\uff0c\u5426\u5219\u6240\u6709 Goroutine \u53ef\u80fd\u5171\u4eab\u540c\u4e00\u4e2a\u53d8\u91cf\uff0c\u5bfc\u81f4\u610f\u5916\u7ed3\u679c\u3002<\/p>\n<h2>2. Channel\uff1aGoroutine \u95f4\u7684\u901a\u4fe1\u6865\u6881<\/h2>\n<p>Channel \u662f\u7c7b\u578b\u5b89\u5168\u7684\u7ba1\u9053\uff0c\u7528\u4e8e\u5728 Goroutine \u4e4b\u95f4\u4f20\u9012\u6570\u636e\u3002\u521b\u5efa Channel \u4f7f\u7528 <code>make(chan T)<\/code>\uff0c\u5176\u4e2d T \u662f\u4f20\u8f93\u7c7b\u578b\u3002<\/p>\n<h3>\u65e0\u7f13\u51b2 Channel \u7684\u540c\u6b65\u7279\u6027<\/h3>\n<pre><code class=\"language-go\">\nch := make(chan string)\ngo func() {\n    ch &lt;- &quot;hello&quot; \/\/ \u53d1\u9001\u4f1a\u963b\u585e\uff0c\u76f4\u5230\u6709\u4eba\u63a5\u6536\n}()\nmsg := &lt;-ch \/\/ \u63a5\u6536\nfmt.Println(msg)\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\u4e24\u4e2a Goroutine \u5728\u67d0\u4e2a\u70b9\u6c47\u5408\u3002<\/p>\n<h3>\u5e26\u7f13\u51b2 Channel \u63d0\u9ad8\u541e\u5410\u91cf<\/h3>\n<pre><code class=\"language-go\">\nch := make(chan int, 3) \/\/ \u7f13\u51b2\u533a\u5927\u5c0f\u4e3a 3\nch &lt;- 1\nch &lt;- 2\nch &lt;- 3\n\/\/ ch &lt;- 4 \/\/ \u4f1a\u963b\u585e\uff0c\u56e0\u4e3a\u7f13\u51b2\u533a\u5df2\u6ee1\nclose(ch)\nfor v := range ch {\n    fmt.Println(v)\n}\n<\/code><\/pre>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a\u6839\u636e\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u7684\u901f\u7387\u5408\u7406\u8bbe\u7f6e\u7f13\u51b2\u533a\u5927\u5c0f\uff0c\u907f\u514d\u9891\u7e41\u963b\u585e\u3002<\/p>\n<h2>3. Select\uff1a\u591a\u8def\u590d\u7528\u7684\u5229\u5668<\/h2>\n<p><code>select<\/code> \u8bed\u53e5\u8ba9\u4e00\u4e2a Goroutine \u540c\u65f6\u7b49\u5f85\u591a\u4e2a Channel \u64cd\u4f5c\uff0c\u7c7b\u4f3c <code>switch<\/code> \u4f46\u4e13\u7528\u4e8e Channel\u3002<\/p>\n<pre><code class=\"language-go\">\nfunc main() {\n    ch1 := make(chan string)\n    ch2 := make(chan string)\n\n    go func() {\n        time.Sleep(1 * time.Second)\n        ch1 &lt;- &quot;from ch1&quot;\n    }()\n    go func() {\n        time.Sleep(2 * time.Second)\n        ch2 &lt;- &quot;from ch2&quot;\n    }()\n\n    for i := 0; i &lt; 2; i++ {\n        select {\n        case msg1 := &lt;-ch1:\n            fmt.Println(msg1)\n        case msg2 := &lt;-ch2:\n            fmt.Println(msg2)\n        }\n    }\n}\n<\/code><\/pre>\n<p><strong>\u8d85\u65f6\u63a7\u5236<\/strong>\uff1a\u7ed3\u5408 <code>time.After<\/code> \u5b9e\u73b0\u8d85\u65f6\uff1a<\/p>\n<pre><code class=\"language-go\">\nselect {\ncase res := &lt;-ch:\n    fmt.Println(res)\ncase &lt;-time.After(500 * time.Millisecond):\n    fmt.Println(&quot;timeout&quot;)\n}\n<\/code><\/pre>\n<p>\ud83d\udca1 <strong>\u6ce8\u610f<\/strong>\uff1a<code>time.After<\/code> \u5728\u6bcf\u6b21 select \u4e2d\u90fd\u4f1a\u521b\u5efa\u65b0\u7684\u8ba1\u65f6\u5668\uff0c\u5728\u5faa\u73af\u4e2d\u5927\u91cf\u4f7f\u7528\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u538b\u529b\u3002\u5bf9\u4e8e\u9ad8\u9891\u573a\u666f\uff0c\u5efa\u8bae\u4f7f\u7528 <code>time.NewTimer<\/code> \u5e76\u91cd\u7f6e\u3002<\/p>\n<h2>4. Context\uff1a\u4f18\u96c5\u5730\u63a7\u5236\u5e76\u53d1<\/h2>\n<p><code>context<\/code> \u5305\u7528\u4e8e\u5728 Goroutine \u4e4b\u95f4\u4f20\u9012\u53d6\u6d88\u4fe1\u53f7\u3001\u8d85\u65f6\u548c\u622a\u6b62\u65f6\u95f4\u3002\u5b83\u662f\u7ba1\u7406\u5e76\u53d1\u751f\u547d\u5468\u671f\u7684\u6807\u51c6\u65b9\u5f0f\u3002<\/p>\n<pre><code class=\"language-go\">\nfunc main() {\n    ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)\n    defer cancel()\n\n    go func() {\n        select {\n        case &lt;-ctx.Done():\n            fmt.Println(&quot;context canceled:&quot;, ctx.Err())\n        }\n    }()\n\n    time.Sleep(3 * time.Second) \/\/ \u6a21\u62df\u5de5\u4f5c\n}\n<\/code><\/pre>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5c06 <code>context.Context<\/code> \u4f5c\u4e3a\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4f20\u9012\u3002<\/li>\n<li>\u4e0d\u8981\u5c06 Context \u5b58\u50a8\u5728\u7ed3\u6784\u4f53\u4e2d\uff0c\u800c\u662f\u663e\u5f0f\u4f20\u9012\u3002<\/li>\n<li>\u4f7f\u7528 <code>context.WithCancel<\/code>\u3001<code>WithTimeout<\/code> \u6216 <code>WithDeadline<\/code> \u521b\u5efa\u53ef\u53d6\u6d88\u7684 Context\u3002<\/li>\n<\/ul>\n<h2>5. \u5e76\u53d1\u6a21\u5f0f\u5b9e\u6218\uff1aWorker Pool<\/h2>\n<p>Worker Pool \u662f\u5e38\u89c1\u7684\u5e76\u53d1\u6a21\u5f0f\uff0c\u7528\u4e8e\u9650\u5236\u5e76\u53d1\u6570\u91cf\uff0c\u907f\u514d\u8d44\u6e90\u8017\u5c3d\u3002<\/p>\n<pre><code class=\"language-go\">\nfunc worker(id int, jobs &lt;-chan int, results chan&lt;- int) {\n    for j := range jobs {\n        fmt.Printf(&quot;worker %d processing job %d\\n&quot;, id, j)\n        time.Sleep(time.Second) \/\/ \u6a21\u62df\u5de5\u4f5c\n        results &lt;- j * 2\n    }\n}\n\nfunc main() {\n    const numJobs = 5\n    jobs := make(chan int, numJobs)\n    results := make(chan int, numJobs)\n\n    \/\/ \u542f\u52a8 3 \u4e2a worker\n    for w := 1; w &lt;= 3; w++ {\n        go worker(w, jobs, results)\n    }\n\n    \/\/ \u53d1\u9001\u4efb\u52a1\n    for j := 1; j &lt;= numJobs; j++ {\n        jobs &lt;- j\n    }\n    close(jobs)\n\n    \/\/ \u6536\u96c6\u7ed3\u679c\n    for r := 1; r &lt;= numJobs; r++ {\n        &lt;-results\n    }\n}\n<\/code><\/pre>\n<p><strong>\u6ce8\u610f<\/strong>\uff1a\u786e\u4fdd\u5728\u6240\u6709\u4efb\u52a1\u53d1\u9001\u5b8c\u6bd5\u540e\u5173\u95ed <code>jobs<\/code> channel\uff0c\u5426\u5219 worker \u4f1a\u4e00\u76f4\u963b\u585e\u5728 <code>range<\/code> \u4e0a\u3002<\/p>\n<h2>6. \u5e38\u89c1\u9677\u9631\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h2>\n<h3>\u9677\u9631 1\uff1aGoroutine \u6cc4\u6f0f<\/h3>\n<p>\u5982\u679c Goroutine \u963b\u585e\u5728 Channel \u64cd\u4f5c\u4e0a\u4e14\u6ca1\u6709\u9000\u51fa\u8def\u5f84\uff0c\u5c31\u4f1a\u5bfc\u81f4\u6cc4\u6f0f\u3002\u59cb\u7ec8\u786e\u4fdd\u6709\u9000\u51fa\u673a\u5236\uff0c\u4f8b\u5982\u4f7f\u7528 <code>context<\/code> \u6216 <code>done<\/code> channel\u3002<\/p>\n<pre><code class=\"language-go\">\nfunc leaky() {\n    ch := make(chan int)\n    go func() {\n        &lt;-ch \/\/ \u6c38\u8fdc\u963b\u585e\n    }()\n    \/\/ \u6ca1\u6709\u5173\u95ed ch\uff0cGoroutine \u6cc4\u6f0f\n}\n<\/code><\/pre>\n<p><strong>\u4fee\u590d<\/strong>\uff1a\u4f7f\u7528 <code>context<\/code> \u6216\u663e\u5f0f\u5173\u95ed channel\u3002<\/p>\n<h3>\u9677\u9631 2\uff1a\u7ade\u6001\u6761\u4ef6<\/h3>\n<p>\u591a\u4e2a Goroutine \u5e76\u53d1\u8bfb\u5199\u5171\u4eab\u53d8\u91cf\u65f6\uff0c\u9700\u8981\u4f7f\u7528 <code>sync.Mutex<\/code> \u6216\u539f\u5b50\u64cd\u4f5c\u3002<\/p>\n<pre><code class=\"language-go\">\nvar mu sync.Mutex\nvar counter int\n\nfunc increment() {\n    mu.Lock()\n    defer mu.Unlock()\n    counter++\n}\n<\/code><\/pre>\n<p>\u4f7f\u7528 <code>go run -race<\/code> \u68c0\u6d4b\u7ade\u6001\u6761\u4ef6\u3002<\/p>\n<h3>\u6700\u4f73\u5b9e\u8df5\u6e05\u5355<\/h3>\n<ul>\n<li>\u4f7f\u7528 <code>sync.WaitGroup<\/code> \u7b49\u5f85\u4e00\u7ec4 Goroutine \u5b8c\u6210\u3002<\/li>\n<li>\u4f7f\u7528 <code>context<\/code> \u63a7\u5236\u8d85\u65f6\u548c\u53d6\u6d88\u3002<\/li>\n<li>\u9650\u5236\u5e76\u53d1\u6570\uff0c\u907f\u514d\u65e0\u9650\u5236\u521b\u5efa Goroutine\u3002<\/li>\n<li>\u4f7f\u7528 <code>select<\/code> \u5904\u7406\u591a Channel \u548c\u8d85\u65f6\u3002<\/li>\n<li>\u59cb\u7ec8\u5173\u95ed\u4e0d\u518d\u4f7f\u7528\u7684 Channel\uff08\u7531\u53d1\u9001\u65b9\u5173\u95ed\uff09\u3002<\/li>\n<li>\u4f7f\u7528 <code>-race<\/code> \u6807\u5fd7\u8fdb\u884c\u7ade\u6001\u68c0\u6d4b\u3002<\/li>\n<\/ul>\n<h2>\u603b\u7ed3<\/h2>\n<p>Go \u7684\u5e76\u53d1\u6a21\u578b\u7b80\u6d01\u800c\u5f3a\u5927\uff0c\u4f46\u6b63\u786e\u4f7f\u7528\u9700\u8981\u7406\u89e3\u5176\u673a\u5236\u548c\u9677\u9631\u3002\u901a\u8fc7\u672c\u6587\u7684\u793a\u4f8b\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u4f60\u53ef\u4ee5\u907f\u514d\u5e38\u89c1\u9519\u8bef\uff0c\u6784\u5efa\u9ad8\u6548\u53ef\u9760\u7684\u5e76\u53d1\u7a0b\u5e8f\u3002\u5efa\u8bae\u8fdb\u4e00\u6b65\u9605\u8bfb\u5b98\u65b9\u6587\u6863 <a href=\"https:\/\/golang.org\/doc\/effective_go\">Effective Go<\/a> \u548c <a href=\"https:\/\/blog.golang.org\/concurrency-patterns\">Go \u5e76\u53d1\u6a21\u5f0f<\/a>\u3002<\/p>\n<p><strong>\u4e0b\u4e00\u6b65<\/strong>\uff1a\u5c1d\u8bd5\u5c06 Worker Pool \u6a21\u5f0f\u5e94\u7528\u5230\u4f60\u7684\u9879\u76ee\u4e2d\uff0c\u6216\u63a2\u7d22 <code>errgroup<\/code> \u5305\u6765\u7b80\u5316\u9519\u8bef\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u6df1\u5165\u63a2\u8ba8 Go \u5e76\u53d1\u7f16\u7a0b\u7684\u6838\u5fc3\u673a\u5236\uff0c\u901a\u8fc7\u53ef\u8fd0\u884c\u7684\u4ee3\u7801\u793a\u4f8b\u5c55\u793a Goroutine\u3001Channel\u3001Select \u548c Context \u7684\u5b9e\u6218\u7528\u6cd5\uff0c\u5e76\u5206\u4eab\u907f\u514d\u5e38\u89c1\u9677\u9631\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u5e2e\u52a9\u8bfb\u8005\u6784\u5efa\u9ad8\u6548\u53ef\u9760\u7684\u5e76\u53d1\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[230,295,92,229,228],"class_list":["post-316","post","type-post","status-publish","format-standard","hentry","category-3","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\/316","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=316"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/316\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}