{"id":205755,"date":"2025-06-12T10:22:46","date_gmt":"2025-06-12T02:22:46","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205755\/"},"modified":"2025-06-12T10:22:46","modified_gmt":"2025-06-12T02:22:46","slug":"golang-%e5%87%bd%e6%95%b0%ef%bc%9agoroutine-%e7%ae%a1%e7%90%86%e5%af%b9%e5%87%bd%e6%95%b0%e6%80%a7%e8%83%bd%e7%9a%84%e5%bd%b1%e5%93%8d","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205755\/","title":{"rendered":"Golang \u51fd\u6570\uff1agoroutine \u7ba1\u7406\u5bf9\u51fd\u6570\u6027\u80fd\u7684\u5f71\u54cd"},"content":{"rendered":"<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728GOLANG\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300aGOLANG \u51fd\u6570\uff1agoroutine \u7ba1\u7406\u5bf9\u51fd\u6570\u6027\u80fd\u7684\u5f71\u54cd\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~,Goroutine \u7684\u6570\u91cf\u5f71\u54cd\u51fd\u6570\u6027\u80fd\uff0c\u4e3b\u8981\u662f\u7531\u4e8e\u8c03\u5ea6\u5f00\u9500\u3001\u5185\u5b58\u5f00\u9500\u548c\u7ade\u4e89\u3002\u4f7f\u7528\u591a\u4e2a goroutine \u7684\u51fd\u6570\uff08FibonacciGoroutines\uff09\u6bd4\u4f7f\u7528\u5355\u4e2a goroutine \u7684\u51fd\u6570\uff08FibonacciSingle\uff09\u6027\u80fd\u66f4\u597d\uff0c\u901a\u8fc7\u57fa\u51c6\u6d4b\u8bd5\u53ef\u4ee5\u770b\u5230\u660e\u663e\u7684\u5dee\u5f02\u3002\u4f18\u5316 goroutine \u7ba1\u7406\uff0c\u5305\u62ec\u5747\u8861 goroutine \u6570\u91cf\u548c\u5e76\u53d1\u6027\uff0c\u53ef\u4ee5\u6700\u5927\u5316\u51fd\u6570\u6027\u80fd\u3002,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1729989226671d8a6abc24e.jpg\" class=\"aligncenter\">,<br \/>\n<strong>Go \u51fd\u6570\uff1agoroutine \u7ba1\u7406\u5bf9\u51fd\u6570\u6027\u80fd\u7684\u5f71\u54cd<\/strong>,Goroutine \u662f Go \u4e2d\u7684\u5e76\u53d1\u539f\u8bed\uff0c\u5141\u8bb8\u60a8\u5728\u4e0d\u521b\u5efa\u65b0\u8fdb\u7a0b\u7684\u60c5\u51b5\u4e0b\u5e76\u884c\u6267\u884c\u4ee3\u7801\u3002\u7136\u800c\uff0cgoroutine \u7684\u6570\u91cf\u5bf9\u51fd\u6570\u6027\u80fd\u6709\u5f88\u5927\u5f71\u54cd\u3002,<br \/>\n<strong>\u5f71\u54cd<\/strong>,\u7ba1\u7406 goroutine \u7684\u5f00\u9500\u5305\u62ec\uff1a,<br \/>\n<strong>\u5b9e\u6218\u6848\u4f8b<\/strong>,\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\uff0c\u6f14\u793a\u4e86 goroutine \u7ba1\u7406\u5bf9\u51fd\u6570\u6027\u80fd\u7684\u5f71\u54cd\uff1a,\u8fd0\u884c\u57fa\u51c6\u6d4b\u8bd5\uff1a,\u60a8\u5c06\u770b\u5230\u4f7f\u7528\u591a\u4e2a goroutine \u7684<br \/>\n<code>FibonacciGoroutines<\/code> \u51fd\u6570\u6027\u80fd\u660e\u663e\u4f18\u4e8e\u4f7f\u7528\u5355\u4e2a goroutine \u7684<br \/>\n<code>FibonacciSingle<\/code> \u51fd\u6570\u3002,<br \/>\n<strong>\u7ed3\u8bba<\/strong>,goroutine \u662f\u63d0\u9ad8 Go \u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u7684\u5f3a\u5927\u5de5\u5177\u3002\u4f46\u662f\uff0c\u91cd\u8981\u7684\u662f\u8981\u4ed4\u7ec6\u7ba1\u7406 goroutine \u6570\u91cf\u4ee5\u907f\u514d\u6027\u80fd\u4e0b\u964d\u3002\u901a\u8fc7\u4ed4\u7ec6\u5747\u8861 goroutine \u6570\u91cf\u548c\u5e76\u53d1\u6027\uff0c\u60a8\u53ef\u4ee5\u4f18\u5316\u51fd\u6570\u6027\u80fd\u5e76\u83b7\u5f97\u6700\u5927\u7684\u6536\u76ca\u3002,\u4ee5\u4e0a\u5c31\u662f\u300aGOLANG \u51fd\u6570\uff1agoroutine \u7ba1\u7406\u5bf9\u51fd\u6570\u6027\u80fd\u7684\u5f71\u54cd\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u51fd\u6570\u6027\u80fd\u7684\u8d44\u6599\u8bf7\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff01,<br \/>\n<strong>\u5185\u5b58\u5f00\u9500\uff1a<\/strong> \u6bcf\u4e2a goroutine \u90fd\u6709\u81ea\u5df1\u7684\u5806\u6808\uff0c\u8fd9\u4f1a\u6d88\u8017\u5185\u5b58\u3002,<br \/>\n<strong>\u8c03\u5ea6\u5f00\u9500\uff1a<\/strong> Go \u8fd0\u884c\u65f6\u9700\u8981\u8c03\u5ea6 goroutine\uff0c\u8fd9\u4f1a\u5e26\u6765\u4e00\u4e9b\u65f6\u95f4\u548c\u8d44\u6e90\u7684\u5f00\u9500\u3002,<br \/>\n<strong>\u7ade\u4e89\uff1a<\/strong> Goroutine \u5728\u5171\u4eab\u8d44\u6e90\u65f6\u53ef\u80fd\u4f1a\u4ea7\u751f\u7ade\u4e89\uff0c\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u3002,\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728GOLANG\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300aGOLANG \u51fd\u6570\uff1agoroutine \u7ba1\u7406\u5bf9\u51fd\u6570\u6027\u80fd\u7684\u5f71\u54cd\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~,<br \/>\n<b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>GOLANG \u51fd\u6570\uff1agoroutine \u7ba1\u7406\u5bf9\u51fd\u6570\u6027\u80fd\u7684\u5f71\u54cd<\/span><\/p>\n<h1>GOLANG \u51fd\u6570\uff1agoroutine \u7ba1\u7406\u5bf9\u51fd\u6570\u6027\u80fd\u7684\u5f71\u54cd<\/h1>\n<p><span>2024-10-27 08:34:01<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728GOLANG\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300aGOLANG \u51fd\u6570\uff1agoroutine \u7ba1\u7406\u5bf9\u51fd\u6570\u6027\u80fd\u7684\u5f71\u54cd\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p>Goroutine \u7684\u6570\u91cf\u5f71\u54cd\u51fd\u6570\u6027\u80fd\uff0c\u4e3b\u8981\u662f\u7531\u4e8e\u8c03\u5ea6\u5f00\u9500\u3001\u5185\u5b58\u5f00\u9500\u548c\u7ade\u4e89\u3002\u4f7f\u7528\u591a\u4e2a goroutine \u7684\u51fd\u6570\uff08FibonacciGoroutines\uff09\u6bd4\u4f7f\u7528\u5355\u4e2a goroutine \u7684\u51fd\u6570\uff08FibonacciSingle\uff09\u6027\u80fd\u66f4\u597d\uff0c\u901a\u8fc7\u57fa\u51c6\u6d4b\u8bd5\u53ef\u4ee5\u770b\u5230\u660e\u663e\u7684\u5dee\u5f02\u3002\u4f18\u5316 goroutine \u7ba1\u7406\uff0c\u5305\u62ec\u5747\u8861 goroutine \u6570\u91cf\u548c\u5e76\u53d1\u6027\uff0c\u53ef\u4ee5\u6700\u5927\u5316\u51fd\u6570\u6027\u80fd\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1729989226671d8a6abc24e.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Go \u51fd\u6570\uff1agoroutine \u7ba1\u7406\u5bf9\u51fd\u6570\u6027\u80fd\u7684\u5f71\u54cd<\/strong><\/p>\n<p>Goroutine \u662f Go \u4e2d\u7684\u5e76\u53d1\u539f\u8bed\uff0c\u5141\u8bb8\u60a8\u5728\u4e0d\u521b\u5efa\u65b0\u8fdb\u7a0b\u7684\u60c5\u51b5\u4e0b\u5e76\u884c\u6267\u884c\u4ee3\u7801\u3002\u7136\u800c\uff0cgoroutine \u7684\u6570\u91cf\u5bf9\u51fd\u6570\u6027\u80fd\u6709\u5f88\u5927\u5f71\u54cd\u3002<\/p>\n<p><strong>\u5f71\u54cd<\/strong><\/p>\n<p>\u7ba1\u7406 goroutine \u7684\u5f00\u9500\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u8c03\u5ea6\u5f00\u9500\uff1a<\/strong> Go \u8fd0\u884c\u65f6\u9700\u8981\u8c03\u5ea6 goroutine\uff0c\u8fd9\u4f1a\u5e26\u6765\u4e00\u4e9b\u65f6\u95f4\u548c\u8d44\u6e90\u7684\u5f00\u9500\u3002<\/li>\n<li><strong>\u5185\u5b58\u5f00\u9500\uff1a<\/strong> \u6bcf\u4e2a goroutine \u90fd\u6709\u81ea\u5df1\u7684\u5806\u6808\uff0c\u8fd9\u4f1a\u6d88\u8017\u5185\u5b58\u3002<\/li>\n<li><strong>\u7ade\u4e89\uff1a<\/strong> Goroutine \u5728\u5171\u4eab\u8d44\u6e90\u65f6\u53ef\u80fd\u4f1a\u4ea7\u751f\u7ade\u4e89\uff0c\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\uff0c\u6f14\u793a\u4e86 goroutine \u7ba1\u7406\u5bf9\u51fd\u6570\u6027\u80fd\u7684\u5f71\u54cd\uff1a<\/p>\n<pre>package main\n\nimport \"testing\"\n\n\/\/ \u4f7f\u7528\u5355\u4e2a goroutine \u8ba1\u7b97\u6590\u6ce2\u90a3\u5951\u6570\u5217\nfunc FibonacciSingle(n int) int64 {\n    if n &lt;= 1 {\n        return 1\n    }\n    return FibonacciSingle(n-1) + FibonacciSingle(n-2)\n}\n\n\/\/ \u4f7f\u7528\u591a\u4e2a goroutine \u5e76\u884c\u8ba1\u7b97\u6590\u6ce2\u90a3\u5951\u6570\u5217\nfunc FibonacciGoroutines(n int) int64 {\n    if n &lt;= 1 {\n        return 1\n    }\n\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u901a\u9053\u6765\u63a5\u6536\u7b2c\u4e00\u4e2a goroutine \u7684\u7ed3\u679c\n    resultCh1 := make(chan int64)\n    \/\/ \u521b\u5efa\u4e00\u4e2a goroutine \u6765\u8ba1\u7b97\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u7b2c\u4e00\u4e2a\u90e8\u5206\n    go func() {\n        resultCh1 &lt;- FibonacciGoroutines(n - 1)\n    }()\n\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u901a\u9053\u6765\u63a5\u6536\u7b2c\u4e8c\u4e2a goroutine \u7684\u7ed3\u679c\n    resultCh2 := make(chan int64)\n    \/\/ \u521b\u5efa\u4e00\u4e2a goroutine \u6765\u8ba1\u7b97\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u7b2c\u4e8c\u4e2a\u90e8\u5206\n    go func() {\n        resultCh2 &lt;- FibonacciGoroutines(n - 1)\n    }()\n\n    return &lt;-resultCh1 + &lt;-resultCh2\n}\n\nfunc BenchmarkFibonacciSingle(b *testing.B) {\n    for i := 0; i &lt; b.N; i++ {\n        FibonacciSingle(30)\n    }\n}\n\nfunc BenchmarkFibonacciGoroutines(b *testing.B) {\n    for i := 0; i &lt; b.N; i++ {\n        FibonacciGoroutines(30)\n    }\n}<\/pre>\n<p>\u8fd0\u884c\u57fa\u51c6\u6d4b\u8bd5\uff1a<\/p>\n<pre>go test -bench=.<\/pre>\n<p>\u60a8\u5c06\u770b\u5230\u4f7f\u7528\u591a\u4e2a goroutine \u7684 <code>FibonacciGoroutines<\/code> \u51fd\u6570\u6027\u80fd\u660e\u663e\u4f18\u4e8e\u4f7f\u7528\u5355\u4e2a goroutine \u7684 <code>FibonacciSingle<\/code> \u51fd\u6570\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>goroutine \u662f\u63d0\u9ad8 Go \u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u7684\u5f3a\u5927\u5de5\u5177\u3002\u4f46\u662f\uff0c\u91cd\u8981\u7684\u662f\u8981\u4ed4\u7ec6\u7ba1\u7406 goroutine \u6570\u91cf\u4ee5\u907f\u514d\u6027\u80fd\u4e0b\u964d\u3002\u901a\u8fc7\u4ed4\u7ec6\u5747\u8861 goroutine \u6570\u91cf\u548c\u5e76\u53d1\u6027\uff0c\u60a8\u53ef\u4ee5\u4f18\u5316\u51fd\u6570\u6027\u80fd\u5e76\u83b7\u5f97\u6700\u5927\u7684\u6536\u76ca\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aGOLANG \u51fd\u6570\uff1agoroutine \u7ba1\u7406\u5bf9\u51fd\u6570\u6027\u80fd\u7684\u5f71\u54cd\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u51fd\u6570\u6027\u80fd\u7684\u8d44\u6599\u8bf7\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5982\u4f55\u5728 LESS \u4e2d\u4f7f\u7528\u5a92\u4f53\u67e5\u8be2\u52a8\u6001\u8c03\u6574\u5143\u7d20\u5185\u8fb9\u8ddd\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   PHP \u53c2\u6570\u7ed1\u5b9a\u4e0e SQL \u6ce8\u5165\u7684\u5173\u8054\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728G&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205755","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205755","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=205755"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205755\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}