{"id":758,"date":"2024-11-07T13:49:52","date_gmt":"2024-11-07T05:49:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/758\/"},"modified":"2024-11-07T13:49:52","modified_gmt":"2024-11-07T05:49:52","slug":"golang-%e5%87%bd%e6%95%b0%ef%bc%9a%e5%b9%b6%e5%8f%91%e4%bb%bb%e5%8a%a1%e6%89%a7%e8%a1%8c%e4%b8%ad%e5%8f%af%e6%89%a9%e5%b1%95%e6%80%a7%e5%92%8c%e5%ae%b9%e9%94%99%e6%80%a7%e7%9a%84%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/758\/","title":{"rendered":"Golang \u51fd\u6570\uff1a\u5e76\u53d1\u4efb\u52a1\u6267\u884c\u4e2d\u53ef\u6269\u5c55\u6027\u548c\u5bb9\u9519\u6027\u7684\u5b9e\u73b0"},"content":{"rendered":"<p>\u5e76\u53d1\u5904\u7406\u7684\u4e24\u4e2a\u5173\u952e\u76ee\u6807\u2014\u2014\u53ef\u6269\u5c55\u6027\u548c\u5bb9\u9519\u6027\u2014\u2014\u53ef\u4ee5\u901a\u8fc7 go \u51fd\u6570\u5b9e\u73b0\u3002\u53ef\u6269\u5c55\u6027\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u8f7b\u91cf\u7ea7\u5e76\u53d1\u6267\u884c\u5355\u5143 goroutine \u6765\u5b9e\u73b0\uff0c\u4ece\u800c\u5145\u5206\u5229\u7528\u591a\u6838 cpu\u3002\u5bb9\u9519\u6027\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u9519\u8bef\u901a\u9053\u3001recover \u548c\u4e0a\u4e0b\u6587\u7b49\u673a\u5236\u6765\u5904\u7406\u9519\u8bef\u548c\u5f02\u5e38\u60c5\u51b5\u3002\u901a\u8fc7\u7ed3\u5408\u53ef\u6269\u5c55\u6027\u548c\u5bb9\u9519\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528 go \u51fd\u6570\u521b\u5efa\u9c81\u68d2\u4e14\u53ef\u6269\u5c55\u7684\u5e76\u53d1\u7a0b\u5e8f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1155\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172826856194051.jpg\" width=\"800\" height=\"320\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172826856194051.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172826856194051-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172826856194051-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172826856194051-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"Golang \u51fd\u6570\uff1a\u5e76\u53d1\u4efb\u52a1\u6267\u884c\u4e2d\u53ef\u6269\u5c55\u6027\u548c\u5bb9\u9519\u6027\u7684\u5b9e\u73b0\u63d2\u56fe\" alt=\"Golang \u51fd\u6570\uff1a\u5e76\u53d1\u4efb\u52a1\u6267\u884c\u4e2d\u53ef\u6269\u5c55\u6027\u548c\u5bb9\u9519\u6027\u7684\u5b9e\u73b0\u63d2\u56fe\" \/><\/p>\n<p><strong>Go \u51fd\u6570\uff1a\u5b9e\u73b0\u5e76\u53d1\u4efb\u52a1\u6267\u884c\u4e2d\u7684\u53ef\u6269\u5c55\u6027\u548c\u5bb9\u9519\u6027<\/strong><\/p>\n<p>\u5e76\u53d1\u5904\u7406\u662f\u63d0\u9ad8\u7a0b\u5e8f\u6548\u7387\u7684\u4e00\u79cd\u91cd\u8981\u6280\u672f\uff0c\u5b83\u80fd\u6709\u6548\u5229\u7528\u591a\u6838 CPU \u7684\u4f18\u52bf\u3002\u5728\u5e76\u53d1\u4efb\u52a1\u6267\u884c\u4e2d\uff0c\u53ef\u6269\u5c55\u6027\uff08\u6269\u5c55\u5904\u7406\u80fd\u529b\uff09\u548c\u5bb9\u9519\u6027\uff08\u5904\u7406\u9519\u8bef\u7684\u80fd\u529b\uff09\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528 Go \u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e9b\u76ee\u6807\u3002<\/p>\n<p><strong>\u53ef\u6269\u5c55\u6027<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201cgo\u8bed\u8a00\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09\u201d\uff1b<\/p>\n<p>\u4f7f\u7528 Go \u7684 goroutine \u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5b9e\u73b0\u53ef\u6269\u5c55\u6027\u3002goroutine \u662f\u8f7b\u91cf\u7ea7\u7684\u5e76\u53d1\u6267\u884c\u5355\u5143\uff0c\u7c7b\u4f3c\u4e8e\u7ebf\u7a0b\uff0c\u4f46\u5f00\u9500\u66f4\u4f4e\u3002\u901a\u8fc7\u521b\u5efa\u591a\u4e2a goroutine\uff0c\u60a8\u53ef\u4ee5\u5c06\u4efb\u52a1\u5206\u914d\u5230\u591a\u4e2a CPU\uff0c\u4ece\u800c\u5145\u5206\u5229\u7528\u8ba1\u7b97\u673a\u7684\u5904\u7406\u80fd\u529b\u3002<\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 goroutine \u6267\u884c\u5e76\u53d1\u4efb\u52a1\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"runtime\"\n)\n\nfunc main() {\n    \/\/ \u5e76\u884c\u6267\u884c 100 \u4e2a goroutine\n    for i := 0; i &lt;p&gt;&lt;strong&gt;\u5bb9\u9519\u6027&lt;\/strong&gt;&lt;\/p&gt;&lt;p&gt;\u5728\u5e76\u53d1\u6267\u884c\u4e2d\uff0c\u4efb\u52a1\u53ef\u80fd\u4f1a\u5931\u8d25\uff0c\u8fd9\u4f1a\u5f71\u54cd\u6574\u4f53\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u3002Go \u51fd\u6570\u63d0\u4f9b\u4e86\u591a\u79cd\u673a\u5236\u6765\u5904\u7406\u9519\u8bef\uff1a&lt;\/p&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>\u9519\u8bef\u901a\u9053:<\/strong> \u60a8\u53ef\u4ee5\u4f7f\u7528\u9519\u8bef\u901a\u9053\u5c06\u9519\u8bef\u4ece goroutine \u4f20\u9012\u56de\u4e3b\u7a0b\u5e8f\u3002<\/li>\n<li> <strong>recover:<\/strong> \u5728 goroutine \u4e2d\u4f7f\u7528 recover \u6355\u83b7\u6050\u614c\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u9519\u8bef\u3002<\/li>\n<li> <strong>\u4e0a\u4e0b\u6587:<\/strong> \u4f7f\u7528\u4e0a\u4e0b\u6587\u53ef\u4ee5\u53d6\u6d88\u4efb\u52a1\u5e76\u5904\u7406\u622a\u6b62\u65f6\u95f4\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u9519\u8bef\u901a\u9053\u5904\u7406\u5e76\u53d1\u7684\u4efb\u52a1\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\nfunc main() {\n    var wg sync.WaitGroup\n\n    \/\/ \u5e76\u884c\u6267\u884c 100 \u4e2a\u4efb\u52a1\uff0c\u6bcf\u4e2a\u4efb\u52a1\u53ef\u80fd\u5931\u8d25\n    for i := 0; i &lt;p&gt;\u901a\u8fc7\u7ed3\u5408\u53ef\u6269\u5c55\u6027\u548c\u5bb9\u9519\u6027\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u9c81\u68d2\u4e14\u53ef\u6269\u5c55\u7684 Go \u7a0b\u5e8f\uff0c\u5373\u4f7f\u5728\u5b58\u5728\u5e76\u53d1\u6311\u6218\u7684\u60c5\u51b5\u4e0b\u4e5f\u80fd\u9ad8\u6548\u8fd0\u884c\u3002&lt;\/p&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fGolang \u51fd\u6570\uff1a\u5e76\u53d1\u4efb\u52a1\u6267\u884c\u4e2d\u53ef\u6269\u5c55\u6027\u548c\u5bb9\u9519\u6027\u7684\u5b9e\u73b0\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5e76\u53d1\u5904\u7406\u7684\u4e24\u4e2a\u5173\u952e\u76ee\u6807\u2014\u2014\u53ef\u6269\u5c55\u6027\u548c\u5bb9\u9519\u6027\u2014\u2014\u53ef\u4ee5\u901a\u8fc7 go \u51fd\u6570\u5b9e\u73b0\u3002\u53ef\u6269\u5c55\u6027\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u8f7b\u91cf\u7ea7\u5e76\u53d1\u6267\u884c\u5355\u5143 goroutine \u6765\u5b9e\u73b0\uff0c\u4ece\u800c\u5145\u5206\u5229\u7528\u591a\u6838 cpu\u3002\u5bb9\u9519\u6027\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u9519\u8bef\u901a\u9053\u3001recover \u548c\u4e0a\u4e0b\u6587\u7b49\u673a\u5236\u6765\u5904\u7406\u9519\u8bef\u548c\u5f02\u5e38\u60c5\u51b5\u3002\u901a\u8fc7\u7ed3\u5408\u53ef\u6269\u5c55\u6027\u548c\u5bb9\u9519\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528 go \u51fd\u6570\u521b\u5efa\u9c81\u68d2\u4e14\u53ef\u6269\u5c55\u7684\u5e76\u53d1\u7a0b\u5e8f\u3002 Go \u51fd\u6570\uff1a\u5b9e\u73b0\u5e76\u53d1\u4efb\u52a1\u6267\u884c\u4e2d\u7684\u53ef\u6269\u5c55\u6027\u548c\u5bb9\u9519\u6027 \u5e76\u53d1\u5904\u7406\u662f\u63d0\u9ad8\u7a0b\u5e8f\u6548\u7387\u7684\u4e00\u79cd\u91cd\u8981\u6280\u672f\uff0c\u5b83\u80fd\u6709\u6548\u5229\u7528\u591a\u6838 CPU \u7684\u4f18\u52bf\u3002\u5728\u5e76\u53d1\u4efb\u52a1\u6267\u884c\u4e2d\uff0c\u53ef\u6269\u5c55\u6027\uff08\u6269\u5c55\u5904\u7406\u80fd\u529b\uff09\u548c\u5bb9\u9519\u6027\uff08\u5904\u7406\u9519\u8bef\u7684\u80fd\u529b\uff09\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528 Go \u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e9b\u76ee\u6807\u3002 \u53ef\u6269\u5c55\u6027 \u7acb\u5373\u5b66\u4e60\u201cgo\u8bed\u8a00\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09\u201d\uff1b \u4f7f\u7528 Go \u7684 goroutine \u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5b9e\u73b0\u53ef\u6269\u5c55\u6027\u3002goroutine \u662f\u8f7b\u91cf\u7ea7\u7684\u5e76\u53d1\u6267\u884c\u5355\u5143\uff0c\u7c7b\u4f3c\u4e8e\u7ebf\u7a0b\uff0c\u4f46\u5f00\u9500\u66f4\u4f4e\u3002\u901a\u8fc7\u521b\u5efa\u591a\u4e2a goroutine\uff0c\u60a8\u53ef\u4ee5\u5c06\u4efb\u52a1\u5206\u914d\u5230\u591a\u4e2a CPU\uff0c\u4ece\u800c\u5145\u5206\u5229\u7528\u8ba1\u7b97\u673a\u7684\u5904\u7406\u80fd\u529b\u3002 \u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 goroutine \u6267\u884c\u5e76\u53d1\u4efb\u52a1\uff1a package main import ( &#8220;fmt&#8221; &#8220;runtime&#8221; ) func main() { \/\/ \u5e76\u884c\u6267\u884c 100 \u4e2a goroutine for i := 0; i &lt;p&gt;&lt;strong&gt;\u5bb9\u9519\u6027&lt;\/strong&gt;&lt;\/p&gt;&lt;p&gt;\u5728\u5e76\u53d1\u6267\u884c\u4e2d\uff0c\u4efb\u52a1\u53ef\u80fd\u4f1a\u5931\u8d25\uff0c\u8fd9\u4f1a\u5f71\u54cd\u6574\u4f53\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u3002Go \u51fd\u6570\u63d0\u4f9b\u4e86\u591a\u79cd\u673a\u5236\u6765\u5904\u7406\u9519\u8bef\uff1a&lt;\/p&gt; \u767b\u5f55\u540e\u590d\u5236 \u9519\u8bef\u901a\u9053: \u60a8\u53ef\u4ee5\u4f7f\u7528\u9519\u8bef\u901a\u9053\u5c06\u9519\u8bef\u4ece goroutine [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-758","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/758","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=758"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/758\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}