{"id":205597,"date":"2025-06-12T11:00:46","date_gmt":"2025-06-12T03:00:46","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205597\/"},"modified":"2025-06-12T11:00:46","modified_gmt":"2025-06-12T03:00:46","slug":"golang-%e5%87%bd%e6%95%b0%ef%bc%9agoroutine-%e4%b8%8e-channel-%e7%9a%84%e9%ad%94%e6%b3%95%e7%bb%84%e5%90%88","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205597\/","title":{"rendered":"Golang \u51fd\u6570\uff1agoroutine \u4e0e channel \u7684\u9b54\u6cd5\u7ec4\u5408"},"content":{"rendered":"<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aGOLANG \u51fd\u6570\uff1agoroutine \u4e0e channel \u7684\u9b54\u6cd5\u7ec4\u5408\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002,\u901a\u8fc7\u7ed3\u5408 goroutine \u548c channel\uff0cGo \u8bed\u8a00\u4e2d\u7684\u5e76\u53d1\u7f16\u7a0b\u53ef\u4ee5\u6784\u5efa\u9ad8\u6548\u7684\u5e94\u7528\u7a0b\u5e8f\u3002goroutine \u662f\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u53ef\u901a\u8fc7 go \u5173\u952e\u5b57\u521b\u5efa\u3002channel \u662f\u6570\u636e\u4ea4\u6362\u7ba1\u9053\uff0c\u53ef\u901a\u8fc7 make \u51fd\u6570\u521b\u5efa\u3002Goroutine \u53ef\u4ee5\u5411 channel \u53d1\u9001\u6570\u636e\uff0c\u800c\u5176\u4ed6 goroutine \u53ef\u4ee5\u4ece channel \u63a5\u6536\u6570\u636e\u3002\u6b64\u6280\u672f\u53ef\u7528\u4e8e\u5404\u79cd\u5e76\u53d1\u65b9\u6848\uff0c\u4f8b\u5982\u5e76\u884c\u5b57\u7b26\u4e32\u641c\u7d22\u3002\u5728\u5e76\u884c\u5b57\u7b26\u4e32\u641c\u7d22\u7684\u793a\u4f8b\u4e2d\uff0c\u591a\u4e2a goroutine \u5e76\u53d1\u641c\u7d22\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u5e76\u5728\u627e\u5230\u5339\u914d\u9879\u65f6\u901a\u8fc7 channel \u5c06\u7d22\u5f15\u53d1\u9001\u56de\u4e3b goroutine\u3002,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729928627671c9db3b6fd1.jpg\" class=\"aligncenter\">,<br \/>\n<strong>Go \u8bed\u8a00\u51fd\u6570\uff1a\u5229\u7528 goroutine \u548c channel \u7684\u5f3a\u5f3a\u8054\u624b<\/strong>,Go \u8bed\u8a00\u4e2d\uff0cgoroutine \u548c channel \u7684\u7ed3\u5408\u662f\u5e76\u53d1\u7f16\u7a0b\u7684\u57fa\u7840\u3002goroutine \u5141\u8bb8\u6211\u4eec\u5f00\u542f\u591a\u4e2a\u5e76\u53d1\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u800c channel \u5219\u4f5c\u4e3a\u6c9f\u901a\u548c\u6570\u636e\u4ea4\u6362\u7684\u7ba1\u9053\u3002\u5c06\u8fd9\u4e24\u4e2a\u7279\u6027\u7ed3\u5408\u8d77\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u6784\u5efa\u9ad8\u6548\u3001\u53ef\u4f38\u7f29\u7684\u5e76\u53d1\u5e94\u7528\u7a0b\u5e8f\u3002,<br \/>\n<strong>goroutine<\/strong>,goroutine \u662f Go \u8bed\u8a00\u4e2d\u7684\u8f7b\u91cf\u7ea7\u5e76\u53d1\u5e93\u3002\u4f7f\u7528<br \/>\n<code>go<\/code> \u5173\u952e\u5b57\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u4e00\u4e2a goroutine\uff1a,<br \/>\n<strong>channel<\/strong>,channel \u662f Go \u8bed\u8a00\u4e2d\u5b9e\u73b0\u5e76\u53d1\u6570\u636e\u4ea4\u6362\u7684\u7ba1\u9053\u3002\u5b83\u5141\u8bb8 goroutine \u4e4b\u95f4\u5b89\u5168\u5730\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<br \/>\n<code>make<\/code> \u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a channel\uff1a,<br \/>\n<strong>\u4f7f\u7528 goroutine \u548c channel<\/strong>,goroutine \u548c channel \u7684\u8054\u7528\u975e\u5e38\u7b80\u5355\u3002\u4e00\u4e2a goroutine \u53ef\u4ee5\u5411 channel \u53d1\u9001\u6570\u636e\uff0c\u800c\u53e6\u4e00\u4e2a goroutine \u53ef\u4ee5\u4ece channel \u4e2d\u63a5\u6536\u6570\u636e\uff1a,<br \/>\n<strong>\u5b9e\u6218\u6848\u4f8b<\/strong>,\u6211\u4eec\u4f7f\u7528 goroutine \u548c channel \u6765\u6784\u5efa\u4e00\u4e2a\u5e76\u53d1\u7684\u5b57\u7b26\u4e32\u641c\u7d22\u5e94\u7528\u7a0b\u5e8f\u3002\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528\u591a\u4e2a goroutine \u5e76\u53d1\u641c\u7d22\u5b57\u7b26\u4e32\u6570\u7ec4\u4e2d\u7684\u6307\u5b9a\u5b50\u5b57\u7b26\u4e32\u3002,\u8fd9\u4e2a\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u591a\u4e2a goroutine \u5e76\u53d1\u641c\u7d22\u5b57\u7b26\u4e32\u6570\u7ec4\u4e2d\u7684\u6307\u5b9a\u5b57\u7b26\u4e32\u3002\u6bcf\u4e2a goroutine \u5c06\u5728\u627e\u5230\u5339\u914d\u9879\u65f6\u5c06\u7d22\u5f15\u53d1\u9001\u5230 channel \u4e2d\u3002\u4e3b goroutine \u4ece channel \u4e2d\u63a5\u6536\u7d22\u5f15\uff0c\u5e76\u6253\u5370\u5339\u914d\u9879\u7684\u4f4d\u7f6e\u3002,\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aGOLANG \u51fd\u6570\uff1agoroutine \u4e0e channel \u7684\u9b54\u6cd5\u7ec4\u5408\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\u5427\uff01,\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aGOLANG \u51fd\u6570\uff1agoroutine \u4e0e channel \u7684\u9b54\u6cd5\u7ec4\u5408\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002,<br \/>\n<b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>GOLANG \u51fd\u6570\uff1agoroutine \u4e0e channel \u7684\u9b54\u6cd5\u7ec4\u5408<\/span><\/p>\n<h1>GOLANG \u51fd\u6570\uff1agoroutine \u4e0e channel \u7684\u9b54\u6cd5\u7ec4\u5408<\/h1>\n<p><span>2024-10-26 15:44:02<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aGOLANG \u51fd\u6570\uff1agoroutine \u4e0e channel \u7684\u9b54\u6cd5\u7ec4\u5408\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p>\u901a\u8fc7\u7ed3\u5408 goroutine \u548c channel\uff0cGo \u8bed\u8a00\u4e2d\u7684\u5e76\u53d1\u7f16\u7a0b\u53ef\u4ee5\u6784\u5efa\u9ad8\u6548\u7684\u5e94\u7528\u7a0b\u5e8f\u3002goroutine \u662f\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u53ef\u901a\u8fc7 go \u5173\u952e\u5b57\u521b\u5efa\u3002channel \u662f\u6570\u636e\u4ea4\u6362\u7ba1\u9053\uff0c\u53ef\u901a\u8fc7 make \u51fd\u6570\u521b\u5efa\u3002Goroutine \u53ef\u4ee5\u5411 channel \u53d1\u9001\u6570\u636e\uff0c\u800c\u5176\u4ed6 goroutine \u53ef\u4ee5\u4ece channel \u63a5\u6536\u6570\u636e\u3002\u6b64\u6280\u672f\u53ef\u7528\u4e8e\u5404\u79cd\u5e76\u53d1\u65b9\u6848\uff0c\u4f8b\u5982\u5e76\u884c\u5b57\u7b26\u4e32\u641c\u7d22\u3002\u5728\u5e76\u884c\u5b57\u7b26\u4e32\u641c\u7d22\u7684\u793a\u4f8b\u4e2d\uff0c\u591a\u4e2a goroutine \u5e76\u53d1\u641c\u7d22\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u5e76\u5728\u627e\u5230\u5339\u914d\u9879\u65f6\u901a\u8fc7 channel \u5c06\u7d22\u5f15\u53d1\u9001\u56de\u4e3b goroutine\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729928627671c9db3b6fd1.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Go \u8bed\u8a00\u51fd\u6570\uff1a\u5229\u7528 goroutine \u548c channel \u7684\u5f3a\u5f3a\u8054\u624b<\/strong><\/p>\n<p>Go \u8bed\u8a00\u4e2d\uff0cgoroutine \u548c channel \u7684\u7ed3\u5408\u662f\u5e76\u53d1\u7f16\u7a0b\u7684\u57fa\u7840\u3002goroutine \u5141\u8bb8\u6211\u4eec\u5f00\u542f\u591a\u4e2a\u5e76\u53d1\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u800c channel \u5219\u4f5c\u4e3a\u6c9f\u901a\u548c\u6570\u636e\u4ea4\u6362\u7684\u7ba1\u9053\u3002\u5c06\u8fd9\u4e24\u4e2a\u7279\u6027\u7ed3\u5408\u8d77\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u6784\u5efa\u9ad8\u6548\u3001\u53ef\u4f38\u7f29\u7684\u5e76\u53d1\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p><strong>goroutine<\/strong><\/p>\n<p>goroutine \u662f Go \u8bed\u8a00\u4e2d\u7684\u8f7b\u91cf\u7ea7\u5e76\u53d1\u5e93\u3002\u4f7f\u7528 <code>go<\/code> \u5173\u952e\u5b57\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u4e00\u4e2a goroutine\uff1a<\/p>\n<pre>go func() {\n    \/\/ \u5e76\u53d1\u6267\u884c\u7684\u4ee3\u7801\n}<\/pre>\n<p><strong>channel<\/strong><\/p>\n<p>channel \u662f Go \u8bed\u8a00\u4e2d\u5b9e\u73b0\u5e76\u53d1\u6570\u636e\u4ea4\u6362\u7684\u7ba1\u9053\u3002\u5b83\u5141\u8bb8 goroutine \u4e4b\u95f4\u5b89\u5168\u5730\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>make<\/code> \u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a channel\uff1a<\/p>\n<pre>ch := make(chan int)<\/pre>\n<p><strong>\u4f7f\u7528 goroutine \u548c channel<\/strong><\/p>\n<p>goroutine \u548c channel \u7684\u8054\u7528\u975e\u5e38\u7b80\u5355\u3002\u4e00\u4e2a goroutine \u53ef\u4ee5\u5411 channel \u53d1\u9001\u6570\u636e\uff0c\u800c\u53e6\u4e00\u4e2a goroutine \u53ef\u4ee5\u4ece channel \u4e2d\u63a5\u6536\u6570\u636e\uff1a<\/p>\n<pre>func producer(ch chan&lt;- int) {\n    for i := 0; i &lt; 10; i++ {\n        ch &lt;- i\n    }\n    close(ch)\n}\n\nfunc consumer(ch &lt;-chan int) {\n    for v := range ch {\n        fmt.Println(v)\n    }\n}\n\nfunc main() {\n    ch := make(chan int)\n    go producer(ch)\n    consumer(ch)\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u6211\u4eec\u4f7f\u7528 goroutine \u548c channel \u6765\u6784\u5efa\u4e00\u4e2a\u5e76\u53d1\u7684\u5b57\u7b26\u4e32\u641c\u7d22\u5e94\u7528\u7a0b\u5e8f\u3002\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528\u591a\u4e2a goroutine \u5e76\u53d1\u641c\u7d22\u5b57\u7b26\u4e32\u6570\u7ec4\u4e2d\u7684\u6307\u5b9a\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<pre>import (\n    \"fmt\"\n    \"sync\"\n)\n\nfunc search(str string, arr []string) (int, bool) {\n    for i, s := range arr {\n        if s == str {\n            return i, true\n        }\n    }\n    return -1, false\n}\n\nfunc main() {\n    arr := []string{\"a\", \"b\", \"c\", \"d\", \"e\", \"f\"}\n    ch := make(chan int, len(arr))\n    var wg sync.WaitGroup\n    wg.Add(len(arr))\n\n    for i, str := range arr {\n        go func(i int, str string) {\n            defer wg.Done()\n            idx, found := search(str, arr)\n            if found {\n                ch &lt;- idx\n            }\n        }(i, str)\n    }\n\n    wg.Wait()\n    close(ch)\n\n    for v := range ch {\n        fmt.Println(v)\n    }\n}<\/pre>\n<p>\u8fd9\u4e2a\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u591a\u4e2a goroutine \u5e76\u53d1\u641c\u7d22\u5b57\u7b26\u4e32\u6570\u7ec4\u4e2d\u7684\u6307\u5b9a\u5b57\u7b26\u4e32\u3002\u6bcf\u4e2a goroutine \u5c06\u5728\u627e\u5230\u5339\u914d\u9879\u65f6\u5c06\u7d22\u5f15\u53d1\u9001\u5230 channel \u4e2d\u3002\u4e3b goroutine \u4ece channel \u4e2d\u63a5\u6536\u7d22\u5f15\uff0c\u5e76\u6253\u5370\u5339\u914d\u9879\u7684\u4f4d\u7f6e\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aGOLANG \u51fd\u6570\uff1agoroutine \u4e0e channel \u7684\u9b54\u6cd5\u7ec4\u5408\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Java \u51fd\u6570\u6027\u80fd\u4f18\u5316\u4e0e\u4ee3\u7801\u53ef\u8bfb\u6027\u4e4b\u95f4\u7684\u6743\u8861\u6709\u54ea\u4e9b\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   PHP \u51fd\u6570\u4e2d\u5f15\u7528\u53c2\u6570\u4e0e\u53d8\u91cf\u4f5c\u7528\u57df\u7684\u5173\u7cfb\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aGOLANG&#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-205597","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205597","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=205597"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205597\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}