{"id":205575,"date":"2025-06-12T09:11:03","date_gmt":"2025-06-12T01:11:03","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205575\/"},"modified":"2025-06-12T09:11:03","modified_gmt":"2025-06-12T01:11:03","slug":"golang-%e5%87%bd%e6%95%b0%ef%bc%9agoroutine-%e4%b9%8b%e9%97%b4%e9%80%9a%e4%bf%a1%e7%9a%84%e8%89%ba%e6%9c%af","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205575\/","title":{"rendered":"Golang \u51fd\u6570\uff1agoroutine \u4e4b\u95f4\u901a\u4fe1\u7684\u827a\u672f"},"content":{"rendered":"<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662fGOLANG\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGOLANG \u51fd\u6570\uff1agoroutine \u4e4b\u95f4\u901a\u4fe1\u7684\u827a\u672f\u300b<\/span>\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01,goroutine \u4e4b\u95f4\u901a\u4fe1\u7684\u4e3b\u8981\u65b9\u5f0f\u5305\u62ec\uff1a\u901a\u9053\uff08\u5b89\u5168\u961f\u5217\uff09\u3001\u539f\u5b50\u53d8\u91cf\uff08\u4e00\u81f4\u66f4\u65b0\u548c\u8bfb\u53d6\uff09\u548c\u540c\u6b65\u539f\u8bed\uff08\u534f\u8c03\u6267\u884c\uff09\u3002\u901a\u9053\u5141\u8bb8 goroutine \u53d1\u9001\u548c\u63a5\u6536\u503c\uff0c\u539f\u5b50\u53d8\u91cf\u63d0\u4f9b\u4e00\u81f4\u7684\u66f4\u65b0\u548c\u8bfb\u53d6\uff0c\u800c\u540c\u6b65\u539f\u8bed\uff08\u5982\u4e92\u65a5\u9501\u548c\u6761\u4ef6\u53d8\u91cf\uff09\u7528\u4e8e\u534f\u8c03 goroutine \u6267\u884c\u548c\u8bbf\u95ee\u4e34\u754c\u533a\u3002,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729925976671c9358bcd2c.jpg\" class=\"aligncenter\">,<br \/>\n<strong>GOLANG \u51fd\u6570\uff1agoroutine \u4e4b\u95f4\u901a\u4fe1\u7684\u827a\u672f<\/strong>,<br \/>\n<strong>\u7b80\u4ecb<\/strong>,Goroutine \u662f Go \u8bed\u8a00\u4e2d\u5e76\u53d1\u7f16\u7a0b\u7684\u57fa\u672c\u5355\u4f4d\u3002\u5b83\u4eec\u662f\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u5e76\u884c\u6267\u884c\u800c\u4e0d\u76f8\u4e92\u963b\u585e\u3002\u4e3a\u4e86\u5728 goroutine \u4e4b\u95f4\u8fdb\u884c\u6709\u6548\u901a\u4fe1\uff0c\u9700\u8981\u4f7f\u7528\u4e00\u4e9b\u7279\u5b9a\u7684\u673a\u5236\u3002,<br \/>\n<strong>\u901a\u4fe1\u65b9\u5f0f<\/strong>,\u6709\u51e0\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5728 goroutine \u4e4b\u95f4\u8fdb\u884c\u901a\u4fe1\uff0c\u5305\u62ec\uff1a,<br \/>\n<strong>\u901a\u9053<\/strong>,\u901a\u9053\u662f goroutine \u4e4b\u95f4\u901a\u4fe1\u7684\u6700\u5e38\u7528\u65b9\u5f0f\u3002\u5b83\u4eec\u5141\u8bb8 goroutine \u5b89\u5168\u5730\u53d1\u9001\u548c\u63a5\u6536\u503c\uff0c\u800c\u4e0d\u4f1a\u53d1\u751f\u6570\u636e\u7ade\u4e89\u3002,<br \/>\n<strong>\u5982\u4f55\u4f7f\u7528\u901a\u9053\uff1a<\/strong>,<br \/>\n<strong>\u793a\u4f8b\uff1a<\/strong>,<br \/>\n<strong>\u539f\u5b50\u53d8\u91cf<\/strong>,\u539f\u5b50\u53d8\u91cf\u63d0\u4f9b\u4e86\u5728 goroutine \u4e4b\u95f4\u4e00\u81f4\u5730\u66f4\u65b0\u548c\u8bfb\u53d6\u503c\u7684\u673a\u5236\u3002\u5b83\u4eec\u4f7f\u7528\u5e95\u5c42 CPU \u6307\u4ee4\u6765\u786e\u4fdd\u64cd\u4f5c\u662f\u539f\u5b50\u6027\u7684\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4eec\u4e0d\u80fd\u88ab\u4e2d\u65ad\u3002,<br \/>\n<strong>\u5982\u4f55\u4f7f\u7528\u539f\u5b50\u53d8\u91cf\uff1a<\/strong>,<br \/>\n<strong>\u793a\u4f8b\uff1a<\/strong>,<br \/>\n<strong>\u540c\u6b65\u539f\u8bed<\/strong>,\u540c\u6b65\u539f\u8bed\u7528\u4e8e\u534f\u8c03 goroutine \u7684\u6267\u884c\u3002\u5b83\u4eec\u53ef\u4ee5\u9632\u6b62 goroutine \u6267\u884c\u4e34\u754c\u533a\uff08\u9700\u8981\u72ec\u5360\u8bbf\u95ee\u8d44\u6e90\u7684\u4ee3\u7801\u5757\uff09\uff0c\u5e76\u786e\u4fdd goroutine \u6309\u9884\u671f\u7684\u65b9\u5f0f\u5b8c\u6210\u3002,\u6700\u5e38\u89c1\u7684\u540c\u6b65\u539f\u8bed\u5305\u62ec\uff1a,<br \/>\n<strong>\u793a\u4f8b\uff1a<\/strong>,\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aGOLANG \u51fd\u6570\uff1agoroutine \u4e4b\u95f4\u901a\u4fe1\u7684\u827a\u672f\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<strong>\u6761\u4ef6\u53d8\u91cf (condition variable)<\/strong>\uff1a\u6761\u4ef6\u53d8\u91cf\u5141\u8bb8 goroutine\u7b49\u5f85\u67d0\u4e2a\u6761\u4ef6\u6ee1\u8db3\u540e\u518d\u7ee7\u7eed\u6267\u884c\u3002,<br \/>\n<strong>\u4e92\u65a5\u9501 (mutex)<\/strong>\uff1a\u4e92\u65a5\u9501\u4e00\u6b21\u53ea\u5141\u8bb8\u4e00\u4e2a goroutine\u8bbf\u95ee\u4e34\u754c\u533a\u3002,<br \/>\n<strong>\u539f\u5b50\u53d8\u91cf (atomic variables)<\/strong>\uff1a\u539f\u5b50\u53d8\u91cf\u662f\u53ef\u4ee5\u5728 goroutine \u4e4b\u95f4\u4e00\u81f4\u5730\u66f4\u65b0\u548c\u8bfb\u53d6\u7684\u53d8\u91cf\u3002,<br \/>\n<strong>\u901a\u9053 (channels)<\/strong>\uff1a\u7ba1\u9053\u662f goroutine \u4e4b\u95f4\u4f20\u9012\u6570\u636e\u7684\u5b89\u5168\u961f\u5217\u3002,<br \/>\n<strong>\u540c\u6b65\u539f\u8bed (synchronization primitives)<\/strong>\uff1a\u540c\u6b65\u539f\u8bed\u662f\u7528\u4e8e\u534f\u8c03 goroutine \u6267\u884c\u7684\u673a\u5236\uff0c\u4f8b\u5982\u4e92\u65a5\u9501\u548c\u6761\u4ef6\u53d8\u91cf\u3002,\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662fGOLANG\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGOLANG \u51fd\u6570\uff1agoroutine \u4e4b\u95f4\u901a\u4fe1\u7684\u827a\u672f\u300b<\/span>\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01,<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 \u4e4b\u95f4\u901a\u4fe1\u7684\u827a\u672f<\/span><\/p>\n<h1>GOLANG \u51fd\u6570\uff1agoroutine \u4e4b\u95f4\u901a\u4fe1\u7684\u827a\u672f<\/h1>\n<p><span>2024-10-26 14:59:51<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662fGOLANG\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGOLANG \u51fd\u6570\uff1agoroutine \u4e4b\u95f4\u901a\u4fe1\u7684\u827a\u672f\u300b<\/span>\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p>goroutine \u4e4b\u95f4\u901a\u4fe1\u7684\u4e3b\u8981\u65b9\u5f0f\u5305\u62ec\uff1a\u901a\u9053\uff08\u5b89\u5168\u961f\u5217\uff09\u3001\u539f\u5b50\u53d8\u91cf\uff08\u4e00\u81f4\u66f4\u65b0\u548c\u8bfb\u53d6\uff09\u548c\u540c\u6b65\u539f\u8bed\uff08\u534f\u8c03\u6267\u884c\uff09\u3002\u901a\u9053\u5141\u8bb8 goroutine \u53d1\u9001\u548c\u63a5\u6536\u503c\uff0c\u539f\u5b50\u53d8\u91cf\u63d0\u4f9b\u4e00\u81f4\u7684\u66f4\u65b0\u548c\u8bfb\u53d6\uff0c\u800c\u540c\u6b65\u539f\u8bed\uff08\u5982\u4e92\u65a5\u9501\u548c\u6761\u4ef6\u53d8\u91cf\uff09\u7528\u4e8e\u534f\u8c03 goroutine \u6267\u884c\u548c\u8bbf\u95ee\u4e34\u754c\u533a\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729925976671c9358bcd2c.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>GOLANG \u51fd\u6570\uff1agoroutine \u4e4b\u95f4\u901a\u4fe1\u7684\u827a\u672f<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>Goroutine \u662f Go \u8bed\u8a00\u4e2d\u5e76\u53d1\u7f16\u7a0b\u7684\u57fa\u672c\u5355\u4f4d\u3002\u5b83\u4eec\u662f\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u5e76\u884c\u6267\u884c\u800c\u4e0d\u76f8\u4e92\u963b\u585e\u3002\u4e3a\u4e86\u5728 goroutine \u4e4b\u95f4\u8fdb\u884c\u6709\u6548\u901a\u4fe1\uff0c\u9700\u8981\u4f7f\u7528\u4e00\u4e9b\u7279\u5b9a\u7684\u673a\u5236\u3002<\/p>\n<p><strong>\u901a\u4fe1\u65b9\u5f0f<\/strong><\/p>\n<p>\u6709\u51e0\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5728 goroutine \u4e4b\u95f4\u8fdb\u884c\u901a\u4fe1\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u901a\u9053 (channels)<\/strong>\uff1a\u7ba1\u9053\u662f goroutine \u4e4b\u95f4\u4f20\u9012\u6570\u636e\u7684\u5b89\u5168\u961f\u5217\u3002<\/li>\n<li><strong>\u539f\u5b50\u53d8\u91cf (atomic variables)<\/strong>\uff1a\u539f\u5b50\u53d8\u91cf\u662f\u53ef\u4ee5\u5728 goroutine \u4e4b\u95f4\u4e00\u81f4\u5730\u66f4\u65b0\u548c\u8bfb\u53d6\u7684\u53d8\u91cf\u3002<\/li>\n<li><strong>\u540c\u6b65\u539f\u8bed (synchronization primitives)<\/strong>\uff1a\u540c\u6b65\u539f\u8bed\u662f\u7528\u4e8e\u534f\u8c03 goroutine \u6267\u884c\u7684\u673a\u5236\uff0c\u4f8b\u5982\u4e92\u65a5\u9501\u548c\u6761\u4ef6\u53d8\u91cf\u3002<\/li>\n<\/ul>\n<p><strong>\u901a\u9053<\/strong><\/p>\n<p>\u901a\u9053\u662f goroutine \u4e4b\u95f4\u901a\u4fe1\u7684\u6700\u5e38\u7528\u65b9\u5f0f\u3002\u5b83\u4eec\u5141\u8bb8 goroutine \u5b89\u5168\u5730\u53d1\u9001\u548c\u63a5\u6536\u503c\uff0c\u800c\u4e0d\u4f1a\u53d1\u751f\u6570\u636e\u7ade\u4e89\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u901a\u9053\uff1a<\/strong><\/p>\n<ol>\n<li>\u4f7f\u7528 <code>make(chan T)<\/code> \u521b\u5efa\u4e00\u4e2a\u901a\u9053\uff0c\u5176\u4e2d <code>T<\/code> \u662f\u8981\u901a\u8fc7\u901a\u9053\u53d1\u9001\u7684\u503c\u7684\u7c7b\u578b\u3002<\/li>\n<li>\u4f7f\u7528 <code>chan&lt;- T<\/code> \u53d1\u9001\u503c\u5230\u901a\u9053\u3002<\/li>\n<li>\u4f7f\u7528 <code>&lt;-chan T<\/code> \u4ece\u901a\u9053\u63a5\u6536\u503c\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u6574\u6570\u901a\u9053\n    ch := make(chan int)\n\n    \/\/ \u542f\u52a8\u4e00\u4e2a goroutine \u6765\u53d1\u9001\u6570\u636e\u5230\u901a\u9053\n    go func() {\n        ch &lt;- 42\n    }()\n\n    \/\/ \u4ece\u901a\u9053\u63a5\u6536\u6570\u636e\n    value := &lt;-ch\n\n    fmt.Println(value) \/\/ \u8f93\u51fa\uff1a42\n}<\/pre>\n<p><strong>\u539f\u5b50\u53d8\u91cf<\/strong><\/p>\n<p>\u539f\u5b50\u53d8\u91cf\u63d0\u4f9b\u4e86\u5728 goroutine \u4e4b\u95f4\u4e00\u81f4\u5730\u66f4\u65b0\u548c\u8bfb\u53d6\u503c\u7684\u673a\u5236\u3002\u5b83\u4eec\u4f7f\u7528\u5e95\u5c42 CPU \u6307\u4ee4\u6765\u786e\u4fdd\u64cd\u4f5c\u662f\u539f\u5b50\u6027\u7684\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4eec\u4e0d\u80fd\u88ab\u4e2d\u65ad\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u539f\u5b50\u53d8\u91cf\uff1a<\/strong><\/p>\n<ol>\n<li>\u4f7f\u7528 <code>sync\/atomic<\/code> \u5305\u6765\u8bbf\u95ee\u539f\u5b50\u53d8\u91cf\u7c7b\u578b\uff0c\u5982 <code>int32<\/code> \u548c <code>uint64<\/code>\u3002<\/li>\n<li>\u4f7f\u7528 <code>Load()<\/code> \u548c <code>Store()<\/code> \u51fd\u6570\u6765\u8bfb\u53d6\u548c\u5199\u5165\u539f\u5b50\u53d8\u91cf\uff0c\u786e\u4fdd\u4e0d\u4f1a\u53d1\u751f\u6570\u636e\u7ade\u4e89\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"sync\/atomic\"\n)\n\nfunc main() {\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u539f\u5b50\u6574\u6570\n    var counter int32\n\n    \/\/ \u542f\u52a8\u591a\u4e2a goroutine \u6765\u9012\u589e\u8ba1\u6570\u5668\n    for i := 0; i &lt; 10; i++ {\n        go func() {\n            atomic.AddInt32(&amp;counter, 1)\n        }()\n    }\n\n    \/\/ \u7b49\u5f85\u6240\u6709 goroutine \u5b8c\u6210\n    time.Sleep(1 * time.Second)\n\n    \/\/ \u8f93\u51fa\u8ba1\u6570\u5668\u7684\u503c\n    fmt.Println(counter) \/\/ \u8f93\u51fa\uff1a10\n}<\/pre>\n<p><strong>\u540c\u6b65\u539f\u8bed<\/strong><\/p>\n<p>\u540c\u6b65\u539f\u8bed\u7528\u4e8e\u534f\u8c03 goroutine \u7684\u6267\u884c\u3002\u5b83\u4eec\u53ef\u4ee5\u9632\u6b62 goroutine \u6267\u884c\u4e34\u754c\u533a\uff08\u9700\u8981\u72ec\u5360\u8bbf\u95ee\u8d44\u6e90\u7684\u4ee3\u7801\u5757\uff09\uff0c\u5e76\u786e\u4fdd goroutine \u6309\u9884\u671f\u7684\u65b9\u5f0f\u5b8c\u6210\u3002<\/p>\n<p>\u6700\u5e38\u89c1\u7684\u540c\u6b65\u539f\u8bed\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u4e92\u65a5\u9501 (mutex)<\/strong>\uff1a\u4e92\u65a5\u9501\u4e00\u6b21\u53ea\u5141\u8bb8\u4e00\u4e2a goroutine\u8bbf\u95ee\u4e34\u754c\u533a\u3002<\/li>\n<li><strong>\u6761\u4ef6\u53d8\u91cf (condition variable)<\/strong>\uff1a\u6761\u4ef6\u53d8\u91cf\u5141\u8bb8 goroutine\u7b49\u5f85\u67d0\u4e2a\u6761\u4ef6\u6ee1\u8db3\u540e\u518d\u7ee7\u7eed\u6267\u884c\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\nfunc main() {\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u4e92\u65a5\u9501\n    mu := sync.Mutex{}\n\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u5171\u4eab\u8d44\u6e90\n    var sharedResource int\n\n    \/\/ \u542f\u52a8\u591a\u4e2a goroutine \u6765\u5e76\u53d1\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\n    for i := 0; i &lt; 10; i++ {\n        go func() {\n            mu.Lock()\n            \/\/ \u5bf9\u5171\u4eab\u8d44\u6e90\u8fdb\u884c\u64cd\u4f5c\n            sharedResource++\n            mu.Unlock()\n        }()\n    }\n\n    \/\/ \u7b49\u5f85\u6240\u6709 goroutine \u5b8c\u6210\n    time.Sleep(1 * time.Second)\n\n    \/\/ \u8f93\u51fa\u5171\u4eab\u8d44\u6e90\u7684\u503c\n    fmt.Println(sharedResource) \/\/ \u8f93\u51fa\uff1a10\n}<\/pre>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aGOLANG \u51fd\u6570\uff1agoroutine \u4e4b\u95f4\u901a\u4fe1\u7684\u827a\u672f\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   Java \u51fd\u6570\u9a8c\u8bc1\u7684\u6700\u4f73\u5b9e\u8df5\u6709\u54ea\u4e9b\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   PHP \u51fd\u6570\u6709\u54ea\u4e9b\u4e3b\u8981\u5206\u7c7b\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211&#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-205575","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205575","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=205575"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205575\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}