{"id":1447,"date":"2025-02-08T23:53:24","date_gmt":"2025-02-08T15:53:24","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1447"},"modified":"2025-02-08T23:53:25","modified_gmt":"2025-02-08T15:53:25","slug":"go%e8%af%ad%e8%a8%80%e8%bf%9b%e9%98%b6%e4%b8%8e%e4%bc%98%e5%8c%96%ef%bc%9a%e6%8f%90%e5%8d%87%e6%80%a7%e8%83%bd%e4%b8%8e%e8%b0%83%e8%af%95%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/08\/go%e8%af%ad%e8%a8%80%e8%bf%9b%e9%98%b6%e4%b8%8e%e4%bc%98%e5%8c%96%ef%bc%9a%e6%8f%90%e5%8d%87%e6%80%a7%e8%83%bd%e4%b8%8e%e8%b0%83%e8%af%95%e6%8a%80%e5%b7%a7\/","title":{"rendered":"Go\u8bed\u8a00\u8fdb\u9636\u4e0e\u4f18\u5316\uff1a\u63d0\u5347\u6027\u80fd\u4e0e\u8c03\u8bd5\u6280\u5de7"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728Go\u8bed\u8a00\u7684\u5f00\u53d1\u4e2d\uff0c\u968f\u7740\u9879\u76ee\u590d\u6742\u5ea6\u7684\u589e\u52a0\uff0c\u6027\u80fd\u4f18\u5316\u548c\u8fdb\u9636\u7f16\u7a0b\u6280\u5de7\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002\u7279\u522b\u662f\u5bf9\u4e8e\u5927\u89c4\u6a21\u7684\u5206\u5e03\u5f0f\u7cfb\u7edf\u548c\u9ad8\u5e76\u53d1\u73af\u5883\uff0c\u5982\u4f55\u9ad8\u6548\u5730\u5229\u7528Go\u7684\u5e76\u53d1\u7279\u6027\u3001\u8fdb\u884c\u5185\u5b58\u7ba1\u7406\u3001\u8c03\u4f18\u6027\u80fd\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177\u8fdb\u884c\u95ee\u9898\u8bca\u65ad\uff0c\u90fd\u662f\u5f00\u53d1\u8005\u5fc5\u987b\u638c\u63e1\u7684\u6280\u80fd\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8Go\u8bed\u8a00\u7684\u6027\u80fd\u8c03\u4f18\u3001\u5185\u5b58\u7ba1\u7406\u3001\u9ad8\u6548\u5e76\u53d1\u8bbe\u8ba1\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u8c03\u8bd5\u548c\u6027\u80fd\u5206\u6790\u5de5\u5177\u8fdb\u884c\u4ee3\u7801\u4f18\u5316\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">9.1 <strong>\u6027\u80fd\u8c03\u4f18\uff08\u5185\u5b58\u3001CPU\u4f7f\u7528\u7b49\uff09<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u4f5c\u4e3a\u4e00\u95e8\u9ad8\u6548\u7684\u7f16\u8bd1\u578b\u8bed\u8a00\uff0c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u63d0\u4f9b\u4e86\u8db3\u591f\u7684\u6027\u80fd\uff0c\u4f46\u5728\u4e00\u4e9b\u6027\u80fd\u654f\u611f\u7684\u573a\u666f\u4e0b\uff0c\u5982\u4f55\u901a\u8fc7\u8c03\u4f18\u63d0\u9ad8\u7a0b\u5e8f\u6548\u7387\uff0c\u4ecd\u7136\u662f\u4e0d\u53ef\u5ffd\u89c6\u7684\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u6027\u80fd\u8c03\u4f18\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">9.1.1 <strong>\u5185\u5b58\u4f18\u5316<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5185\u5b58\u4f18\u5316\u662f\u63d0\u5347Go\u5e94\u7528\u6027\u80fd\u7684\u91cd\u8981\u65b9\u9762\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u6570\u636e\u91cf\u6216\u9ad8\u5e76\u53d1\u65f6\u3002Go\u7684\u5783\u573e\u56de\u6536\u673a\u5236\u4f1a\u81ea\u52a8\u5904\u7406\u5185\u5b58\u7ba1\u7406\uff0c\u4f46\u6709\u65f6\u6211\u4eec\u4ecd\u9700\u624b\u52a8\u4f18\u5316\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u907f\u514d\u9891\u7e41\u7684\u5185\u5b58\u5206\u914d\uff1a<\/strong> \u5728\u9ad8\u5e76\u53d1\u7684\u60c5\u51b5\u4e0b\uff0c\u9891\u7e41\u7684\u5185\u5b58\u5206\u914d\u548c\u5783\u573e\u56de\u6536\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u74f6\u9888\u3002\u5c3d\u91cf\u91cd\u7528\u5185\u5b58\uff0c\u4f7f\u7528<code>sync.Pool<\/code>\u6c60\u5316\u5bf9\u8c61\u6765\u51cf\u5c11\u5185\u5b58\u5206\u914d\u3002 <strong>\u793a\u4f8b\uff1a\u4f7f\u7528<code>sync.Pool<\/code>\u6c60\u5316\u5bf9\u8c61<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   var myPool = sync.Pool{\n       New: func() interface{} {\n           return new(MyStruct)\n       },\n   }\n\n   \/\/ \u83b7\u53d6\u5bf9\u8c61\n   obj := myPool.Get().(*MyStruct)\n\n   \/\/ \u4f7f\u7528\u5bf9\u8c61\u540e\u8fd4\u56de\u6c60\u4e2d\n   myPool.Put(obj)<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u51cf\u5c11\u5185\u5b58\u62f7\u8d1d\uff1a<\/strong> Go\u4e2d\u7684\u5207\u7247\u548c\u6570\u7ec4\u662f\u5e38\u89c1\u7684\u5185\u5b58\u7ed3\u6784\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d\u53ef\u4ee5\u51cf\u5c11\u5185\u5b58\u6d88\u8017\u3002\u5728\u4f20\u9012\u5927\u7684\u5207\u7247\u6216\u6570\u7ec4\u65f6\uff0c\u5c3d\u91cf\u4f20\u9012\u6307\u9488\u3002<\/li>\n\n\n\n<li><strong>\u5185\u5b58\u6cc4\u6f0f\u68c0\u6d4b\uff1a<\/strong> \u5982\u679c\u7a0b\u5e8f\u4e2d\u67d0\u4e9b\u5185\u5b58\u65e0\u6cd5\u91ca\u653e\uff0c\u53ef\u80fd\u4f1a\u53d1\u751f\u5185\u5b58\u6cc4\u6f0f\u3002\u4f7f\u7528<code>pprof<\/code>\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u68c0\u6d4b\u548c\u89e3\u51b3\u5185\u5b58\u6cc4\u6f0f\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">9.1.2 <strong>CPU\u4f18\u5316<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u7684\u5e76\u53d1\u80fd\u529b\u4f7f\u5f97\u7a0b\u5e8f\u80fd\u591f\u5728\u591a\u6838\u673a\u5668\u4e0a\u8fd0\u884c\uff0c\u4f46\u5982\u4f55\u9ad8\u6548\u5730\u5229\u7528\u591a\u6838CPU\uff0c\u4e5f\u662f\u6027\u80fd\u4f18\u5316\u7684\u4e00\u90e8\u5206\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316\u6280\u5de7\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5408\u7406\u4f7f\u7528GOMAXPROCS\uff1a<\/strong> Go\u7a0b\u5e8f\u4f1a\u9ed8\u8ba4\u4f7f\u7528\u4e0e\u673a\u5668\u6838\u5fc3\u6570\u91cf\u76f8\u540c\u7684GOMAXPROCS\uff0c\u4f46\u5728\u4e00\u4e9b\u7279\u5b9a\u573a\u666f\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u624b\u52a8\u8bbe\u7f6e<code>runtime.GOMAXPROCS<\/code>\u6765\u4f18\u5316CPU\u5229\u7528\u7387\u3002 <strong>\u793a\u4f8b\uff1a\u624b\u52a8\u8bbe\u7f6eGOMAXPROCS<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   import \"runtime\"\n\n   func main() {\n       runtime.GOMAXPROCS(4) \/\/ \u8bbe\u7f6e\u7a0b\u5e8f\u6700\u591a\u4f7f\u75284\u4e2aCPU\u6838\u5fc3\n   }<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>CPU\u70ed\u5ea6\u5206\u6790\uff1a<\/strong> \u4f7f\u7528<code>pprof<\/code>\u5de5\u5177\u8fdb\u884cCPU\u5206\u6790\uff0c\u8bc6\u522b\u7a0b\u5e8f\u4e2d\u54ea\u4e9b\u90e8\u5206\u6d88\u8017\u4e86\u5927\u91cf\u7684CPU\u8d44\u6e90\u3002<\/li>\n\n\n\n<li><strong>\u51cf\u5c11\u9501\u7ade\u4e89\uff1a<\/strong> \u5982\u679c\u5728\u5e76\u53d1\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u4e86\u5927\u91cf\u7684\u9501\uff0c\u4f1a\u5bfc\u81f4\u7ebf\u7a0b\u4e89\u7528\u548cCPU\u6d88\u8017\u8fc7\u9ad8\u3002\u901a\u8fc7\u51cf\u5c11\u9501\u7c92\u5ea6\u6216\u4f7f\u7528\u65e0\u9501\u6570\u636e\u7ed3\u6784\uff08\u5982<code>sync\/atomic<\/code>\u5305\u4e2d\u7684\u539f\u5b50\u64cd\u4f5c\uff09\u6765\u51cf\u5c11\u9501\u7ade\u4e89\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">9.2 <strong>\u5185\u5b58\u7ba1\u7406\uff08\u5783\u573e\u56de\u6536\u673a\u5236\uff09<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u7684\u5783\u573e\u56de\u6536\u673a\u5236\uff08GC\uff09\u91c7\u7528\u4e86\u6807\u8bb0-\u6e05\u9664\u7684\u7b97\u6cd5\uff0c\u5b83\u80fd\u81ea\u52a8\u7ba1\u7406\u5185\u5b58\uff0c\u907f\u514d\u4e86\u5f00\u53d1\u8005\u624b\u52a8\u7ba1\u7406\u5185\u5b58\u5206\u914d\u548c\u91ca\u653e\u7684\u8d1f\u62c5\u3002\u7406\u89e3\u548c\u4f18\u5316Go\u7684GC\u884c\u4e3a\u53ef\u4ee5\u663e\u8457\u63d0\u5347\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">9.2.1 <strong>Go\u7684\u5783\u573e\u56de\u6536\u673a\u5236<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u7684\u5783\u573e\u56de\u6536\u91c7\u7528\u4e86\u201c\u4e09\u8272\u6807\u8bb0\u6cd5\u201d\uff0c\u5373\u901a\u8fc7\u6807\u8bb0\u6240\u6709\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u6765\u51b3\u5b9a\u54ea\u4e9b\u5bf9\u8c61\u5e94\u8be5\u88ab\u56de\u6536\u3002GC\u5468\u671f\u7684\u957f\u77ed\u76f4\u63a5\u5f71\u54cd\u5230\u5e94\u7528\u7684\u6027\u80fd\uff0c\u9891\u7e41\u7684GC\u53ef\u80fd\u5bfc\u81f4\u5e94\u7528\u7684\u505c\u987f\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5783\u573e\u56de\u6536\u7684\u89e6\u53d1\uff1a<\/strong> Go\u7684GC\u4f1a\u5728\u5185\u5b58\u4f7f\u7528\u8fbe\u5230\u4e00\u5b9a\u9608\u503c\u65f6\u81ea\u52a8\u89e6\u53d1\uff0c\u901a\u5e38\u901a\u8fc7<code>GOGC<\/code>\u73af\u5883\u53d8\u91cf\u63a7\u5236GC\u7684\u89e6\u53d1\u9891\u7387\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>GOGC<\/code>\u4e3a100\uff0c\u5373\u6bcf\u6b21GC\u540e\uff0c\u5806\u5185\u5b58\u4f1a\u589e\u957f\u81f3\u539f\u6765\u76842\u500d\u65f6\u518d\u89e6\u53d1\u4e00\u6b21GC\u3002 <strong>\u793a\u4f8b\uff1a\u8c03\u6574GC\u9891\u7387<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   GOGC=200 go run main.go<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u51cf\u5c11GC\u505c\u987f\uff1a<\/strong> \u901a\u8fc7\u5206\u6790\u7a0b\u5e8f\u7684\u5185\u5b58\u5206\u914d\u548cGC\u884c\u4e3a\uff0c\u53ef\u4ee5\u51cf\u5c11GC\u505c\u987f\u65f6\u95f4\u3002\u901a\u8fc7<code>runtime.GC()<\/code>\u624b\u52a8\u89e6\u53d1GC\uff0c\u6216\u8005\u4f7f\u7528<code>pprof<\/code>\u5de5\u5177\u5206\u6790GC\u505c\u987f\u548c\u5185\u5b58\u5360\u7528\u60c5\u51b5\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">9.2.2 <strong>\u5185\u5b58\u5206\u914d\u4e0e\u5bf9\u8c61\u590d\u7528<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u907f\u514d\u5927\u5bf9\u8c61\u9891\u7e41\u521b\u5efa\uff1a<\/strong> \u5927\u5bf9\u8c61\u9891\u7e41\u521b\u5efa\u548c\u9500\u6bc1\u4f1a\u589e\u52a0GC\u7684\u8d1f\u62c5\uff0c\u5c3d\u91cf\u51cf\u5c11\u5927\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u9500\u6bc1\uff0c\u907f\u514d\u9891\u7e41\u89e6\u53d1GC\u3002<\/li>\n\n\n\n<li><strong>\u5185\u5b58\u6c60\uff1a<\/strong> \u4f7f\u7528\u5185\u5b58\u6c60\uff08\u5982<code>sync.Pool<\/code>\uff09\u6765\u51cf\u5c11\u9891\u7e41\u7684\u5185\u5b58\u5206\u914d\uff0c\u5c24\u5176\u662f\u5f53\u9700\u8981\u5927\u91cf\u5c0f\u5bf9\u8c61\u65f6\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">9.3 <strong>\u9ad8\u6548\u7684\u5e76\u53d1\u8bbe\u8ba1<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u7684\u5e76\u53d1\u7f16\u7a0b\u6a21\u578b\u662f\u5176\u6700\u5927\u7279\u70b9\u4e4b\u4e00\u3002\u5408\u7406\u8bbe\u8ba1\u5e76\u53d1\u6a21\u578b\u4e0d\u4ec5\u80fd\u63d0\u9ad8\u5e94\u7528\u7684\u6027\u80fd\uff0c\u8fd8\u80fd\u907f\u514d\u5e38\u89c1\u7684\u5e76\u53d1\u95ee\u9898\uff0c\u5982\u6b7b\u9501\u548c\u7ade\u4e89\u6761\u4ef6\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">9.3.1 <strong>goroutine\u4e0echannel\u7684\u4f7f\u7528<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>goroutine\uff1a<\/strong> Go\u4e2d\u7684goroutine\u662f\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u6bcf\u4e2agoroutine\u7684\u5185\u5b58\u5360\u7528\u6781\u5c0f\uff0c\u53ef\u4ee5\u5e76\u53d1\u6267\u884c\u5927\u91cf\u7684\u4efb\u52a1\u3002 <strong>\u793a\u4f8b\uff1a\u4f7f\u7528goroutine\u5e76\u53d1\u6267\u884c\u4efb\u52a1<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   go func() {\n       fmt.Println(\"Hello from goroutine!\")\n   }()<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>channel\uff1a<\/strong> Go\u7684channel\u7528\u4e8e\u5728\u591a\u4e2agoroutine\u4e4b\u95f4\u4f20\u9012\u6570\u636e\uff0c\u5b83\u662fGo\u5e76\u53d1\u7684\u6838\u5fc3\u3002\u5408\u7406\u4f7f\u7528channel\u53ef\u4ee5\u6709\u6548\u5730\u540c\u6b65goroutine\u7684\u72b6\u6001\u3002 <strong>\u793a\u4f8b\uff1a\u4f7f\u7528channel\u5728\u591a\u4e2agoroutine\u4e4b\u95f4\u4f20\u9012\u6570\u636e<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   ch := make(chan int)\n\n   go func() {\n       ch &lt;- 42 \/\/ \u5c06\u6570\u636e\u53d1\u9001\u5230channel\n   }()\n\n   result := &lt;-ch \/\/ \u4ecechannel\u63a5\u6536\u6570\u636e\n   fmt.Println(result)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">9.3.2 <strong>\u5e76\u53d1\u8bbe\u8ba1\u6a21\u5f0f<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u5f0f\uff1a<\/strong> \u751f\u4ea7\u8005\u751f\u4ea7\u6570\u636e\uff0c\u6d88\u8d39\u8005\u5904\u7406\u6570\u636e\uff0c\u53cc\u65b9\u901a\u8fc7channel\u8fdb\u884c\u901a\u4fe1\u3002 <strong>\u793a\u4f8b\uff1a\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u5f0f<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   func producer(ch chan&lt;- int) {\n       for i := 0; i &lt; 5; i++ {\n           ch &lt;- i\n       }\n   }\n\n   func consumer(ch &lt;-chan int) {\n       for value := range ch {\n           fmt.Println(value)\n       }\n   }\n\n   func main() {\n       ch := make(chan int)\n       go producer(ch)\n       go consumer(ch)\n       time.Sleep(time.Second)\n   }<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Worker Pool\u6a21\u5f0f\uff1a<\/strong> \u901a\u8fc7\u591a\u4e2a\u5de5\u4f5cgoroutine\u6c60\u5316\u4efb\u52a1\uff0c\u907f\u514d\u8fc7\u591a\u7684goroutine\u521b\u5efa\u548c\u9500\u6bc1\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002 <strong>\u793a\u4f8b\uff1aWorker Pool\u6a21\u5f0f<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   func worker(id int, jobs &lt;-chan int, results chan&lt;- int) {\n       for j := range jobs {\n           fmt.Printf(\"Worker %d processing job %d\\n\", id, j)\n           results &lt;- j * 2\n       }\n   }\n\n   func main() {\n       jobs := make(chan int, 100)\n       results := make(chan int, 100)\n\n       \/\/ \u542f\u52a8\u591a\u4e2aworker\n       for w := 1; w &lt;= 3; w++ {\n           go worker(w, jobs, results)\n       }\n\n       \/\/ \u53d1\u9001\u4efb\u52a1\n       for j := 1; j &lt;= 5; j++ {\n           jobs &lt;- j\n       }\n       close(jobs)\n\n       \/\/ \u83b7\u53d6\u7ed3\u679c\n       for a := 1; a &lt;= 5; a++ {\n           fmt.Println(&lt;-results)\n       }\n   }<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">9.4 <strong>\u8c03\u8bd5\u4e0e\u6027\u80fd\u5206\u6790\u5de5\u5177<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u63d0\u4f9b\u4e86\u591a\u4e2a\u5185\u5efa\u7684\u8c03\u8bd5\u548c\u6027\u80fd\u5206\u6790\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5206\u6790\u7a0b\u5e8f\u74f6\u9888\uff0c\u53d1\u73b0\u6027\u80fd\u95ee\u9898\uff0c\u5e76\u5bf9\u4ee3\u7801\u8fdb\u884c\u4f18\u5316\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">9.4.1 <strong>pprof\uff1a\u6027\u80fd\u5206\u6790\u5de5\u5177<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>pprof<\/code>\u662fGo\u8bed\u8a00\u5185\u7f6e\u7684\u6027\u80fd\u5206\u6790\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5206\u6790CPU\u3001\u5185\u5b58\u3001goroutine\u7b49\u7684\u4f7f\u7528\u60c5\u51b5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u542f\u7528pprof\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import (\n    \"net\/http\"\n    _ \"net\/http\/pprof\"\n)\n\nfunc main() {\n    go func() {\n        log.Println(http.ListenAndServe(\"localhost:6060\", nil))\n    }()\n    \/\/ Your program logic\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u8bbf\u95ee<code>http:\/\/localhost:6060\/debug\/pprof\/<\/code>\u53ef\u4ee5\u67e5\u770b\u5404\u7c7b\u6027\u80fd\u6570\u636e\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">9.4.2 <strong>Go tool trace\uff1a\u8ffd\u8e2a\u5de5\u5177<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u7684<code>trace<\/code>\u5de5\u5177\u53ef\u4ee5\u7528\u4e8e\u5206\u6790\u7a0b\u5e8f\u7684\u6267\u884c\u8fc7\u7a0b\u3001goroutine\u7684\u8c03\u5ea6\u7b49\u4fe1\u606f\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u6df1\u5165\u4e86\u89e3\u5e76\u53d1\u7a0b\u5e8f\u7684\u884c\u4e3a\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a\u751f\u6210\u548c\u67e5\u770btrace\u6570\u636e<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>go run main.go\ngo tool trace trace.out<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u63d0\u4f9b\u4e86\u591a\u79cd\u4f18\u5316\u548c\u8c03\u4f18\u6027\u80fd\u7684\u624b\u6bb5\uff0c\u4ece\u5185\u5b58\u7ba1\u7406\u3001CPU\u4f7f\u7528\u5230\u5e76\u53d1\u8bbe\u8ba1\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u901a\u8fc7\u5408\u7406\u7684\u65b9\u5f0f\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002\u540c\u65f6\uff0cGo\u4e5f\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u8c03\u8bd5\u4e0e\u6027\u80fd\u5206\u6790\u5de5\u5177\uff0c\u5982<code>pprof<\/code>\u548c<code>trace<\/code>\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u8bca\u65ad\u6027\u80fd\u74f6\u9888\u3002\u5bf9\u4e8e\u5927\u89c4\u6a21\u5206\u5e03\u5f0f\u7cfb\u7edf\u5f00\u53d1\uff0cGo\u7684\u5e76\u53d1\u6a21\u578b\u548c\u9ad8\u6548\u7684\u5185\u5b58\u7ba1\u7406\u4f7f\u5f97\u5b83\u6210\u4e3a\u6784\u5efa\u9ad8\u6027\u80fd\u7cfb\u7edf\u7684\u7406\u60f3\u9009\u62e9\u3002\u638c\u63e1\u8fd9\u4e9b\u8fdb\u9636\u6280\u5de7\u548c\u5de5\u5177\uff0c\u5c06\u6781\u5927\u63d0\u5347\u4f60\u7684Go\u5f00\u53d1\u6280\u80fd\uff0c\u5e2e\u52a9\u4f60\u6784\u5efa\u51fa\u66f4\u52a0\u9ad8\u6548\u3001\u7a33\u5b9a\u7684\u5e94\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Go\u8bed\u8a00\u7684\u5f00\u53d1\u4e2d\uff0c\u968f\u7740\u9879\u76ee\u590d\u6742\u5ea6\u7684\u589e\u52a0\uff0c\u6027\u80fd\u4f18\u5316\u548c\u8fdb\u9636\u7f16\u7a0b\u6280\u5de7\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002\u7279\u522b\u662f\u5bf9\u4e8e\u5927\u89c4\u6a21\u7684\u5206\u5e03\u5f0f\u7cfb\u7edf\u548c\u9ad8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[],"class_list":["post-1447","post","type-post","status-publish","format-standard","hentry","category-golang"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1447","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1447"}],"version-history":[{"count":2,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1447\/revisions"}],"predecessor-version":[{"id":1461,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1447\/revisions\/1461"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}