{"id":57714,"date":"2024-10-10T20:35:19","date_gmt":"2024-10-10T12:35:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57714\/"},"modified":"2024-10-10T20:35:19","modified_gmt":"2024-10-10T12:35:19","slug":"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e4%b8%a6%e7%99%bc%e9%80%9a%e4%bf%a1","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57714\/","title":{"rendered":"GO\u6559\u7a0b: Go \u8a9e\u8a00\u4e26\u767c\u901a\u4fe1"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e4%b8%a6%e7%99%bc%e9%80%9a%e4%bf%a1-ErFZxQeMGG\">GO\u6559\u7a0b: Go \u8a9e\u8a00\u4e26\u767c\u901a\u4fe1<\/h1>\n<p>Go \u8a9e\u8a00\uff0c\u4f5c\u70ba\u4e00\u7a2e\u73fe\u4ee3\u7de8\u7a0b\u8a9e\u8a00\uff0c\u4ee5\u5176\u7c21\u6f54\u7684\u8a9e\u6cd5\u548c\u5f37\u5927\u7684\u4e26\u767c\u652f\u6301\u800c\u805e\u540d\u3002\u4e26\u767c\u662f\u6307\u540c\u6642\u57f7\u884c\u591a\u500b\u4efb\u52d9\u7684\u80fd\u529b\uff0c\u9019\u5728\u7576\u4eca\u7684\u8a08\u7b97\u74b0\u5883\u4e2d\u5c24\u70ba\u91cd\u8981\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e Go \u8a9e\u8a00\u7684\u4e26\u767c\u901a\u4fe1\u6a5f\u5236\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728 Go \u4e2d\u5be6\u73fe\u9ad8\u6548\u7684\u4e26\u767c\u7de8\u7a0b\u3002<\/p>\n<h2 id=\"go-%e8%aa%9e%e8%a8%80%e7%9a%84%e4%b8%a6%e7%99%bc%e6%a8%a1%e5%9e%8b-ErFZxQeMGG\">Go \u8a9e\u8a00\u7684\u4e26\u767c\u6a21\u578b<\/h2>\n<p>Go \u8a9e\u8a00\u7684\u4e26\u767c\u6a21\u578b\u57fa\u65bc\u201cgoroutine\u201d\u548c\u201cchannel\u201d\u3002<\/p>\n<h3 id=\"goroutine-ErFZxQeMGG\">Goroutine<\/h3>\n<p>Goroutine \u662f Go \u8a9e\u8a00\u4e2d\u8f15\u91cf\u7d1a\u7684\u57f7\u884c\u55ae\u5143\u3002\u8207\u50b3\u7d71\u7684\u7dda\u7a0b\u76f8\u6bd4\uff0cgoroutine \u7684\u958b\u92b7\u66f4\u5c0f\uff0c\u4e26\u4e14\u53ef\u4ee5\u5728\u540c\u4e00\u500b\u5730\u5740\u7a7a\u9593\u4e2d\u904b\u884c\u3002\u555f\u52d5\u4e00\u500b goroutine \u975e\u5e38\u7c21\u55ae\uff0c\u53ea\u9700\u4f7f\u7528 <code>go<\/code> \u95dc\u9375\u5b57\u5373\u53ef\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>go func() {\n    fmt.Println(\"Hello from goroutine!\")\n}()\n<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07\u5728\u65b0\u7684 goroutine \u4e2d\u57f7\u884c\uff0c\u4e26\u4e0d\u6703\u963b\u585e\u4e3b\u7a0b\u5e8f\u7684\u57f7\u884c\u3002<\/p>\n<h3 id=\"channel-ErFZxQeMGG\">Channel<\/h3>\n<p>Channel \u662f Go \u8a9e\u8a00\u4e2d\u7528\u65bc\u5728 goroutine \u4e4b\u9593\u9032\u884c\u901a\u4fe1\u7684\u7ba1\u9053\u3002\u901a\u904e channel\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5b89\u5168\u5730\u50b3\u905e\u6578\u64da\uff0c\u907f\u514d\u4e86\u4f7f\u7528\u9396\u7684\u8907\u96dc\u6027\u3002\u5275\u5efa\u4e00\u500b channel \u7684\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>ch := make(chan int)\n<\/code><\/pre>\n<p>\u8981\u5411 channel \u767c\u9001\u6578\u64da\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>&lt;-<\/code> \u64cd\u4f5c\u7b26\uff1a<\/p>\n<pre><code>ch &lt;- 42\n<\/code><\/pre>\n<p>\u8981\u5f9e channel \u63a5\u6536\u6578\u64da\uff0c\u540c\u6a23\u4f7f\u7528 <code>&lt;-<\/code> \u64cd\u4f5c\u7b26\uff1a<\/p>\n<pre><code>value := &lt;-ch\n<\/code><\/pre>\n<h2 id=\"%e4%bd%bf%e7%94%a8-channel-%e9%80%b2%e8%a1%8c%e4%b8%a6%e7%99%bc%e9%80%9a%e4%bf%a1%e7%9a%84%e7%af%84%e4%be%8b-ErFZxQeMGG\">\u4f7f\u7528 Channel \u9032\u884c\u4e26\u767c\u901a\u4fe1\u7684\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 goroutine \u548c channel \u9032\u884c\u4e26\u767c\u901a\u4fe1\uff1a<\/p>\n<pre><code>package main\n\nimport (\n    \"fmt\"\n    \"time\"\n)\n\nfunc worker(id int, ch chan int) {\n    time.Sleep(time.Second)\n    ch &lt;- id * 2\n}\n\nfunc main() {\n    ch := make(chan int)\n    for i := 1; i &lt;= 5; i++ {\n        go worker(i, ch)\n    }\n\n    for i := 1; i &lt;= 5; i++ {\n        result := &lt;-ch\n        fmt.Println(\"Received:\", result)\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e94\u500b goroutine\uff0c\u6bcf\u500b goroutine \u90fd\u6703\u5c07\u5176\u8a08\u7b97\u7d50\u679c\u767c\u9001\u5230 channel \u4e2d\u3002\u4e3b\u7a0b\u5e8f\u5247\u5f9e channel \u4e2d\u63a5\u6536\u7d50\u679c\u4e26\u6253\u5370\u51fa\u4f86\u3002<\/p>\n<h2 id=\"%e4%b8%a6%e7%99%bc%e7%9a%84%e5%84%aa%e5%8b%a2-ErFZxQeMGG\">\u4e26\u767c\u7684\u512a\u52e2<\/h2>\n<p>\u4f7f\u7528 Go \u8a9e\u8a00\u7684\u4e26\u767c\u7279\u6027\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u8f15\u9b06\u5730\u69cb\u5efa\u9ad8\u6548\u7684\u61c9\u7528\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4e26\u767c\u7684\u512a\u52e2\uff1a<\/p>\n<ul>\n<li><strong>\u6027\u80fd\u63d0\u5347\uff1a<\/strong>\u901a\u904e\u4e26\u884c\u8655\u7406\u591a\u500b\u4efb\u52d9\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/li>\n<li><strong>\u8cc7\u6e90\u5229\u7528\u7387\uff1a<\/strong>goroutine \u7684\u958b\u92b7\u5c0f\uff0c\u80fd\u5920\u5728\u6709\u9650\u7684\u8cc7\u6e90\u4e0b\u904b\u884c\u66f4\u591a\u7684\u4efb\u52d9\u3002<\/li>\n<li><strong>\u7c21\u5316\u4ee3\u78bc\uff1a<\/strong>\u4f7f\u7528 channel \u9032\u884c\u901a\u4fe1\uff0c\u6e1b\u5c11\u4e86\u5c0d\u9396\u7684\u9700\u6c42\uff0c\u4f7f\u4ee3\u78bc\u66f4\u7c21\u6f54\u6613\u61c2\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-ErFZxQeMGG\">\u7e3d\u7d50<\/h2>\n<p>Go \u8a9e\u8a00\u7684\u4e26\u767c\u901a\u4fe1\u6a5f\u5236\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u8f15\u9b06\u5730\u69cb\u5efa\u9ad8\u6548\u7684\u61c9\u7528\u7a0b\u5e8f\u3002\u901a\u904e\u4f7f\u7528 goroutine \u548c channel\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5be6\u73fe\u7c21\u55ae\u800c\u5f37\u5927\u7684\u4e26\u767c\u6a21\u578b\uff0c\u5f9e\u800c\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u53ef\u7dad\u8b77\u6027\u3002\u5982\u679c\u60a8\u5c0d\u65bc\u5982\u4f55\u5728\u9999\u6e2f\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u74b0\u5883\u4e2d\u904b\u884c Go \u61c9\u7528\u7a0b\u5e8f\u611f\u8208\u8da3\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 Go \u8a9e\u8a00\u7684\u4e26\u767c\u901a\u4fe1\uff0c\u5b78\u7fd2\u5982\u4f55\u6709\u6548\u5730\u4f7f\u7528 goroutines \u548c channels \u4f86\u63d0\u5347\u7a0b\u5f0f\u6027\u80fd\u8207\u6548\u7387\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-57714","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57714","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=57714"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57714\/revisions"}],"predecessor-version":[{"id":57715,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57714\/revisions\/57715"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}