{"id":205885,"date":"2025-06-12T12:33:01","date_gmt":"2025-06-12T04:33:01","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205885\/"},"modified":"2025-06-12T12:33:01","modified_gmt":"2025-06-12T04:33:01","slug":"golang-%e5%87%bd%e6%95%b0%e5%93%8d%e5%ba%94%e5%bb%b6%e8%bf%9f%e4%bc%98%e5%8c%96%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205885\/","title":{"rendered":"Golang \u51fd\u6570\u54cd\u5e94\u5ef6\u8fdf\u4f18\u5316\u6700\u4f73\u5b9e\u8df5"},"content":{"rendered":"<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9GOLANG\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300aGOLANG \u51fd\u6570\u54cd\u5e94\u5ef6\u8fdf\u4f18\u5316\u6700\u4f73\u5b9e\u8df5\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01,\u6700\u4f73\u5b9e\u8df5\u53ef\u663e\u8457\u4f18\u5316 Go \u51fd\u6570\u54cd\u5e94\u65f6\u95f4\uff0c\u63d0\u5347\u6027\u80fd\u548c\u7528\u6237\u4f53\u9a8c\uff1a\u5e76\u53d1\u6027\uff1a\u4f7f\u7528 goroutine \u5e76\u884c\u6267\u884c\u8017\u65f6\u4efb\u52a1\u3002\u4ee3\u7801\u5206\u6790\uff1a\u4f7f\u7528 pprof \u8bc6\u522b\u6027\u80fd\u74f6\u9888\u548c\u4f18\u5316\u8017\u65f6\u64cd\u4f5c\u3002\u7f13\u5b58\uff1a\u4f7f\u7528\u5e76\u53d1\u5b89\u5168\u4e14\u6709\u6548\u7684\u7f13\u5b58\uff0c\u5982 sync.Map\uff0c\u5bf9\u7ecf\u5e38\u8bbf\u95ee\u7684\u6570\u636e\u8fdb\u884c\u7f13\u5b58\u3002\u975e\u963b\u585e I\/O\uff1a\u4f7f\u7528\u975e\u963b\u585e I\/O \u673a\u5236\uff0c\u5982 net\/http\uff0c\u907f\u514d\u963b\u585e\u5728\u5355\u4e2a goroutine \u4e2d\u3002\u8d44\u6e90\u9650\u5236\uff1a\u9650\u5236\u51fd\u6570\u6267\u884c\u65f6\u95f4\uff0c\u9632\u6b62\u957f\u5c3e\u8c03\u7528\u548c\u8d44\u6e90\u6cc4\u6f0f\u3002,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730028504671e23d83577e.jpg\" class=\"aligncenter\">,<br \/>\n<strong>Go \u51fd\u6570\u54cd\u5e94\u5ef6\u8fdf\u4f18\u5316\u6700\u4f73\u5b9e\u8df5<\/strong>,\u5ef6\u8fdf\u6216\u7f13\u6162\u7684\u51fd\u6570\u54cd\u5e94\u4f1a\u5f71\u54cd\u7528\u6237\u4f53\u9a8c\u5e76\u5371\u5bb3\u7cfb\u7edf\u7684\u603b\u4f53\u6027\u80fd\u3002\u4f18\u5316 Go \u51fd\u6570\u7684\u54cd\u5e94\u5ef6\u8fdf\u81f3\u5173\u91cd\u8981\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6700\u4f73\u5b9e\u8df5\u6765\u5b9e\u73b0\uff1a,<br \/>\n<strong>\u5e76\u53d1\u6027<\/strong>,<br \/>\n<strong>\u4ee3\u7801\u5206\u6790<\/strong>,<br \/>\n<strong>\u7f13\u5b58<\/strong>,<br \/>\n<strong>\u975e\u963b\u585e I\/O<\/strong>,<br \/>\n<strong>\u8d44\u6e90\u9650\u5236<\/strong>,<br \/>\n<strong>\u5b9e\u6218\u6848\u4f8b<\/strong>,\u8003\u8651\u4ee5\u4e0b\u793a\u4f8b\u51fd\u6570\uff0c\u5b83\u4ece\u6570\u636e\u5e93\u8bfb\u53d6\u6570\u636e\u5e76\u8fd4\u56de\u4e00\u4e2a HTTP \u54cd\u5e94\uff1a,<br \/>\n<strong>\u4f18\u5316<\/strong>,\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4f18\u5316\u6765\u51cf\u5c11\u51fd\u6570\u7684\u5ef6\u8fdf\uff1a,\u4f18\u5316\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a,\u901a\u8fc7\u5e94\u7528\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u60a8\u53ef\u4ee5\u663e\u7740\u4f18\u5316 Go \u51fd\u6570\u7684\u54cd\u5e94\u5ef6\u8fdf\uff0c\u4ece\u800c\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6574\u4f53\u6027\u80fd\u548c\u7528\u6237\u4f53\u9a8c\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\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~,\u6807\u8bc6\u8017\u65f6\u7684\u64cd\u4f5c\u5e76\u8fdb\u884c\u4f18\u5316\u3002,\u4f7f\u7528 pprof \u5206\u6790\u51fd\u6570\u7684\u6027\u80fd\u74f6\u9888\u3002,\u901a\u8fc7 channel \u540c\u6b65\u5e76\u4f20\u9012\u7ed3\u679c\u3002,\u4f7f\u7528 goroutine \u5728\u4e0d\u540c\u7684 CPU \u5185\u6838\u4e0a\u5e76\u884c\u8fd0\u884c\u8017\u65f6\u7684\u4efb\u52a1\u3002,\u5c0f\u4f19\u4f34\u4eec\u5bf9GOLANG\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300aGOLANG \u51fd\u6570\u54cd\u5e94\u5ef6\u8fdf\u4f18\u5316\u6700\u4f73\u5b9e\u8df5\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\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\u54cd\u5e94\u5ef6\u8fdf\u4f18\u5316\u6700\u4f73\u5b9e\u8df5<\/span><\/p>\n<h1>GOLANG \u51fd\u6570\u54cd\u5e94\u5ef6\u8fdf\u4f18\u5316\u6700\u4f73\u5b9e\u8df5<\/h1>\n<p><span>2024-10-27 19:28:39<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9GOLANG\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300aGOLANG \u51fd\u6570\u54cd\u5e94\u5ef6\u8fdf\u4f18\u5316\u6700\u4f73\u5b9e\u8df5\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>\u6700\u4f73\u5b9e\u8df5\u53ef\u663e\u8457\u4f18\u5316 Go \u51fd\u6570\u54cd\u5e94\u65f6\u95f4\uff0c\u63d0\u5347\u6027\u80fd\u548c\u7528\u6237\u4f53\u9a8c\uff1a\u5e76\u53d1\u6027\uff1a\u4f7f\u7528 goroutine \u5e76\u884c\u6267\u884c\u8017\u65f6\u4efb\u52a1\u3002\u4ee3\u7801\u5206\u6790\uff1a\u4f7f\u7528 pprof \u8bc6\u522b\u6027\u80fd\u74f6\u9888\u548c\u4f18\u5316\u8017\u65f6\u64cd\u4f5c\u3002\u7f13\u5b58\uff1a\u4f7f\u7528\u5e76\u53d1\u5b89\u5168\u4e14\u6709\u6548\u7684\u7f13\u5b58\uff0c\u5982 sync.Map\uff0c\u5bf9\u7ecf\u5e38\u8bbf\u95ee\u7684\u6570\u636e\u8fdb\u884c\u7f13\u5b58\u3002\u975e\u963b\u585e I\/O\uff1a\u4f7f\u7528\u975e\u963b\u585e I\/O \u673a\u5236\uff0c\u5982 net\/http\uff0c\u907f\u514d\u963b\u585e\u5728\u5355\u4e2a goroutine \u4e2d\u3002\u8d44\u6e90\u9650\u5236\uff1a\u9650\u5236\u51fd\u6570\u6267\u884c\u65f6\u95f4\uff0c\u9632\u6b62\u957f\u5c3e\u8c03\u7528\u548c\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730028504671e23d83577e.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Go \u51fd\u6570\u54cd\u5e94\u5ef6\u8fdf\u4f18\u5316\u6700\u4f73\u5b9e\u8df5<\/strong><\/p>\n<p>\u5ef6\u8fdf\u6216\u7f13\u6162\u7684\u51fd\u6570\u54cd\u5e94\u4f1a\u5f71\u54cd\u7528\u6237\u4f53\u9a8c\u5e76\u5371\u5bb3\u7cfb\u7edf\u7684\u603b\u4f53\u6027\u80fd\u3002\u4f18\u5316 Go \u51fd\u6570\u7684\u54cd\u5e94\u5ef6\u8fdf\u81f3\u5173\u91cd\u8981\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6700\u4f73\u5b9e\u8df5\u6765\u5b9e\u73b0\uff1a<\/p>\n<p><strong>\u5e76\u53d1\u6027<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 goroutine \u5728\u4e0d\u540c\u7684 CPU \u5185\u6838\u4e0a\u5e76\u884c\u8fd0\u884c\u8017\u65f6\u7684\u4efb\u52a1\u3002<\/li>\n<li>\u901a\u8fc7 channel \u540c\u6b65\u5e76\u4f20\u9012\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<p><strong>\u4ee3\u7801\u5206\u6790<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 pprof \u5206\u6790\u51fd\u6570\u7684\u6027\u80fd\u74f6\u9888\u3002<\/li>\n<li>\u6807\u8bc6\u8017\u65f6\u7684\u64cd\u4f5c\u5e76\u8fdb\u884c\u4f18\u5316\u3002<\/li>\n<\/ul>\n<p><strong>\u7f13\u5b58<\/strong><\/p>\n<ul>\n<li>\u5bf9\u7ecf\u5e38\u8bbf\u95ee\u7684\u6570\u636e\u8fdb\u884c\u7f13\u5b58\uff0c\u4ee5\u907f\u514d\u91cd\u590d\u8c03\u7528\u6570\u636e\u5e93\u6216\u5176\u4ed6\u7f13\u6162\u7684\u8d44\u6e90\u3002<\/li>\n<li>\u4f7f\u7528\u5e76\u53d1\u5b89\u5168\u4e14\u6709\u6548\u7684\u7f13\u5b58\u5e93\uff0c\u5982 sync.Map\u3002<\/li>\n<\/ul>\n<p><strong>\u975e\u963b\u585e I\/O<\/strong><\/p>\n<ul>\n<li>\u5bf9\u4e8e I\/O \u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u4f7f\u7528\u975e\u963b\u585e I\/O \u673a\u5236\uff0c\u5982 net\/http\u3001sync\/Mutex \u548c rwlock\u3002<\/li>\n<li>\u907f\u514d\u5728\u540c\u4e00\u4e2a goroutine \u4e2d\u8fdb\u884c\u963b\u585e\u8c03\u7528\u3002<\/li>\n<\/ul>\n<p><strong>\u8d44\u6e90\u9650\u5236<\/strong><\/p>\n<ul>\n<li>\u9650\u5236\u51fd\u6570\u6267\u884c\u65f6\u95f4\uff0c\u4ee5\u9632\u6b62\u957f\u5c3e\u8c03\u7528\u548c\u8d44\u6e90\u6cc4\u6f0f\u3002<\/li>\n<li>\u5728\u51fd\u6570\u8fd4\u56de\u6216 panic \u65f6\u6e05\u7406\u8d44\u6e90\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u793a\u4f8b\u51fd\u6570\uff0c\u5b83\u4ece\u6570\u636e\u5e93\u8bfb\u53d6\u6570\u636e\u5e76\u8fd4\u56de\u4e00\u4e2a HTTP \u54cd\u5e94\uff1a<\/p>\n<pre>func GetUserData(w http.ResponseWriter, r *http.Request) {\n    \/\/ \u4ece\u6570\u636e\u5e93\u8bfb\u53d6\u6570\u636e\n    data, err := db.GetUser(r.FormValue(\"id\"))\n    if err != nil {\n        http.Error(w, err.Error(), http.StatusInternalServerError)\n        return\n    }\n\n    \/\/ \u6784\u5efa HTTP \u54cd\u5e94\n    response := fmt.Sprintf(\"user: %v\", data)\n    w.Write([]byte(response))\n}<\/pre>\n<p><strong>\u4f18\u5316<\/strong><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4f18\u5316\u6765\u51cf\u5c11\u51fd\u6570\u7684\u5ef6\u8fdf\uff1a<\/p>\n<ul>\n<li>\u5c06<code>db.GetUser<\/code>\u8c03\u7528\u79fb\u5230\u4e00\u4e2a\u5355\u72ec\u7684 goroutine \u4e2d\uff0c\u4ee5\u5e76\u884c\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/li>\n<li>\u4f7f\u7528<code>sync.Mutex<\/code>\u6216<code>rwlock<\/code>\u5bf9<code>data<\/code>\u53d8\u91cf\u8fdb\u884c\u5e76\u53d1\u63a7\u5236\u3002<\/li>\n<li>\u7f13\u5b58\u6700\u8fd1\u83b7\u53d6\u7684\u7528\u6237\u6570\u636e\uff0c\u4ee5\u907f\u514d\u91cd\u590d\u6570\u636e\u5e93\u8c03\u7528\u3002<\/li>\n<\/ul>\n<p>\u4f18\u5316\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>var userCache = sync.Map{}\n\nfunc GetUserData(w http.ResponseWriter, r *http.Request) {\n    id := r.FormValue(\"id\")\n\n    \/\/ \u4ece\u7f13\u5b58\u4e2d\u83b7\u53d6\u6570\u636e\n    if cachedData, ok := userCache.Load(id); ok {\n        response := fmt.Sprintf(\"user: %v\", cachedData)\n        w.Write([]byte(response))\n        return\n    }\n\n    \/\/ \u4ece\u6570\u636e\u5e93\u8bfb\u53d6\u6570\u636e\n    go func() {\n        data, err := db.GetUser(id)\n        if err != nil {\n            http.Error(w, err.Error(), http.StatusInternalServerError)\n            return\n        }\n\n        \/\/ \u66f4\u65b0\u7f13\u5b58\n        userCache.Store(id, data)\n    }()\n\n    \/\/ \u6784\u5efa HTTP \u54cd\u5e94\n    response := fmt.Sprintf(\"user: placeholder\")\n    w.Write([]byte(response))\n}<\/pre>\n<p>\u901a\u8fc7\u5e94\u7528\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u60a8\u53ef\u4ee5\u663e\u7740\u4f18\u5316 Go \u51fd\u6570\u7684\u54cd\u5e94\u5ef6\u8fdf\uff0c\u4ece\u800c\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6574\u4f53\u6027\u80fd\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\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\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u4eba\u4eba\u89c6\u9891\u6709\u7535\u8111\u7248\u5417\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u638c\u63e1 PHP \u51fd\u6570\u7684\u5185\u5b58\u7ba1\u7406\u6280\u5de7\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9GOLANG\u7f16\u7a0b\u611f\u5174\u8da3&#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-205885","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205885","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=205885"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205885\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}