{"id":57638,"date":"2024-10-10T19:55:16","date_gmt":"2024-10-10T11:55:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57638\/"},"modified":"2024-10-10T19:55:16","modified_gmt":"2024-10-10T11:55:16","slug":"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80-goroutine%ef%bc%88%e8%bc%95%e9%87%8f%e7%b4%9a%e7%b7%9a%e7%a8%8b%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57638\/","title":{"rendered":"GO\u6559\u7a0b: Go \u8a9e\u8a00 goroutine\uff08\u8f15\u91cf\u7d1a\u7dda\u7a0b\uff09"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80-goroutine%ef%bc%88%e8%bc%95%e9%87%8f%e7%b4%9a%e7%b7%9a%e7%a8%8b%ef%bc%89-MjVRVBezZJ\">GO\u6559\u7a0b: Go \u8a9e\u8a00 goroutine\uff08\u8f15\u91cf\u7d1a\u7dda\u7a0b\uff09<\/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\uff08\u53c8\u7a31 Golang\uff09\u4ee5\u5176\u7c21\u6f54\u7684\u8a9e\u6cd5\u548c\u5f37\u5927\u7684\u4e26\u884c\u8655\u7406\u80fd\u529b\u800c\u53d7\u5230\u5ee3\u6cdb\u95dc\u6ce8\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u6df1\u5165\u63a2\u8a0e Go \u8a9e\u8a00\u4e2d\u7684 goroutine\uff0c\u9019\u662f\u4e00\u7a2e\u8f15\u91cf\u7d1a\u7684\u7dda\u7a0b\uff0c\u80fd\u5920\u6709\u6548\u5730\u5be6\u73fe\u4e26\u884c\u8655\u7406\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-goroutine%ef%bc%9f-MjVRVBezZJ\">\u4ec0\u9ebc\u662f goroutine\uff1f<\/h2>\n<p>goroutine \u662f Go \u8a9e\u8a00\u4e2d\u7528\u4f86\u5be6\u73fe\u4e26\u884c\u8655\u7406\u7684\u57fa\u672c\u55ae\u4f4d\u3002\u5b83\u662f\u4e00\u500b\u8f15\u91cf\u7d1a\u7684\u7dda\u7a0b\uff0c\u8207\u50b3\u7d71\u7684\u7dda\u7a0b\u76f8\u6bd4\uff0cgoroutine \u7684\u958b\u92b7\u8981\u5c0f\u5f97\u591a\u3002\u9019\u4f7f\u5f97\u958b\u767c\u8005\u53ef\u4ee5\u5728\u540c\u4e00\u500b\u61c9\u7528\u4e2d\u555f\u52d5\u6210\u5343\u4e0a\u842c\u7684 goroutine\uff0c\u800c\u4e0d\u6703\u5c0d\u7cfb\u7d71\u6027\u80fd\u9020\u6210\u986f\u8457\u5f71\u97ff\u3002<\/p>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-goroutine-MjVRVBezZJ\">\u5982\u4f55\u4f7f\u7528 goroutine<\/h2>\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\u51fd\u6578 <code>sayHello<\/code> \u88ab\u4f5c\u70ba\u4e00\u500b goroutine \u555f\u52d5\u3002\u7531\u65bc\u4e3b\u51fd\u6578\u5728\u7d50\u675f\u524d\u9700\u8981\u7b49\u5f85\u4e00\u6bb5\u6642\u9593\uff0c\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd goroutine \u6709\u8db3\u5920\u7684\u6642\u9593\u57f7\u884c\u3002<\/p>\n<h2 id=\"goroutine-%e7%9a%84%e7%89%b9%e9%bb%9e-MjVRVBezZJ\">goroutine \u7684\u7279\u9ede<\/h2>\n<ul>\n<li><strong>\u8f15\u91cf\u7d1a\uff1a<\/strong>goroutine \u7684\u5167\u5b58\u958b\u92b7\u975e\u5e38\u5c0f\uff0c\u901a\u5e38\u53ea\u9700\u5e7e KB \u7684\u5167\u5b58\u3002<\/li>\n<li><strong>\u81ea\u52d5\u7ba1\u7406\uff1a<\/strong>Go \u8a9e\u8a00\u7684\u904b\u884c\u6642\u7cfb\u7d71\u6703\u81ea\u52d5\u7ba1\u7406 goroutine \u7684\u8abf\u5ea6\uff0c\u958b\u767c\u8005\u7121\u9700\u624b\u52d5\u7ba1\u7406\u7dda\u7a0b\u3002<\/li>\n<li><strong>\u7c21\u55ae\u6613\u7528\uff1a<\/strong>\u555f\u52d5 goroutine \u7684\u8a9e\u6cd5\u975e\u5e38\u7c21\u55ae\uff0c\u50c5\u9700\u5728\u51fd\u6578\u8abf\u7528\u524d\u52a0\u4e0a <code>go<\/code> \u95dc\u9375\u5b57\u3002<\/li>\n<\/ul>\n<h2 id=\"goroutine-%e7%9a%84%e8%aa%bf%e5%ba%a6-MjVRVBezZJ\">goroutine \u7684\u8abf\u5ea6<\/h2>\n<p>Go \u8a9e\u8a00\u7684\u904b\u884c\u6642\u7cfb\u7d71\u4f7f\u7528 M:N \u8abf\u5ea6\u6a21\u578b\uff0c\u9019\u610f\u5473\u8457 M \u500b goroutine \u53ef\u4ee5\u5728 N \u500b\u64cd\u4f5c\u7cfb\u7d71\u7dda\u7a0b\u4e0a\u904b\u884c\u3002\u9019\u7a2e\u6a21\u578b\u4f7f\u5f97 goroutine \u7684\u8abf\u5ea6\u66f4\u52a0\u9ad8\u6548\uff0c\u80fd\u5920\u5145\u5206\u5229\u7528\u591a\u6838\u8655\u7406\u5668\u7684\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8%e9%80%9a%e9%81%93%ef%bc%88channel%ef%bc%89%e9%80%b2%e8%a1%8c%e9%80%9a%e4%bf%a1-MjVRVBezZJ\">\u4f7f\u7528\u901a\u9053\uff08Channel\uff09\u9032\u884c\u901a\u4fe1<\/h2>\n<p>\u5728\u4e26\u884c\u8655\u7406\u4e2d\uff0cgoroutine \u4e4b\u9593\u7684\u901a\u4fe1\u662f\u4e00\u500b\u91cd\u8981\u7684\u554f\u984c\u3002Go \u8a9e\u8a00\u63d0\u4f9b\u4e86\u901a\u9053\uff08channel\uff09\u4f86\u5be6\u73fe goroutine \u4e4b\u9593\u7684\u5b89\u5168\u901a\u4fe1\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528\u901a\u9053\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>package main\n\nimport (\n    \"fmt\"\n)\n\nfunc greet(c chan string) {\n    c &lt;- \"Hello from goroutine!\" \/\/ \u5c07\u6d88\u606f\u767c\u9001\u5230\u901a\u9053\n}\n\nfunc main() {\n    c := make(chan string) \/\/ \u5275\u5efa\u901a\u9053\n    go greet(c) \/\/ \u555f\u52d5 goroutine\n    msg := &lt;-c \/\/ \u5f9e\u901a\u9053\u63a5\u6536\u6d88\u606f\n    fmt.Println(msg)\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b\u901a\u9053 <code>c<\/code>\uff0c\u4e26\u5728 goroutine \u4e2d\u5c07\u6d88\u606f\u767c\u9001\u5230\u901a\u9053\u3002\u4e3b\u51fd\u6578\u5247\u5f9e\u901a\u9053\u63a5\u6536\u6d88\u606f\u4e26\u6253\u5370\u51fa\u4f86\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-MjVRVBezZJ\">\u7e3d\u7d50<\/h2>\n<p>goroutine \u662f Go \u8a9e\u8a00\u4e2d\u5be6\u73fe\u4e26\u884c\u8655\u7406\u7684\u6838\u5fc3\u7279\u6027\u4e4b\u4e00\u3002\u5b83\u7684\u8f15\u91cf\u7d1a\u7279\u6027\u548c\u7c21\u55ae\u7684\u8a9e\u6cd5\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u8f15\u9b06\u5730\u5be6\u73fe\u9ad8\u6548\u7684\u4e26\u884c\u7a0b\u5e8f\u3002\u901a\u904e\u4f7f\u7528\u901a\u9053\uff0cgoroutine \u4e4b\u9593\u7684\u901a\u4fe1\u4e5f\u8b8a\u5f97\u5b89\u5168\u800c\u7c21\u55ae\u3002\u96a8\u8457\u5c0d\u4e26\u884c\u8655\u7406\u9700\u6c42\u7684\u589e\u52a0\uff0c\u638c\u63e1 goroutine \u5c07\u5c0d\u958b\u767c\u8005\u7684\u5de5\u4f5c\u5927\u6709\u88e8\u76ca\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u670d\u52d9\u611f\u8208\u8da3\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u5404\u7a2e\u9700\u6c42\u3002\u7121\u8ad6\u662f\u958b\u767c\u74b0\u5883\u9084\u662f\u751f\u7522\u74b0\u5883\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u89e3\u6c7a\u65b9\u6848\u90fd\u80fd\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 Go \u8a9e\u8a00\u4e2d\u7684 goroutine\uff0c\u4e86\u89e3\u5982\u4f55\u5229\u7528\u8f15\u91cf\u7d1a\u7dda\u7a0b\u63d0\u5347\u4e26\u767c\u6027\u80fd\uff0c\u7c21\u5316\u591a\u4efb\u52d9\u8655\u7406\u7684\u5be6\u73fe\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-57638","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57638","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=57638"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57638\/revisions"}],"predecessor-version":[{"id":57639,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57638\/revisions\/57639"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}