{"id":57750,"date":"2024-10-10T20:51:17","date_gmt":"2024-10-10T12:51:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57750\/"},"modified":"2024-10-10T20:51:17","modified_gmt":"2024-10-10T12:51:17","slug":"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e9%80%9a%e9%81%93%ef%bc%88chan%ef%bc%89-goroutine-%e4%b9%8b%e9%96%93%e9%80%9a%e4%bf%a1%e7%9a%84%e7%ae%a1%e9%81%93","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57750\/","title":{"rendered":"GO\u6559\u7a0b: Go \u8a9e\u8a00\u901a\u9053\uff08chan\uff09\u2014\u2014goroutine \u4e4b\u9593\u901a\u4fe1\u7684\u7ba1\u9053"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e9%80%9a%e9%81%93%ef%bc%88chan%ef%bc%89-goroutine-%e4%b9%8b%e9%96%93%e9%80%9a%e4%bf%a1%e7%9a%84%e7%ae%a1%e9%81%93-yrOTEKzbsK\">GO\u6559\u7a0b: Go \u8a9e\u8a00\u901a\u9053\uff08chan\uff09\u2014\u2014goroutine \u4e4b\u9593\u901a\u4fe1\u7684\u7ba1\u9053<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u8edf\u9ad4\u958b\u767c\u4e2d\uff0c\u4e26\u884c\u8655\u7406\u5df2\u6210\u70ba\u4e00\u500b\u91cd\u8981\u7684\u4e3b\u984c\u3002Go \u8a9e\u8a00\u4ee5\u5176\u7c21\u6f54\u7684\u8a9e\u6cd5\u548c\u5f37\u5927\u7684\u4e26\u884c\u8655\u7406\u80fd\u529b\u800c\u53d7\u5230\u5ee3\u6cdb\u95dc\u6ce8\u3002Go \u8a9e\u8a00\u4e2d\u7684\u901a\u9053\uff08channel\uff09\u662f\u5be6\u73fe goroutine \u4e4b\u9593\u901a\u4fe1\u7684\u6838\u5fc3\u6a5f\u5236\u4e4b\u4e00\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e Go \u8a9e\u8a00\u7684\u901a\u9053\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u7bc4\u4f8b\u4f86\u5e6b\u52a9\u8b80\u8005\u7406\u89e3\u5176\u7528\u6cd5\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e9%80%9a%e9%81%93%ef%bc%88channel%ef%bc%89%ef%bc%9f-yrOTEKzbsK\">\u4ec0\u9ebc\u662f\u901a\u9053\uff08channel\uff09\uff1f<\/h2>\n<p>\u901a\u9053\u662f Go \u8a9e\u8a00\u4e2d\u7528\u4f86\u5728 goroutine \u4e4b\u9593\u50b3\u905e\u6578\u64da\u7684\u7ba1\u9053\u3002\u5b83\u5141\u8a31\u4e00\u500b goroutine \u5c07\u6578\u64da\u767c\u9001\u5230\u53e6\u4e00\u500b goroutine\uff0c\u5f9e\u800c\u5be6\u73fe\u540c\u6b65\u548c\u901a\u4fe1\u3002\u901a\u9053\u7684\u4f7f\u7528\u4f7f\u5f97\u4e26\u884c\u7de8\u7a0b\u8b8a\u5f97\u66f4\u52a0\u7c21\u55ae\u548c\u5b89\u5168\uff0c\u56e0\u70ba\u5b83\u80fd\u5920\u81ea\u52d5\u8655\u7406\u6578\u64da\u7684\u50b3\u905e\u548c\u540c\u6b65\u554f\u984c\u3002<\/p>\n<h2 id=\"%e9%80%9a%e9%81%93%e7%9a%84%e5%9f%ba%e6%9c%ac%e7%94%a8%e6%b3%95-yrOTEKzbsK\">\u901a\u9053\u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>\u5728 Go \u8a9e\u8a00\u4e2d\uff0c\u901a\u9053\u7684\u8072\u660e\u548c\u4f7f\u7528\u975e\u5e38\u7c21\u55ae\u3002\u4ee5\u4e0b\u662f\u901a\u9053\u7684\u57fa\u672c\u7528\u6cd5\uff1a<\/p>\n<pre><code>package main\n\nimport (\n    \"fmt\"\n    \"time\"\n)\n\nfunc main() {\n    \/\/ \u5275\u5efa\u4e00\u500b\u6574\u6578\u985e\u578b\u7684\u901a\u9053\n    ch := make(chan int)\n\n    \/\/ \u555f\u52d5\u4e00\u500b goroutine\n    go func() {\n        for i := 0; i &lt; 5; i++ {\n            ch &lt;- i \/\/ \u5c07\u6578\u64da\u767c\u9001\u5230\u901a\u9053\n            time.Sleep(time.Second)\n        }\n        close(ch) \/\/ \u95dc\u9589\u901a\u9053\n    }()\n\n    \/\/ \u5f9e\u901a\u9053\u63a5\u6536\u6578\u64da\n    for num := range ch {\n        fmt.Println(num) \/\/ \u8f38\u51fa\u63a5\u6536\u5230\u7684\u6578\u64da\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b\u6574\u6578\u985e\u578b\u7684\u901a\u9053\uff0c\u4e26\u555f\u52d5\u4e86\u4e00\u500b goroutine \u4f86\u767c\u9001\u6578\u64da\u3002\u4e3b goroutine \u5247\u5f9e\u901a\u9053\u4e2d\u63a5\u6536\u6578\u64da\u4e26\u8f38\u51fa\u3002\u7576 goroutine \u5b8c\u6210\u6578\u64da\u767c\u9001\u5f8c\uff0c\u901a\u9053\u88ab\u95dc\u9589\uff0c\u9019\u6a23\u4e3b goroutine \u5c31\u53ef\u4ee5\u5b89\u5168\u5730\u7d50\u675f\u3002<\/p>\n<h2 id=\"%e9%80%9a%e9%81%93%e7%9a%84%e7%89%b9%e6%80%a7-yrOTEKzbsK\">\u901a\u9053\u7684\u7279\u6027<\/h2>\n<p>\u901a\u9053\u6709\u5e7e\u500b\u91cd\u8981\u7684\u7279\u6027\uff1a<\/p>\n<ul>\n<li><strong>\u985e\u578b\u5b89\u5168\uff1a<\/strong>\u901a\u9053\u662f\u985e\u578b\u5b89\u5168\u7684\uff0c\u9019\u610f\u5473\u8457\u4f60\u53ea\u80fd\u901a\u904e\u901a\u9053\u767c\u9001\u548c\u63a5\u6536\u7279\u5b9a\u985e\u578b\u7684\u6578\u64da\u3002<\/li>\n<li><strong>\u963b\u585e\u884c\u70ba\uff1a<\/strong>\u7576\u4e00\u500b goroutine \u5617\u8a66\u5f9e\u4e00\u500b\u7a7a\u7684\u901a\u9053\u63a5\u6536\u6578\u64da\u6642\uff0c\u5b83\u6703\u88ab\u963b\u585e\uff0c\u76f4\u5230\u6709\u6578\u64da\u53ef\u7528\u3002\u540c\u6a23\uff0c\u7576\u4e00\u500b goroutine \u5617\u8a66\u5411\u4e00\u500b\u6eff\u7684\u901a\u9053\u767c\u9001\u6578\u64da\u6642\uff0c\u5b83\u4e5f\u6703\u88ab\u963b\u585e\uff0c\u76f4\u5230\u6709\u7a7a\u9593\u53ef\u7528\u3002<\/li>\n<li><strong>\u96d9\u5411\u901a\u9053\uff1a<\/strong>\u901a\u9053\u53ef\u4ee5\u662f\u96d9\u5411\u7684\uff0c\u9019\u610f\u5473\u8457\u4f60\u53ef\u4ee5\u5728\u540c\u4e00\u901a\u9053\u4e0a\u540c\u6642\u767c\u9001\u548c\u63a5\u6536\u6578\u64da\u3002<\/li>\n<\/ul>\n<h2 id=\"%e9%81%b8%e6%93%87%e9%80%9a%e9%81%93%ef%bc%88select%ef%bc%89-yrOTEKzbsK\">\u9078\u64c7\u901a\u9053\uff08select\uff09<\/h2>\n<p>Go \u8a9e\u8a00\u9084\u63d0\u4f9b\u4e86\u4e00\u500b\u5f37\u5927\u7684\u9078\u64c7\u8a9e\u53e5\uff08select\uff09\uff0c\u53ef\u4ee5\u7528\u4f86\u540c\u6642\u76e3\u807d\u591a\u500b\u901a\u9053\u3002\u9019\u4f7f\u5f97\u8655\u7406\u591a\u500b goroutine \u7684\u901a\u4fe1\u8b8a\u5f97\u66f4\u52a0\u9748\u6d3b\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528 select \u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>package main\n\nimport (\n    \"fmt\"\n    \"time\"\n)\n\nfunc main() {\n    ch1 := make(chan string)\n    ch2 := make(chan string)\n\n    go func() {\n        time.Sleep(2 * time.Second)\n        ch1 &lt;- \"\u4f86\u81ea\u901a\u90531\"\n    }()\n\n    go func() {\n        time.Sleep(1 * time.Second)\n        ch2 &lt;- \"\u4f86\u81ea\u901a\u90532\"\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>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u5169\u500b\u901a\u9053\uff0c\u4e26\u555f\u52d5\u4e86\u5169\u500b goroutine\u3002\u4f7f\u7528 select \u8a9e\u53e5\uff0c\u6211\u5011\u53ef\u4ee5\u540c\u6642\u76e3\u807d\u9019\u5169\u500b\u901a\u9053\uff0c\u4e26\u6839\u64da\u54ea\u500b\u901a\u9053\u5148\u63a5\u6536\u5230\u6578\u64da\u4f86\u57f7\u884c\u76f8\u61c9\u7684\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-yrOTEKzbsK\">\u7e3d\u7d50<\/h2>\n<p>Go \u8a9e\u8a00\u7684\u901a\u9053\uff08chan\uff09\u662f\u5be6\u73fe goroutine \u4e4b\u9593\u901a\u4fe1\u7684\u91cd\u8981\u5de5\u5177\u3002\u5b83\u4e0d\u50c5\u7c21\u5316\u4e86\u4e26\u884c\u7de8\u7a0b\u7684\u8907\u96dc\u6027\uff0c\u9084\u63d0\u4f9b\u4e86\u985e\u578b\u5b89\u5168\u548c\u963b\u585e\u884c\u70ba\u7b49\u7279\u6027\uff0c\u4f7f\u5f97\u6578\u64da\u50b3\u905e\u66f4\u52a0\u5b89\u5168\u548c\u9ad8\u6548\u3002\u901a\u904e\u4f7f\u7528\u901a\u9053\u548c select \u8a9e\u53e5\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u9748\u6d3b\u5730\u8655\u7406\u591a\u500b goroutine \u4e4b\u9593\u7684\u901a\u4fe1\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc\u4f7f\u7528 Go \u8a9e\u8a00\u9032\u884c\u4e26\u884c\u7de8\u7a0b\u611f\u8208\u8da3\uff0c\u4e26\u5e0c\u671b\u5728\u9999\u6e2f\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u5e6b\u52a9\u60a8\u8f15\u9b06\u90e8\u7f72\u548c\u7ba1\u7406\u60a8\u7684\u61c9\u7528\u7a0b\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 Go \u8a9e\u8a00\u4e2d\u7684\u901a\u9053\uff08chan\uff09\uff0c\u4e86\u89e3\u5982\u4f55\u5be6\u73fe goroutine \u4e4b\u9593\u7684\u9ad8\u6548\u901a\u4fe1\uff0c\u63d0\u5347\u4e26\u767c\u7de8\u7a0b\u7684\u6548\u7387\u8207\u7c21\u6f54\u6027\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-57750","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57750","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=57750"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57750\/revisions"}],"predecessor-version":[{"id":57751,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57750\/revisions\/57751"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}