{"id":40356,"date":"2024-12-01T08:07:52","date_gmt":"2024-12-01T00:07:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/40356\/"},"modified":"2024-12-01T08:07:52","modified_gmt":"2024-12-01T00:07:52","slug":"golang%e6%a1%86%e6%9e%b6%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3%e5%b9%b6%e5%8f%91%e9%97%ae%e9%a2%98%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/40356\/","title":{"rendered":"Golang\u6846\u67b6\u5982\u4f55\u89e3\u51b3\u5e76\u53d1\u95ee\u9898\uff1f"},"content":{"rendered":"<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aGolang\u6846\u67b6\u5982\u4f55\u89e3\u51b3\u5e76\u53d1\u95ee\u9898\uff1f\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002,GoLang \u6846\u67b6\u901a\u8fc7\u4f7f\u7528 Goroutine \u548c Channel \u89e3\u51b3\u5e76\u53d1\u95ee\u9898\u3002Goroutine \u4e3a\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u800c Channel \u4e3a Goroutine \u95f4\u901a\u4fe1\u7684\u7ba1\u9053\u3002\u6848\u4f8b\u4e2d\uff0c\u521b\u5efa Goroutine \u5e76\u884c\u5904\u7406\u6570\u636e\uff0c\u4f7f\u7528 Channel \u63a5\u6536\u5904\u7406\u7ed3\u679c\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728 Web \u670d\u52a1\u4e2d\u4f7f\u7528\u6b64\u65b9\u6cd5\u4f18\u5316\u6027\u80fd\u3002,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729914271671c659f8fd72.jpg\" class=\"aligncenter\" title=\"Golang\u6846\u67b6\u5982\u4f55\u89e3\u51b3\u5e76\u53d1\u95ee\u9898\uff1f\u63d2\u56fe\" alt=\"Golang\u6846\u67b6\u5982\u4f55\u89e3\u51b3\u5e76\u53d1\u95ee\u9898\uff1f\u63d2\u56fe\" \/>,<br \/>\n<strong>GoLang \u6846\u67b6\u5982\u4f55\u89e3\u51b3\u5e76\u53d1\u95ee\u9898<\/strong>,\u5e76\u53d1\u7f16\u7a0b\u662f\u4e00\u79cd\u5904\u7406\u6d89\u53ca\u591a\u4e2a\u6267\u884c\u6d41\u7684\u4efb\u52a1\u7684\u7f16\u7a0b\u8303\u4f8b\u3002\u5728 GoLang \u4e2d\uff0c\u5e76\u53d1\u6027\u662f\u901a\u8fc7 Goroutine \u548c Channel \u5b9e\u73b0\u7684\u3002,<br \/>\n<strong>Goroutine<\/strong>,Goroutine \u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u5b83\u4f7f\u7528\u4e0e GoLang \u8c03\u5ea6\u7a0b\u5e8f\u76f8\u540c\u7684\u5806\u6808\u3002\u4e0e\u7ebf\u7a0b\u4e0d\u540c\uff0cGoroutine \u5728\u542f\u52a8\u65f6\u4e0d\u5206\u914d\u56fa\u5b9a\u5927\u5c0f\u7684\u5806\u6808\u7a7a\u95f4\uff0c\u800c\u662f\u6839\u636e\u9700\u8981\u52a8\u6001\u589e\u957f\u548c\u6536\u7f29\u3002\u8fd9\u4f7f\u5f97 Goroutine \u975e\u5e38\u8f7b\u91cf\uff0c\u53ef\u4ee5\u521b\u5efa\u5927\u91cf Goroutine \u800c\u4e0d\u4f1a\u8017\u5c3d\u5185\u5b58\u3002\u521b\u5efa Goroutine \u7684\u8bed\u6cd5\u5982\u4e0b\uff1a,<br \/>\n<strong>Channel<\/strong>,Channel \u662f Goroutine \u4e4b\u95f4\u901a\u4fe1\u7684\u7ba1\u9053\u3002\u5b83\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u540c\u6b65\u961f\u5217\uff0c\u5141\u8bb8\u5728\u4e00\u4e2a Goroutine \u4e2d\u53d1\u9001\u6570\u636e\u5e76\u4ece\u53e6\u4e00\u4e2a Goroutine \u4e2d\u8bfb\u53d6\u6570\u636e\u3002Channel \u7684\u8bed\u6cd5\u5982\u4e0b\uff1a,<br \/>\n<strong>\u5b9e\u6218\u6848\u4f8b<\/strong>,\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 Goroutine \u548c Channel \u89e3\u51b3\u5e76\u53d1\u95ee\u9898\u7684\u793a\u4f8b\u3002\u60f3\u8c61\u4e00\u4e0b\u6709\u4e00\u4e2a\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u7684 Web \u670d\u52a1\u3002\u4e3a\u4e86\u4f18\u5316\u6027\u80fd\uff0c\u6211\u4eec\u5c06\u4f7f\u7528 Goroutine \u5e76\u884c\u5904\u7406\u6570\u636e\u3002,\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8golang\u5b66\u4e60\u7f51\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~,\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aGolang\u6846\u67b6\u5982\u4f55\u89e3\u51b3\u5e76\u53d1\u95ee\u9898\uff1f\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002,<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\u6846\u67b6\u5982\u4f55\u89e3\u51b3\u5e76\u53d1\u95ee\u9898\uff1f<\/span><\/p>\n<h1>Golang\u6846\u67b6\u5982\u4f55\u89e3\u51b3\u5e76\u53d1\u95ee\u9898\uff1f<\/h1>\n<p><span>2024-10-26 11:44:49<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aGolang\u6846\u67b6\u5982\u4f55\u89e3\u51b3\u5e76\u53d1\u95ee\u9898\uff1f\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p>GoLang \u6846\u67b6\u901a\u8fc7\u4f7f\u7528 Goroutine \u548c Channel \u89e3\u51b3\u5e76\u53d1\u95ee\u9898\u3002Goroutine \u4e3a\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u800c Channel \u4e3a Goroutine \u95f4\u901a\u4fe1\u7684\u7ba1\u9053\u3002\u6848\u4f8b\u4e2d\uff0c\u521b\u5efa Goroutine \u5e76\u884c\u5904\u7406\u6570\u636e\uff0c\u4f7f\u7528 Channel \u63a5\u6536\u5904\u7406\u7ed3\u679c\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728 Web \u670d\u52a1\u4e2d\u4f7f\u7528\u6b64\u65b9\u6cd5\u4f18\u5316\u6027\u80fd\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729914271671c659f8fd72.jpg\" class=\"aligncenter\" title=\"Golang\u6846\u67b6\u5982\u4f55\u89e3\u51b3\u5e76\u53d1\u95ee\u9898\uff1f\u63d2\u56fe\" alt=\"Golang\u6846\u67b6\u5982\u4f55\u89e3\u51b3\u5e76\u53d1\u95ee\u9898\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>GoLang \u6846\u67b6\u5982\u4f55\u89e3\u51b3\u5e76\u53d1\u95ee\u9898<\/strong><\/p>\n<p>\u5e76\u53d1\u7f16\u7a0b\u662f\u4e00\u79cd\u5904\u7406\u6d89\u53ca\u591a\u4e2a\u6267\u884c\u6d41\u7684\u4efb\u52a1\u7684\u7f16\u7a0b\u8303\u4f8b\u3002\u5728 GoLang \u4e2d\uff0c\u5e76\u53d1\u6027\u662f\u901a\u8fc7 Goroutine \u548c Channel \u5b9e\u73b0\u7684\u3002<\/p>\n<p><strong>Goroutine<\/strong><\/p>\n<p>Goroutine \u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u5b83\u4f7f\u7528\u4e0e GoLang \u8c03\u5ea6\u7a0b\u5e8f\u76f8\u540c\u7684\u5806\u6808\u3002\u4e0e\u7ebf\u7a0b\u4e0d\u540c\uff0cGoroutine \u5728\u542f\u52a8\u65f6\u4e0d\u5206\u914d\u56fa\u5b9a\u5927\u5c0f\u7684\u5806\u6808\u7a7a\u95f4\uff0c\u800c\u662f\u6839\u636e\u9700\u8981\u52a8\u6001\u589e\u957f\u548c\u6536\u7f29\u3002\u8fd9\u4f7f\u5f97 Goroutine \u975e\u5e38\u8f7b\u91cf\uff0c\u53ef\u4ee5\u521b\u5efa\u5927\u91cf Goroutine \u800c\u4e0d\u4f1a\u8017\u5c3d\u5185\u5b58\u3002\u521b\u5efa Goroutine \u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>go func() {\n    \/\/ Goroutine \u4ee3\u7801\n}()<\/pre>\n<p><strong>Channel<\/strong><\/p>\n<p>Channel \u662f Goroutine \u4e4b\u95f4\u901a\u4fe1\u7684\u7ba1\u9053\u3002\u5b83\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u540c\u6b65\u961f\u5217\uff0c\u5141\u8bb8\u5728\u4e00\u4e2a Goroutine \u4e2d\u53d1\u9001\u6570\u636e\u5e76\u4ece\u53e6\u4e00\u4e2a Goroutine \u4e2d\u8bfb\u53d6\u6570\u636e\u3002Channel \u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>ch := make(chan int)\n\/\/ \u53d1\u9001\u6570\u636e\nch &lt;- 10\n\/\/ \u63a5\u6536\u6570\u636e\ndata := &lt;-ch<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 Goroutine \u548c Channel \u89e3\u51b3\u5e76\u53d1\u95ee\u9898\u7684\u793a\u4f8b\u3002\u60f3\u8c61\u4e00\u4e0b\u6709\u4e00\u4e2a\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u7684 Web \u670d\u52a1\u3002\u4e3a\u4e86\u4f18\u5316\u6027\u80fd\uff0c\u6211\u4eec\u5c06\u4f7f\u7528 Goroutine \u5e76\u884c\u5904\u7406\u6570\u636e\u3002<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\nfunc main() {\n    \/\/ \u521b\u5efa\u4e00\u4e2a Channel \u6765\u63a5\u6536\u5904\u7406\u540e\u7684\u6570\u636e\n    resultCh := make(chan int)\n    \n    \/\/ \u521b\u5efa\u4e00\u4e2aWaitGroup\u6765\u7b49\u5f85\u6240\u6709Goroutine\u5b8c\u6210\n    wg := sync.WaitGroup{}\n\n    \/\/ \u521b\u5efa Goroutine \u6765\u5904\u7406\u6570\u636e\uff0c\u5e76\u5c06\u7ed3\u679c\u53d1\u9001\u5230 Channel\n    for i := 0; i &lt; 1000; i++ {\n        wg.Add(1)\n        go func(num int) {\n            defer wg.Done()\n            result := num * num\n            resultCh &lt;- result\n        }(i)\n    }\n\n    \/\/ \u4ece Channel \u4e2d\u8bfb\u53d6\u7ed3\u679c\u5e76\u663e\u793a\n    go func() {\n        for result := range resultCh {\n            fmt.Println(result)\n        }\n    }()\n\n    \/\/ \u7b49\u5f85\u6240\u6709 Goroutine \u5b8c\u6210\n    wg.Wait()\n    \n    \/\/ \u5173\u95ed Channel \u4ee5\u6307\u793a\u6240\u6709\u6570\u636e\u5df2\u5904\u7406\u5b8c\u6bd5\n    close(resultCh)\n}<\/pre>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8golang\u5b66\u4e60\u7f51\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Golang\u6846\u67b6\u4e2d\u7684\u8de8\u7ad9\u811a\u672c\u653b\u51fb\u9632\u8303\u4e4b\u9053\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   PHP \u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u7684\u542b\u4e49\u662f\u4ec0\u4e48\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aGolang\u6846\u67b6\u5982\u4f55\u89e3\u51b3\u5e76\u53d1\u95ee\u9898\uff1f\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002,GoLang \u6846\u67b6\u901a\u8fc7\u4f7f\u7528 Goroutine \u548c Channel \u89e3\u51b3\u5e76\u53d1\u95ee\u9898\u3002Goroutine \u4e3a\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u800c Channel \u4e3a Goroutine \u95f4\u901a\u4fe1\u7684\u7ba1\u9053\u3002\u6848\u4f8b\u4e2d\uff0c\u521b\u5efa Goroutine \u5e76\u884c\u5904\u7406\u6570\u636e\uff0c\u4f7f\u7528 Channel \u63a5\u6536\u5904\u7406\u7ed3\u679c\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728 Web \u670d\u52a1\u4e2d\u4f7f\u7528\u6b64\u65b9\u6cd5\u4f18\u5316\u6027\u80fd\u3002, , GoLang \u6846\u67b6\u5982\u4f55\u89e3\u51b3\u5e76\u53d1\u95ee\u9898,\u5e76\u53d1\u7f16\u7a0b\u662f\u4e00\u79cd\u5904\u7406\u6d89\u53ca\u591a\u4e2a\u6267\u884c\u6d41\u7684\u4efb\u52a1\u7684\u7f16\u7a0b\u8303\u4f8b\u3002\u5728 GoLang \u4e2d\uff0c\u5e76\u53d1\u6027\u662f\u901a\u8fc7 Goroutine \u548c Channel \u5b9e\u73b0\u7684\u3002, Goroutine,Goroutine \u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u5b83\u4f7f\u7528\u4e0e GoLang \u8c03\u5ea6\u7a0b\u5e8f\u76f8\u540c\u7684\u5806\u6808\u3002\u4e0e\u7ebf\u7a0b\u4e0d\u540c\uff0cGoroutine \u5728\u542f\u52a8\u65f6\u4e0d\u5206\u914d\u56fa\u5b9a\u5927\u5c0f\u7684\u5806\u6808\u7a7a\u95f4\uff0c\u800c\u662f\u6839\u636e\u9700\u8981\u52a8\u6001\u589e\u957f\u548c\u6536\u7f29\u3002\u8fd9\u4f7f\u5f97 Goroutine \u975e\u5e38\u8f7b\u91cf\uff0c\u53ef\u4ee5\u521b\u5efa\u5927\u91cf Goroutine \u800c\u4e0d\u4f1a\u8017\u5c3d\u5185\u5b58\u3002\u521b\u5efa Goroutine \u7684\u8bed\u6cd5\u5982\u4e0b\uff1a, Channel,Channel \u662f Goroutine \u4e4b\u95f4\u901a\u4fe1\u7684\u7ba1\u9053\u3002\u5b83\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u540c\u6b65\u961f\u5217\uff0c\u5141\u8bb8\u5728\u4e00\u4e2a Goroutine \u4e2d\u53d1\u9001\u6570\u636e\u5e76\u4ece\u53e6\u4e00\u4e2a Goroutine \u4e2d\u8bfb\u53d6\u6570\u636e\u3002Channel \u7684\u8bed\u6cd5\u5982\u4e0b\uff1a, \u5b9e\u6218\u6848\u4f8b,\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 Goroutine \u548c Channel \u89e3\u51b3\u5e76\u53d1\u95ee\u9898\u7684\u793a\u4f8b\u3002\u60f3\u8c61\u4e00\u4e0b\u6709\u4e00\u4e2a\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u7684 Web \u670d\u52a1\u3002\u4e3a\u4e86\u4f18\u5316\u6027\u80fd\uff0c\u6211\u4eec\u5c06\u4f7f\u7528 Goroutine \u5e76\u884c\u5904\u7406\u6570\u636e\u3002,\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8golang\u5b66\u4e60\u7f51\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~,\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aGolang\u6846\u67b6\u5982\u4f55\u89e3\u51b3\u5e76\u53d1\u95ee\u9898\uff1f\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-40356","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40356","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=40356"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40356\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=40356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=40356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=40356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}