{"id":57660,"date":"2024-10-10T20:05:15","date_gmt":"2024-10-10T12:05:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57660\/"},"modified":"2024-10-10T20:05:15","modified_gmt":"2024-10-10T12:05:15","slug":"go%e6%95%99%e7%a8%8b-goroutine-%e5%92%8c-coroutine-%e7%9a%84%e5%8d%80%e5%88%a5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57660\/","title":{"rendered":"GO\u6559\u7a0b: goroutine \u548c coroutine \u7684\u5340\u5225"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-goroutine-%e5%92%8c-coroutine-%e7%9a%84%e5%8d%80%e5%88%a5-MZuxLHtSaI\">GO\u6559\u7a0b: goroutine \u548c coroutine \u7684\u5340\u5225<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u8edf\u9ad4\u958b\u767c\u4e2d\uff0c\u4e26\u884c\u8655\u7406\u548c\u975e\u540c\u6b65\u7de8\u7a0b\u5df2\u6210\u70ba\u63d0\u5347\u61c9\u7528\u6548\u80fd\u7684\u95dc\u9375\u6280\u8853\u3002Go \u8a9e\u8a00\uff08\u901a\u5e38\u7a31\u70ba Golang\uff09\u4ee5\u5176\u7368\u7279\u7684 goroutine \u6a5f\u5236\u800c\u805e\u540d\uff0c\u9019\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u8f15\u9b06\u5730\u5be6\u73fe\u4e26\u884c\u8655\u7406\u3002\u53e6\u4e00\u65b9\u9762\uff0ccoroutine \u662f\u53e6\u4e00\u7a2e\u6d41\u884c\u7684\u975e\u540c\u6b65\u7de8\u7a0b\u6280\u8853\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u591a\u7a2e\u7de8\u7a0b\u8a9e\u8a00\u4e2d\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e goroutine \u548c coroutine \u4e4b\u9593\u7684\u5340\u5225\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-goroutine%ef%bc%9f-MZuxLHtSaI\">\u4ec0\u9ebc\u662f goroutine\uff1f<\/h2>\n<p>goroutine \u662f Go \u8a9e\u8a00\u4e2d\u7684\u4e00\u7a2e\u8f15\u91cf\u7d1a\u57f7\u884c\u55ae\u5143\u3002\u5b83\u5011\u7531 Go \u7684\u904b\u884c\u6642\u7cfb\u7d71\u7ba1\u7406\uff0c\u4e26\u4e14\u53ef\u4ee5\u5728\u55ae\u500b\u7dda\u7a0b\u4e2d\u4e26\u884c\u904b\u884c\u3002\u555f\u52d5\u4e00\u500b goroutine \u975e\u5e38\u7c21\u55ae\uff0c\u53ea\u9700\u4f7f\u7528\u95dc\u9375\u5b57 <code>go<\/code>\uff0c\u5f8c\u9762\u8ddf\u8457\u8981\u57f7\u884c\u7684\u51fd\u6578\u3002\u4f8b\u5982\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 \u57f7\u884c\u3002\u7531\u65bc goroutine \u662f\u8f15\u91cf\u7d1a\u7684\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u555f\u52d5\u6210\u5343\u4e0a\u842c\u7684 goroutine\uff0c\u800c\u4e0d\u6703\u5c0d\u7cfb\u7d71\u8cc7\u6e90\u9020\u6210\u904e\u5927\u58d3\u529b\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-coroutine%ef%bc%9f-MZuxLHtSaI\">\u4ec0\u9ebc\u662f coroutine\uff1f<\/h2>\n<p>coroutine \u662f\u4e00\u7a2e\u901a\u7528\u7684\u7de8\u7a0b\u7d50\u69cb\uff0c\u5141\u8a31\u51fd\u6578\u5728\u57f7\u884c\u904e\u7a0b\u4e2d\u66ab\u505c\u548c\u6062\u5fa9\u3002\u9019\u7a2e\u7279\u6027\u4f7f\u5f97 coroutine \u7279\u5225\u9069\u5408\u65bc\u8655\u7406\u975e\u540c\u6b65\u64cd\u4f5c\u548c\u5354\u4f5c\u5f0f\u591a\u4efb\u52d9\u8655\u7406\u3002\u8a31\u591a\u7de8\u7a0b\u8a9e\u8a00\uff08\u5982 Python\u3001Lua \u548c Kotlin\uff09\u90fd\u652f\u6301 coroutine\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528 Python \u7684 coroutine \u793a\u4f8b\uff1a<\/p>\n<pre><code>import asyncio\n\nasync def say_hello():\n    print(\"Hello, World!\")\n    await asyncio.sleep(1)  # \u66ab\u505c coroutine\n\nasync def main():\n    await say_hello()  # \u555f\u52d5 coroutine\n\nasyncio.run(main())\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6578 <code>say_hello<\/code> \u662f\u4e00\u500b coroutine\uff0c\u4f7f\u7528 <code>await<\/code> \u95dc\u9375\u5b57\u4f86\u66ab\u505c\u5176\u57f7\u884c\uff0c\u76f4\u5230 <code>asyncio.sleep<\/code> \u5b8c\u6210\u3002<\/p>\n<h2 id=\"goroutine-%e5%92%8c-coroutine-%e7%9a%84%e4%b8%bb%e8%a6%81%e5%8d%80%e5%88%a5-MZuxLHtSaI\">goroutine \u548c coroutine \u7684\u4e3b\u8981\u5340\u5225<\/h2>\n<ul>\n<li><strong>\u57f7\u884c\u6a21\u578b\uff1a<\/strong>goroutine \u662f\u7531 Go \u7684\u904b\u884c\u6642\u7cfb\u7d71\u7ba1\u7406\u7684\u8f15\u91cf\u7d1a\u57f7\u884c\u55ae\u5143\uff0c\u800c coroutine \u901a\u5e38\u7531\u958b\u767c\u8005\u624b\u52d5\u7ba1\u7406\u3002<\/li>\n<li><strong>\u8a9e\u6cd5\uff1a<\/strong>\u555f\u52d5 goroutine \u53ea\u9700\u4f7f\u7528 <code>go<\/code> \u95dc\u9375\u5b57\uff0c\u800c coroutine \u901a\u5e38\u9700\u8981\u4f7f\u7528 <code>async<\/code> \u548c <code>await<\/code> \u95dc\u9375\u5b57\u3002<\/li>\n<li><strong>\u8cc7\u6e90\u4f7f\u7528\uff1a<\/strong>goroutine \u7684\u8cc7\u6e90\u958b\u92b7\u76f8\u5c0d\u8f03\u5c0f\uff0c\u80fd\u5920\u5728\u55ae\u500b\u7dda\u7a0b\u4e2d\u904b\u884c\u5927\u91cf\u7684 goroutine\uff1b\u800c coroutine \u7684\u6578\u91cf\u901a\u5e38\u53d7\u5230\u8a9e\u8a00\u672c\u8eab\u7684\u9650\u5236\u3002<\/li>\n<li><strong>\u8abf\u5ea6\uff1a<\/strong>goroutine \u7684\u8abf\u5ea6\u7531 Go \u7684\u904b\u884c\u6642\u7cfb\u7d71\u81ea\u52d5\u8655\u7406\uff0c\u800c coroutine \u7684\u8abf\u5ea6\u901a\u5e38\u9700\u8981\u958b\u767c\u8005\u624b\u52d5\u63a7\u5236\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-MZuxLHtSaI\">\u7d50\u8ad6<\/h2>\n<p>goroutine \u548c coroutine \u90fd\u662f\u5be6\u73fe\u4e26\u884c\u548c\u975e\u540c\u6b65\u7de8\u7a0b\u7684\u91cd\u8981\u5de5\u5177\u3002goroutine \u63d0\u4f9b\u4e86\u4e00\u7a2e\u7c21\u55ae\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f\u4f86\u8655\u7406\u4e26\u884c\u4efb\u52d9\uff0c\u800c coroutine \u5247\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u9748\u6d3b\u6027\u548c\u63a7\u5236\u529b\u3002\u6839\u64da\u5177\u9ad4\u7684\u61c9\u7528\u9700\u6c42\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u9078\u64c7\u6700\u9069\u5408\u7684\u6280\u8853\u4f86\u5be6\u73fe\u9ad8\u6548\u7684\u7a0b\u5f0f\u8a2d\u8a08\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u670d\u52d9\u6709\u8208\u8da3\uff0c\u6216\u60f3\u4e86\u89e3\u66f4\u591a\u95dc\u65bc <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u7684\u8cc7\u8a0a\uff0c\u6b61\u8fce\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u8a73\u7d30\u8cc7\u6599\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3GO\u6559\u7a0b\u4e2dgoroutine\u8207coroutine\u7684\u5340\u5225\uff0c\u6df1\u5165\u63a2\u8a0e\u5169\u8005\u7684\u7279\u6027\u3001\u7528\u6cd5\u53ca\u6027\u80fd\u5dee\u7570\uff0c\u63d0\u5347\u4f60\u7684\u7de8\u7a0b\u6280\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-57660","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57660","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=57660"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57660\/revisions"}],"predecessor-version":[{"id":57661,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57660\/revisions\/57661"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}