{"id":205766,"date":"2025-06-12T12:30:27","date_gmt":"2025-06-12T04:30:27","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205766\/"},"modified":"2025-06-12T12:30:27","modified_gmt":"2025-06-12T04:30:27","slug":"%e5%bc%82%e6%ad%a5%e5%a4%84%e7%90%86%e5%9c%a8-golang-%e5%87%bd%e6%95%b0%e4%b8%ad%e4%bd%bf%e7%94%a8%e5%b9%b6%e5%8f%91-goroutine-%e7%9a%84%e4%bc%98%e5%8a%bf%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205766\/","title":{"rendered":"\u5f02\u6b65\u5904\u7406\u5728 Golang \u51fd\u6570\u4e2d\u4f7f\u7528\u5e76\u53d1 goroutine \u7684\u4f18\u52bf\uff1f"},"content":{"rendered":"<p>\u4eca\u5929GOLANG\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5f02\u6b65\u5904\u7406\u5728 GOLANG \u51fd\u6570\u4e2d\u4f7f\u7528\u5e76\u53d1 goroutine \u7684\u4f18\u52bf\uff1f\u300b<\/span>\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1729993960671d9ce82446f.jpg\" class=\"aligncenter\">,\u5e76\u53d1\u7f16\u7a0b\u662f\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u7684\u5173\u952e\u6280\u672f\u3002\u5728 Go \u4e2d\uff0cgoroutine \u662f\u5b9e\u73b0\u5e76\u53d1\u7684\u5f3a\u5927\u5de5\u5177\u3002goroutine \u662f\u8f7b\u91cf\u7ea7\u7684\u865a\u62df\u7ebf\u7a0b\uff0c\u53ef\u4e0e\u534f\u7a0b\u4e00\u8d77\u8f7b\u677e\u5b9e\u73b0\u5f02\u6b65\u5904\u7406\u3002,\u4f7f\u7528 goroutine \u63d0\u4f9b\u4e86\u4ee5\u4e0b\u4f18\u52bf\uff1a,\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u6765\u4e0b\u8f7d\u4e00\u7ec4 URL \u5e76\u4f7f\u7528 goroutine \u5e76\u884c\u6267\u884c\u4efb\u52a1\u3002,\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff1a,\u901a\u8fc7\u4f7f\u7528 goroutine\uff0c\u6211\u4eec\u80fd\u591f\u5e76\u53d1\u5730\u4e0b\u8f7d URL\uff0c\u6781\u5927\u5730\u63d0\u9ad8\u4e86\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u54cd\u5e94\u80fd\u529b\u3002,\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5f02\u6b65\u5904\u7406\u5728 GOLANG \u51fd\u6570\u4e2d\u4f7f\u7528\u5e76\u53d1 goroutine \u7684\u4f18\u52bf\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~GOLANG\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03GOLANG\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01,<br \/>\n<code>main<\/code> \u51fd\u6570\u4ece\u901a\u9053\u63a5\u6536\u7ed3\u679c\u5e76\u5c06\u5176\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002,goroutine \u4f1a\u5c06\u4e0b\u8f7d\u7ed3\u679c\u6216\u9519\u8bef\u6d88\u606f\u53d1\u9001\u5230\u901a\u9053\u3002,\u5bf9\u4e8e\u6bcf\u4e2a URL\uff0c\u6211\u4eec\u542f\u52a8\u4e00\u4e2a goroutine \u5e76\u884c\u4e0b\u8f7d URL\u3002,<br \/>\n<code>main<\/code> \u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a\u901a\u9053 (<br \/>\n<code>results<\/code>)\uff0c\u7528\u4e8e\u63a5\u6536 goroutine \u7684\u7ed3\u679c\u3002,<br \/>\n<strong>\u63d0\u9ad8\u54cd\u5e94\u80fd\u529b\uff1a<\/strong>I\/O \u5bc6\u96c6\u578b\u4efb\u52a1\u53ef\u4ee5\u5728 goroutine \u4e2d\u6267\u884c\uff0c\u907f\u514d\u963b\u585e\u4e3b\u7ebf\u7a0b\u5e76\u4fdd\u6301\u5e94\u7528\u7a0b\u5e8f\u7684\u54cd\u5e94\u80fd\u529b\u3002,<br \/>\n<strong>\u5e76\u884c\u6267\u884c\u4efb\u52a1\uff1a<\/strong>goroutine \u53ef\u4ee5\u5728\u5e76\u53d1\u6267\u884c\u4efb\u52a1\uff0c\u5145\u5206\u5229\u7528\u591a\u6838 CPU\u3002,<br \/>\n<strong>\u7b80\u5316\u4ee3\u7801\uff1a<\/strong>\u4f7f\u7528 goroutine \u53ef\u4ee5\u907f\u514d\u590d\u6742\u7684\u7ebf\u7a0b\u540c\u6b65\u548c\u9501\u673a\u5236\u3002,\u4eca\u5929GOLANG\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5f02\u6b65\u5904\u7406\u5728 GOLANG \u51fd\u6570\u4e2d\u4f7f\u7528\u5e76\u53d1 goroutine \u7684\u4f18\u52bf\uff1f\u300b<\/span>\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~,<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>\u5f02\u6b65\u5904\u7406\u5728 GOLANG \u51fd\u6570\u4e2d\u4f7f\u7528\u5e76\u53d1 goroutine \u7684\u4f18\u52bf\uff1f<\/span><\/p>\n<h1>\u5f02\u6b65\u5904\u7406\u5728 GOLANG \u51fd\u6570\u4e2d\u4f7f\u7528\u5e76\u53d1 goroutine \u7684\u4f18\u52bf\uff1f<\/h1>\n<p><span>2024-10-27 09:52:55<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eca\u5929GOLANG\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5f02\u6b65\u5904\u7406\u5728 GOLANG \u51fd\u6570\u4e2d\u4f7f\u7528\u5e76\u53d1 goroutine \u7684\u4f18\u52bf\uff1f\u300b<\/span>\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1729993960671d9ce82446f.jpg\" class=\"aligncenter\"><\/p>\n<h2>\u5f02\u6b65\u5904\u7406\u5728 GOLANG \u51fd\u6570\u4e2d\u4f7f\u7528\u5e76\u53d1 goroutine \u7684\u4f18\u52bf<\/h2>\n<p>\u5e76\u53d1\u7f16\u7a0b\u662f\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u7684\u5173\u952e\u6280\u672f\u3002\u5728 Go \u4e2d\uff0cgoroutine \u662f\u5b9e\u73b0\u5e76\u53d1\u7684\u5f3a\u5927\u5de5\u5177\u3002goroutine \u662f\u8f7b\u91cf\u7ea7\u7684\u865a\u62df\u7ebf\u7a0b\uff0c\u53ef\u4e0e\u534f\u7a0b\u4e00\u8d77\u8f7b\u677e\u5b9e\u73b0\u5f02\u6b65\u5904\u7406\u3002<\/p>\n<h3>goroutine \u7684\u4f18\u70b9<\/h3>\n<p>\u4f7f\u7528 goroutine \u63d0\u4f9b\u4e86\u4ee5\u4e0b\u4f18\u52bf\uff1a<\/p>\n<ul>\n<li><strong>\u5e76\u884c\u6267\u884c\u4efb\u52a1\uff1a<\/strong>goroutine \u53ef\u4ee5\u5728\u5e76\u53d1\u6267\u884c\u4efb\u52a1\uff0c\u5145\u5206\u5229\u7528\u591a\u6838 CPU\u3002<\/li>\n<li><strong>\u63d0\u9ad8\u54cd\u5e94\u80fd\u529b\uff1a<\/strong>I\/O \u5bc6\u96c6\u578b\u4efb\u52a1\u53ef\u4ee5\u5728 goroutine \u4e2d\u6267\u884c\uff0c\u907f\u514d\u963b\u585e\u4e3b\u7ebf\u7a0b\u5e76\u4fdd\u6301\u5e94\u7528\u7a0b\u5e8f\u7684\u54cd\u5e94\u80fd\u529b\u3002<\/li>\n<li><strong>\u7b80\u5316\u4ee3\u7801\uff1a<\/strong>\u4f7f\u7528 goroutine \u53ef\u4ee5\u907f\u514d\u590d\u6742\u7684\u7ebf\u7a0b\u540c\u6b65\u548c\u9501\u673a\u5236\u3002<\/li>\n<\/ul>\n<h3>\u4f7f\u7528 goroutine \u5b9e\u6218\u6848\u4f8b<\/h3>\n<p>\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u6765\u4e0b\u8f7d\u4e00\u7ec4 URL \u5e76\u4f7f\u7528 goroutine \u5e76\u884c\u6267\u884c\u4efb\u52a1\u3002<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"net\/http\"\n    \"time\"\n)\n\nfunc main() {\n    urls := []string{\n        \"https:\/\/google.com\",\n        \"https:\/\/amazon.com\",\n        \"https:\/\/facebook.com\",\n    }\n\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u901a\u9053\u6765\u63a5\u6536 goroutine \u7684\u7ed3\u679c\n    results := make(chan string)\n\n    \/\/ \u542f\u52a8 goroutine \u5e76\u884c\u4e0b\u8f7d URL\n    for _, url := range urls {\n        go func(url string) {\n            \/\/ \u4e0b\u8f7d URL \u5e76\u5c06\u7ed3\u679c\u53d1\u9001\u5230\u901a\u9053\n            resp, err := http.Get(url)\n            if err != nil {\n                results &lt;- fmt.Sprintf(\"Error downloading %s: %v\", url, err)\n            } else {\n                results &lt;- fmt.Sprintf(\"%s: downloaded\", url)\n            }\n        }(url)\n    }\n\n    \/\/ \u4ece\u901a\u9053\u4e2d\u63a5\u6536\u7ed3\u679c\u5e76\u6253\u5370\u5230\u63a7\u5236\u53f0\n    for i := 0; i &lt; len(urls); i++ {\n        result := &lt;-results\n        fmt.Println(result)\n    }\n}<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff1a<\/p>\n<ul>\n<li><code>main<\/code> \u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a\u901a\u9053 (<code>results<\/code>)\uff0c\u7528\u4e8e\u63a5\u6536 goroutine \u7684\u7ed3\u679c\u3002<\/li>\n<li>\u5bf9\u4e8e\u6bcf\u4e2a URL\uff0c\u6211\u4eec\u542f\u52a8\u4e00\u4e2a goroutine \u5e76\u884c\u4e0b\u8f7d URL\u3002<\/li>\n<li>goroutine \u4f1a\u5c06\u4e0b\u8f7d\u7ed3\u679c\u6216\u9519\u8bef\u6d88\u606f\u53d1\u9001\u5230\u901a\u9053\u3002<\/li>\n<li><code>main<\/code> \u51fd\u6570\u4ece\u901a\u9053\u63a5\u6536\u7ed3\u679c\u5e76\u5c06\u5176\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u4f7f\u7528 goroutine\uff0c\u6211\u4eec\u80fd\u591f\u5e76\u53d1\u5730\u4e0b\u8f7d URL\uff0c\u6781\u5927\u5730\u63d0\u9ad8\u4e86\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u54cd\u5e94\u80fd\u529b\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5f02\u6b65\u5904\u7406\u5728 GOLANG \u51fd\u6570\u4e2d\u4f7f\u7528\u5e76\u53d1 goroutine \u7684\u4f18\u52bf\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~GOLANG\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03GOLANG\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   MIT\u97e9\u677e\u56e2\u961f\u957f\u4e0a\u4e0b\u6587LLM\u63a8\u7406\u9ad8\u6548\u6846\u67b6DuoAttention\uff1a\u5355GPU\u5b9e\u73b0330\u4e07Token\u4e0a\u4e0b\u6587\u63a8\u7406\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u7528 GOLANG \u533f\u540d\u51fd\u6570\u521b\u5efa\u9ad8\u9636\u51fd\u6570\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929GOLANG\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86 \u300a&#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-205766","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205766","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=205766"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205766\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}