{"id":255,"date":"2026-09-16T10:19:56","date_gmt":"2026-09-16T02:19:56","guid":{"rendered":"https:\/\/yykcj.com\/?p=255"},"modified":"2026-09-16T10:19:56","modified_gmt":"2026-09-16T02:19:56","slug":"go-%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%e9%ab%98%e5%b9%b6%e5%8f%91%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=255","title":{"rendered":"Go \u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\uff1a\u4ece goroutine \u5230\u9ad8\u5e76\u53d1\u6a21\u5f0f"},"content":{"rendered":"<h2>\u4e3a\u4ec0\u4e48 Go \u7684\u5e76\u53d1\u5982\u6b64\u7279\u522b\uff1f<\/h2>\n<p>\u5728\u4f20\u7edf\u8bed\u8a00\u4e2d\uff0c\u5e76\u53d1\u7f16\u7a0b\u5f80\u5f80\u610f\u5473\u7740\u590d\u6742\u7684\u7ebf\u7a0b\u7ba1\u7406\u548c\u9501\u7ade\u4e89\u3002\u800c Go \u4ece\u8bed\u8a00\u5c42\u9762\u5185\u7f6e\u4e86\u5e76\u53d1\u539f\u8bed\uff1a<strong>goroutine<\/strong> \u548c <strong>channel<\/strong>\uff0c\u8ba9\u5e76\u53d1\u53d8\u5f97\u8f7b\u91cf\u4e14\u4f18\u96c5\u3002\u4f46\u8fd9\u4e5f\u5e26\u6765\u4e86\u65b0\u7684\u6311\u6218\uff1a\u5982\u4f55\u6b63\u786e\u540c\u6b65\u3001\u5982\u4f55\u907f\u514d goroutine \u6cc4\u6f0f\u3001\u5982\u4f55\u9009\u62e9\u9501\u8fd8\u662f channel\uff1f<\/p>\n<p>\u672c\u6587\u5c06\u901a\u8fc7\u4e00\u4e2a\u5b8c\u6574\u7684\u5b9e\u6218\u6848\u4f8b\u2014\u2014\u5e76\u53d1\u722c\u866b\uff08\u6a21\u62df\uff09\uff0c\u5e26\u4f60\u638c\u63e1 Go \u5e76\u53d1\u7f16\u7a0b\u7684\u6838\u5fc3\u6280\u80fd\u3002<\/p>\n<h2>\u73af\u5883\u51c6\u5907<\/h2>\n<ul>\n<li>Go 1.20+\uff08\u672c\u6587\u4f7f\u7528 Go 1.21\uff09<\/li>\n<li>\u4efb\u610f\u7f16\u8f91\u5668<\/li>\n<\/ul>\n<p>\u521b\u5efa\u4e00\u4e2a\u65b0\u76ee\u5f55\u5e76\u521d\u59cb\u5316\u6a21\u5757\uff1a<\/p>\n<pre><code class=\"language-bash\">\nmkdir go-concurrency-demo &amp;&amp; cd go-concurrency-demo\ngo mod init demo\n<\/code><\/pre>\n<h2>\u7b2c\u4e00\u6b65\uff1a\u7406\u89e3 goroutine \u4e0e WaitGroup<\/h2>\n<p><strong>goroutine<\/strong> \u662f\u7531 Go \u8fd0\u884c\u65f6\u7ba1\u7406\u7684\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u542f\u52a8\u4e00\u4e2a goroutine \u53ea\u9700 <code>go<\/code> \u5173\u952e\u5b57\u3002<\/p>\n<p>\u4f46\u4e3b goroutine \u4e0d\u4f1a\u7b49\u5f85\u5176\u4ed6 goroutine \u7ed3\u675f\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981 <code>sync.WaitGroup<\/code> \u6765\u540c\u6b65\u3002<\/p>\n<pre><code class=\"language-go\">\npackage main\n\nimport (\n    &quot;fmt&quot;\n    &quot;sync&quot;\n    &quot;time&quot;\n)\n\nfunc worker(id int, wg *sync.WaitGroup) {\n    defer wg.Done() \/\/ \u786e\u4fdd\u8ba1\u6570\u5668\u51cf\u4e00\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    var wg sync.WaitGroup\n\n    for i := 1; i &lt;= 3; i++ {\n        wg.Add(1) \/\/ \u5fc5\u987b\u5728\u542f\u52a8 goroutine \u524d\u8c03\u7528\n        go worker(i, &amp;wg)\n    }\n\n    wg.Wait() \/\/ \u963b\u585e\u76f4\u5230\u6240\u6709 worker \u5b8c\u6210\n    fmt.Println(&quot;All workers finished&quot;)\n}\n<\/code><\/pre>\n<p><strong>\u6ce8\u610f<\/strong>\uff1a<code>wg.Add(1)<\/code> \u5fc5\u987b\u5728 <code>go<\/code> \u8bed\u53e5\u4e4b\u524d\u8c03\u7528\uff0c\u5426\u5219\u53ef\u80fd\u4e3b goroutine \u5df2\u7ecf\u6267\u884c\u5230 <code>wg.Wait()<\/code> \u800c worker \u8fd8\u6ca1\u542f\u52a8\uff0c\u5bfc\u81f4 panic\u3002<\/p>\n<h2>\u7b2c\u4e8c\u6b65\uff1achannel \u57fa\u7840\u4e0e\u65b9\u5411<\/h2>\n<p>Channel \u662f goroutine \u4e4b\u95f4\u7684\u901a\u4fe1\u7ba1\u9053\u3002\u9ed8\u8ba4\u662f\u53cc\u5411\u7684\uff0c\u4f46\u53ef\u4ee5\u6307\u5b9a\u65b9\u5411\u63d0\u9ad8\u4ee3\u7801\u5b89\u5168\u6027\u3002<\/p>\n<pre><code class=\"language-go\">\nfunc producer(ch chan&lt;- int) { \/\/ \u53ea\u53d1\u9001\n    for i := 0; i &lt; 5; i++ {\n        ch &lt;- i\n    }\n    close(ch) \/\/ \u53d1\u9001\u65b9\u5173\u95ed channel\n}\n\nfunc consumer(ch &lt;-chan int) { \/\/ \u53ea\u63a5\u6536\n    for v := range ch {\n        fmt.Println(&quot;Received:&quot;, v)\n    }\n}\n\nfunc main() {\n    ch := make(chan int)\n    go producer(ch)\n    consumer(ch)\n}\n<\/code><\/pre>\n<p><strong>\ud83d\udca1 \u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a\u6c38\u8fdc\u7531\u53d1\u9001\u65b9\u5173\u95ed channel\uff0c\u63a5\u6536\u65b9\u4e0d\u8981\u5173\u95ed\u3002\u5411\u5df2\u5173\u95ed\u7684 channel \u53d1\u9001\u6570\u636e\u4f1a panic\u3002<\/p>\n<h2>\u7b2c\u4e09\u6b65\uff1a\u5e26\u7f13\u51b2\u7684 channel \u4e0e select<\/h2>\n<p>\u65e0\u7f13\u51b2 channel \u662f\u540c\u6b65\u7684\uff0c\u53d1\u9001\u548c\u63a5\u6536\u5fc5\u987b\u540c\u65f6\u51c6\u5907\u597d\u3002\u5e26\u7f13\u51b2\u7684 channel \u53ef\u4ee5\u5f02\u6b65\uff0c\u4f46\u8981\u6ce8\u610f\u7f13\u51b2\u533a\u6ee1\u65f6\u53d1\u9001\u4f1a\u963b\u585e\u3002<\/p>\n<p><code>select<\/code> \u53ef\u4ee5\u540c\u65f6\u7b49\u5f85\u591a\u4e2a channel \u64cd\u4f5c\uff0c\u7c7b\u4f3c switch \u4f46\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\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>\u6ce8\u610f<\/strong>\uff1aselect \u5982\u679c\u6ca1\u6709 default \u5206\u652f\uff0c\u4f1a\u4e00\u76f4\u963b\u585e\u76f4\u5230\u67d0\u4e2a case \u5c31\u7eea\u3002\u5982\u679c\u6240\u6709 channel \u90fd\u963b\u585e\u4e14\u6ca1\u6709 default\uff0c\u5219 select \u6c38\u4e45\u963b\u585e\u3002<\/p>\n<h2>\u7b2c\u56db\u6b65\uff1a\u4e92\u65a5\u9501\u4e0e\u7ade\u6001\u68c0\u6d4b<\/h2>\n<p>\u5f53\u591a\u4e2a goroutine \u8bbf\u95ee\u5171\u4eab\u53d8\u91cf\u65f6\uff0c\u5fc5\u987b\u52a0\u9501\u3002Go \u63d0\u4f9b\u4e86 <code>sync.Mutex<\/code> \u548c <code>sync.RWMutex<\/code>\u3002<\/p>\n<pre><code class=\"language-go\">\nvar counter int\nvar mu sync.Mutex\n\nfunc increment(wg *sync.WaitGroup) {\n    defer wg.Done()\n    for i := 0; i &lt; 1000; i++ {\n        mu.Lock()\n        counter++\n        mu.Unlock()\n    }\n}\n\nfunc main() {\n    var wg sync.WaitGroup\n    for i := 0; i &lt; 10; i++ {\n        wg.Add(1)\n        go increment(&amp;wg)\n    }\n    wg.Wait()\n    fmt.Println(&quot;Counter:&quot;, counter) \/\/ \u5e94\u4e3a 10000\n}\n<\/code><\/pre>\n<p><strong>\u68c0\u6d4b\u7ade\u6001<\/strong>\uff1a\u4f7f\u7528 <code>go run -race main.go<\/code> \u53ef\u4ee5\u68c0\u6d4b\u6570\u636e\u7ade\u4e89\uff0c\u5f3a\u70c8\u5efa\u8bae\u5728\u6d4b\u8bd5\u65f6\u5f00\u542f\u3002<\/p>\n<h2>\u7b2c\u4e94\u6b65\uff1a\u5b9e\u6218\u2014\u2014\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u578b<\/h2>\n<p>\u8fd9\u662f\u4e00\u4e2a\u7ecf\u5178\u5e76\u53d1\u6a21\u5f0f\uff0c\u4f7f\u7528\u5e26\u7f13\u51b2 channel \u89e3\u8026\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u3002<\/p>\n<pre><code class=\"language-go\">\npackage main\n\nimport (\n    &quot;fmt&quot;\n    &quot;math\/rand&quot;\n    &quot;sync&quot;\n    &quot;time&quot;\n)\n\nfunc producer(id int, ch chan&lt;- int, wg *sync.WaitGroup) {\n    defer wg.Done()\n    for i := 0; i &lt; 5; i++ {\n        num := rand.Intn(100)\n        fmt.Printf(&quot;Producer %d produced: %d\\n&quot;, id, num)\n        ch &lt;- num\n        time.Sleep(time.Millisecond * 200)\n    }\n}\n\nfunc consumer(id int, ch &lt;-chan int, wg *sync.WaitGroup) {\n    defer wg.Done()\n    for num := range ch {\n        fmt.Printf(&quot;Consumer %d consumed: %d\\n&quot;, id, num)\n        time.Sleep(time.Millisecond * 300)\n    }\n}\n\nfunc main() {\n    ch := make(chan int, 10) \/\/ \u7f13\u51b2\u533a\u5927\u5c0f\u4e3a 10\n    var wg sync.WaitGroup\n\n    \/\/ \u542f\u52a8 2 \u4e2a\u751f\u4ea7\u8005\n    for i := 1; i &lt;= 2; i++ {\n        wg.Add(1)\n        go producer(i, ch, &amp;wg)\n    }\n\n    \/\/ \u542f\u52a8 3 \u4e2a\u6d88\u8d39\u8005\n    var consumerWg sync.WaitGroup\n    for i := 1; i &lt;= 3; i++ {\n        consumerWg.Add(1)\n        go consumer(i, ch, &amp;consumerWg)\n    }\n\n    \/\/ \u7b49\u5f85\u6240\u6709\u751f\u4ea7\u8005\u5b8c\u6210\uff0c\u7136\u540e\u5173\u95ed channel\n    wg.Wait()\n    close(ch)\n\n    \/\/ \u7b49\u5f85\u6240\u6709\u6d88\u8d39\u8005\u5b8c\u6210\n    consumerWg.Wait()\n    fmt.Println(&quot;All done&quot;)\n}\n<\/code><\/pre>\n<p><strong>\u5173\u952e\u70b9<\/strong>\uff1a<\/p>\n<ul>\n<li>\u751f\u4ea7\u8005\u5b8c\u6210\u540e\u5173\u95ed channel\uff0c\u6d88\u8d39\u8005\u901a\u8fc7 <code>range<\/code> \u81ea\u52a8\u68c0\u6d4b\u5173\u95ed\u3002<\/li>\n<li>\u4f7f\u7528\u4e24\u4e2a WaitGroup \u5206\u522b\u7ba1\u7406\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\uff0c\u907f\u514d\u8fc7\u65e9\u5173\u95ed channel\u3002<\/li>\n<\/ul>\n<h2>\u7b2c\u516d\u6b65\uff1a\u8d85\u65f6\u63a7\u5236\u4e0e context<\/h2>\n<p>\u5e76\u53d1\u64cd\u4f5c\u7ecf\u5e38\u9700\u8981\u8d85\u65f6\u63a7\u5236\u3002<code>context<\/code> \u5305\u662f\u6807\u51c6\u505a\u6cd5\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    result := make(chan string)\n    go func() {\n        time.Sleep(3 * time.Second) \/\/ \u6a21\u62df\u8017\u65f6\u64cd\u4f5c\n        result &lt;- &quot;done&quot;\n    }()\n\n    select {\n    case res := &lt;-result:\n        fmt.Println(res)\n    case &lt;-ctx.Done():\n        fmt.Println(&quot;Timeout:&quot;, ctx.Err())\n    }\n}\n<\/code><\/pre>\n<p><strong>\ud83d\udca1 \u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a\u5c06 <code>ctx<\/code> \u4f5c\u4e3a\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4f20\u9012\uff0c\u5e76\u5728\u6240\u6709\u963b\u585e\u64cd\u4f5c\u4e2d\u76d1\u542c <code>ctx.Done()<\/code>\u3002<\/p>\n<h2>\u5e38\u89c1\u5751\u4e0e\u907f\u5751\u6307\u5357<\/h2>\n<ol>\n<li><strong>goroutine \u6cc4\u6f0f<\/strong>\uff1a\u542f\u52a8\u7684 goroutine \u5982\u679c\u6c38\u8fdc\u963b\u585e\u5728 channel \u4e0a\uff0c\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002\u4f7f\u7528 <code>context<\/code> \u6216 <code>select<\/code> \u52a0\u8d85\u65f6\u6765\u907f\u514d\u3002<\/li>\n<li><strong>\u6b7b\u9501<\/strong>\uff1a\u4f8b\u5982\u4e24\u4e2a goroutine \u4e92\u76f8\u7b49\u5f85\u5bf9\u65b9\u53d1\u9001\u6570\u636e\u3002\u4f7f\u7528 <code>go run -race<\/code> \u548c\u4ed4\u7ec6\u8bbe\u8ba1 channel \u65b9\u5411\u6765\u9884\u9632\u3002<\/li>\n<li><strong>\u5173\u95ed\u5df2\u5173\u95ed\u7684 channel<\/strong>\uff1a\u4f1a panic\u3002\u53ea\u7531\u53d1\u9001\u65b9\u5173\u95ed\uff0c\u4e14\u786e\u4fdd\u53ea\u5173\u95ed\u4e00\u6b21\u3002<\/li>\n<li><strong>\u9501\u7684\u7c92\u5ea6<\/strong>\uff1a\u9501\u7684\u4e34\u754c\u533a\u5e94\u5c3d\u53ef\u80fd\u5c0f\uff0c\u4f46\u4e5f\u8981\u907f\u514d\u8fc7\u5ea6\u52a0\u9501\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\u3002<\/li>\n<\/ol>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u672c\u6587\u4ece goroutine \u548c WaitGroup \u51fa\u53d1\uff0c\u9010\u6b65\u6df1\u5165\u5230 channel\u3001select\u3001\u9501\u3001context\uff0c\u6700\u540e\u5b9e\u73b0\u4e86\u4e00\u4e2a\u5b8c\u6574\u7684\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u578b\u3002\u638c\u63e1\u8fd9\u4e9b\u6838\u5fc3\u6982\u5ff5\u540e\uff0c\u4f60\u53ef\u4ee5\u5e94\u5bf9\u5927\u591a\u6570\u5e76\u53d1\u573a\u666f\u3002<\/p>\n<p><strong>\u4e0b\u4e00\u6b65\u65b9\u5411<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5b66\u4e60 <code>sync.Pool<\/code> \u51cf\u5c11\u5185\u5b58\u5206\u914d<\/li>\n<li>\u4e86\u89e3 <code>errgroup<\/code> \u5305\u7ba1\u7406 goroutine \u9519\u8bef<\/li>\n<li>\u7814\u7a76 Go \u8c03\u5ea6\u5668\u539f\u7406\uff08GMP \u6a21\u578b\uff09<\/li>\n<\/ul>\n<p>\u5e76\u53d1\u7f16\u7a0b\u7684\u6838\u5fc3\u662f<strong>\u5171\u4eab\u5185\u5b58\u901a\u8fc7\u901a\u4fe1\u6765\u5b9e\u73b0<\/strong>\uff0c\u7262\u8bb0\u8fd9\u4e00\u54f2\u5b66\uff0c\u4f60\u7684 Go \u7a0b\u5e8f\u4f1a\u66f4\u52a0\u5065\u58ee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u6df1\u5165\u8bb2\u89e3 Go \u5e76\u53d1\u7f16\u7a0b\u7684\u6838\u5fc3\u673a\u5236\uff0c\u5305\u62ec goroutine\u3001channel\u3001sync \u5305\u53ca\u5e38\u89c1\u5e76\u53d1\u6a21\u5f0f\u3002\u901a\u8fc7\u5b9e\u6218\u4ee3\u7801\u6f14\u793a\u5982\u4f55\u907f\u514d\u7ade\u6001\u6761\u4ef6\u3001\u6b7b\u9501\uff0c\u5e76\u6784\u5efa\u9ad8\u6548\u7684\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u578b\uff0c\u52a9\u4f60\u5199\u51fa\u7a33\u5b9a\u9ad8\u5e76\u53d1\u7684 Go \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":[4],"tags":[230,92,229,66,228],"class_list":["post-255","post","type-post","status-publish","format-standard","hentry","category-4","tag-channel","tag-go","tag-goroutine","tag-66","tag-228"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/255","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=255"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/255\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}