{"id":57748,"date":"2024-10-10T20:50:25","date_gmt":"2024-10-10T12:50:25","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57748\/"},"modified":"2024-10-10T20:50:25","modified_gmt":"2024-10-10T12:50:25","slug":"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e5%a4%9a%e6%a0%b8%e4%b8%a6%e8%a1%8c%e5%8c%96","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57748\/","title":{"rendered":"GO\u6559\u7a0b: Go \u8a9e\u8a00\u591a\u6838\u4e26\u884c\u5316"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e5%a4%9a%e6%a0%b8%e4%b8%a6%e8%a1%8c%e5%8c%96-sfjLfEhpFq\">GO\u6559\u7a0b: Go \u8a9e\u8a00\u591a\u6838\u4e26\u884c\u5316<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u8a08\u7b97\u74b0\u5883\u4e2d\uff0c\u591a\u6838\u8655\u7406\u5668\u5df2\u7d93\u6210\u70ba\u6a19\u6e96\u914d\u7f6e\u3002\u96a8\u8457\u61c9\u7528\u7a0b\u5e8f\u9700\u6c42\u7684\u589e\u52a0\uff0c\u958b\u767c\u8005\u9700\u8981\u5229\u7528\u591a\u6838\u67b6\u69cb\u4f86\u63d0\u9ad8\u6027\u80fd\u3002Go \u8a9e\u8a00\uff08\u53c8\u7a31 Golang\uff09\u56e0\u5176\u5167\u5efa\u7684\u4e26\u884c\u8655\u7406\u7279\u6027\u800c\u53d7\u5230\u5ee3\u6cdb\u95dc\u6ce8\u3002\u672c\u6587\u5c07\u63a2\u8a0e Go \u8a9e\u8a00\u7684\u591a\u6838\u4e26\u884c\u5316\u7279\u6027\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u4f86\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7406\u89e3\u9019\u4e00\u6982\u5ff5\u3002<\/p>\n<h2 id=\"go-%e8%aa%9e%e8%a8%80%e7%9a%84%e4%b8%a6%e8%a1%8c%e8%99%95%e7%90%86%e6%a8%a1%e5%9e%8b-sfjLfEhpFq\">Go \u8a9e\u8a00\u7684\u4e26\u884c\u8655\u7406\u6a21\u578b<\/h2>\n<p>Go \u8a9e\u8a00\u7684\u4e26\u884c\u8655\u7406\u6a21\u578b\u57fa\u65bc Goroutines \u548c Channels\u3002Goroutines \u662f\u8f15\u91cf\u7d1a\u7684\u57f7\u884c\u55ae\u5143\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u555f\u52d5\u6578\u5343\u500b Goroutines\uff0c\u800c\u4e0d\u6703\u5c0d\u7cfb\u7d71\u8cc7\u6e90\u9020\u6210\u904e\u5927\u58d3\u529b\u3002Channels \u5247\u7528\u65bc\u5728 Goroutines \u4e4b\u9593\u9032\u884c\u901a\u4fe1\uff0c\u9019\u4f7f\u5f97\u6578\u64da\u5171\u4eab\u8b8a\u5f97\u66f4\u52a0\u5b89\u5168\u548c\u7c21\u55ae\u3002<\/p>\n<h3 id=\"goroutines-%e7%9a%84%e4%bd%bf%e7%94%a8-sfjLfEhpFq\">Goroutines \u7684\u4f7f\u7528<\/h3>\n<p>\u8981\u555f\u52d5\u4e00\u500b Goroutine\uff0c\u53ea\u9700\u5728\u51fd\u6578\u8abf\u7528\u524d\u52a0\u4e0a <code>go<\/code> \u95dc\u9375\u5b57\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>package main\n\nimport (\n    \"fmt\"\n    \"time\"\n)\n\nfunc sayHello() {\n    fmt.Println(\"Hello, World!\")\n}\n\nfunc main() {\n    go sayHello() \/\/ \u555f\u52d5\u4e00\u500b Goroutine\n    time.Sleep(1 * time.Second) \/\/ \u7b49\u5f85 Goroutine \u5b8c\u6210\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c<code>sayHello<\/code> \u51fd\u6578\u5728\u4e00\u500b\u65b0\u7684 Goroutine \u4e2d\u57f7\u884c\u3002\u7531\u65bc\u4e3b\u51fd\u6578\u5728\u7d50\u675f\u524d\u7b49\u5f85\u4e86\u4e00\u79d2\u9418\uff0c\u56e0\u6b64\u53ef\u4ee5\u78ba\u4fdd <code>sayHello<\/code> \u6709\u8db3\u5920\u7684\u6642\u9593\u57f7\u884c\u3002<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8-channels-%e9%80%b2%e8%a1%8c%e9%80%9a%e4%bf%a1-sfjLfEhpFq\">\u4f7f\u7528 Channels \u9032\u884c\u901a\u4fe1<\/h3>\n<p>Channels \u662f Go \u8a9e\u8a00\u4e2d\u7528\u65bc\u5728 Goroutines \u4e4b\u9593\u50b3\u905e\u6578\u64da\u7684\u4e3b\u8981\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528 Channels \u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>package main\n\nimport (\n    \"fmt\"\n)\n\nfunc calculateSquare(number int, ch chan int) {\n    ch &lt;- number * number \/\/ \u5c07\u7d50\u679c\u767c\u9001\u5230 Channel\n}\n\nfunc main() {\n    ch := make(chan int) \/\/ \u5275\u5efa\u4e00\u500b Channel\n\n    for i := 1; i &lt;= 5; i++ {\n        go calculateSquare(i, ch) \/\/ \u555f\u52d5 Goroutine\n    }\n\n    for i := 1; i &lt;= 5; i++ {\n        result := &lt;-ch \/\/ \u5f9e Channel \u63a5\u6536\u7d50\u679c\n        fmt.Println(\"Square:\", result)\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b Channel\uff0c\u4e26\u555f\u52d5\u4e86\u4e94\u500b Goroutines \u4f86\u8a08\u7b97\u6578\u5b57\u7684\u5e73\u65b9\u3002\u4e3b\u51fd\u6578\u5f9e Channel \u4e2d\u63a5\u6536\u7d50\u679c\u4e26\u6253\u5370\u51fa\u4f86\u3002<\/p>\n<h2 id=\"go-%e8%aa%9e%e8%a8%80%e7%9a%84%e4%b8%a6%e8%a1%8c%e5%8c%96%e5%84%aa%e5%8b%a2-sfjLfEhpFq\">Go \u8a9e\u8a00\u7684\u4e26\u884c\u5316\u512a\u52e2<\/h2>\n<ul>\n<li><strong>\u7c21\u55ae\u6613\u7528\uff1a<\/strong>Go \u8a9e\u8a00\u7684\u8a9e\u6cd5\u7c21\u6f54\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u555f\u52d5\u548c\u7ba1\u7406 Goroutines\u3002<\/li>\n<li><strong>\u9ad8\u6548\u6027\u80fd\uff1a<\/strong>Goroutines \u7684\u958b\u92b7\u975e\u5e38\u5c0f\uff0c\u80fd\u5920\u5728\u591a\u6838\u8655\u7406\u5668\u4e0a\u9ad8\u6548\u904b\u884c\u3002<\/li>\n<li><strong>\u5b89\u5168\u7684\u6578\u64da\u5171\u4eab\uff1a<\/strong>Channels \u63d0\u4f9b\u4e86\u4e00\u7a2e\u5b89\u5168\u7684\u65b9\u5f0f\u4f86\u5728 Goroutines \u4e4b\u9593\u5171\u4eab\u6578\u64da\uff0c\u907f\u514d\u4e86\u50b3\u7d71\u591a\u7dda\u7a0b\u7de8\u7a0b\u4e2d\u7684\u7af6\u722d\u689d\u4ef6\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-sfjLfEhpFq\">\u7d50\u8ad6<\/h2>\n<p>Go \u8a9e\u8a00\u7684\u591a\u6838\u4e26\u884c\u5316\u7279\u6027\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u5145\u5206\u5229\u7528\u73fe\u4ee3\u8655\u7406\u5668\u7684\u6027\u80fd\u3002\u901a\u904e\u4f7f\u7528 Goroutines \u548c Channels\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u69cb\u5efa\u9ad8\u6548\u7684\u4e26\u884c\u61c9\u7528\u7a0b\u5e8f\u3002\u96a8\u8457\u5c0d\u6027\u80fd\u8981\u6c42\u7684\u63d0\u9ad8\uff0cGo \u8a9e\u8a00\u7121\u7591\u5c07\u6210\u70ba\u672a\u4f86\u958b\u767c\u7684\u91cd\u8981\u5de5\u5177\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc\u5982\u4f55\u5728 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u4e0a\u904b\u884c Go \u61c9\u7528\u7a0b\u5e8f\u611f\u8208\u8da3\uff0c\u6216\u60f3\u4e86\u89e3\u66f4\u591a\u95dc\u65bc <a href=\"https:\/\/server.hk\">\u4e91\u670d\u52a1\u5668<\/a> \u7684\u4fe1\u606f\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u8cc7\u6e90\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 Go \u8a9e\u8a00\u7684\u591a\u6838\u4e26\u884c\u5316\u6280\u8853\uff0c\u5b78\u7fd2\u5982\u4f55\u6709\u6548\u5229\u7528\u591a\u6838\u8655\u7406\u5668\u63d0\u5347\u61c9\u7528\u6027\u80fd\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-57748","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57748","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=57748"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57748\/revisions"}],"predecessor-version":[{"id":57749,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57748\/revisions\/57749"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57748"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57748"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57748"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}