{"id":205822,"date":"2025-06-12T09:19:06","date_gmt":"2025-06-12T01:19:06","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205822\/"},"modified":"2025-06-12T09:19:06","modified_gmt":"2025-06-12T01:19:06","slug":"%e5%a6%82%e4%bd%95%e5%87%8f%e5%b0%91-golang-%e5%87%bd%e6%95%b0%e8%b0%83%e7%94%a8%e5%bc%80%e9%94%80","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205822\/","title":{"rendered":"\u5982\u4f55\u51cf\u5c11 Golang \u51fd\u6570\u8c03\u7528\u5f00\u9500"},"content":{"rendered":"<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929GOLANG\u5c31\u6574\u7406\u5206\u4eab\u300a\u5982\u4f55\u51cf\u5c11 GOLANG \u51fd\u6570\u8c03\u7528\u5f00\u9500\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9GOLANG\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7GOLANG\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002,\u5728 GOLANG \u4e2d\uff0c\u51cf\u5c11\u51fd\u6570\u8c03\u7528\u5f00\u9500\u7684\u65b9\u6cd5\u5305\u62ec\uff1a1. \u5185\u8054\u51fd\u6570\uff1b2. \u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\uff1b3. \u4f7f\u7528\u51fd\u6570\u503c\uff1b4. \u51cf\u5c11\u53c2\u6570\u6570\u91cf\u3002\u901a\u8fc7\u5e94\u7528\u8fd9\u4e9b\u6280\u672f\uff0c\u53ef\u4ee5\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u3002,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730009205671dd875edd8e.jpg\" class=\"aligncenter\">,<br \/>\n<strong>\u5982\u4f55\u5728 GOLANG \u51fd\u6570\u8c03\u7528\u4e2d\u51cf\u5c11\u5f00\u9500<\/strong>,\u5728 GOLANG \u4e2d\uff0c\u51fd\u6570\u8c03\u7528\u4f1a\u5e26\u6765\u5f00\u9500\uff0c\u8fd9\u53ef\u80fd\u5728\u6027\u80fd\u654f\u611f\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6210\u4e3a\u74f6\u9888\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316 GOLANG \u51fd\u6570\u8c03\u7528\u5f00\u9500\u7684\u6280\u672f\uff1a,<br \/>\n<strong>1. \u5185\u8054\u51fd\u6570<\/strong>,\u5185\u8054\u51fd\u6570\u901a\u8fc7\u5c06\u51fd\u6570\u4f53\u76f4\u63a5\u590d\u5236\u5230\u8c03\u7528\u5b83\u7684\u4f4d\u7f6e\u6765\u6d88\u9664\u51fd\u6570\u8c03\u7528\u7684\u5f00\u9500\u3002\u8fd9\u5bf9\u4e8e\u5c0f\u51fd\u6570\u6216\u90a3\u4e9b\u7ecf\u5e38\u8c03\u7528\u7684\u51fd\u6570\u7279\u522b\u6709\u6548\u3002\u53ef\u4ee5\u4f7f\u7528<br \/>\n<code>inline<\/code> \u7f16\u8bd1\u5668\u6807\u5fd7\u6765\u542f\u7528\u5185\u8054\u3002,<br \/>\n<strong>2. \u4f7f\u7528\u5c40\u90e8\u53d8\u91cf<\/strong>,\u5c06\u53c2\u6570\u4f5c\u4e3a\u5c40\u90e8\u53d8\u91cf\u5b58\u50a8\u53ef\u4ee5\u51cf\u5c11\u51fd\u6570\u8c03\u7528\u671f\u95f4\u4f20\u9012\u503c\u7684\u5f00\u9500\u3002,<br \/>\n<strong>3. \u51fd\u6570\u503c<\/strong>,\u51fd\u6570\u503c\u5141\u8bb8\u5c06\u51fd\u6570\u4f5c\u4e3a\u5176\u4ed6\u51fd\u6570\u7684\u53c2\u6570\u4f20\u9012\u3002\u8fd9\u53ef\u4ee5\u907f\u514d\u5728\u6bcf\u6b21\u8c03\u7528\u51fd\u6570\u65f6\u521b\u5efa\u65b0\u51fd\u6570\u5bf9\u8c61\u3002,<br \/>\n<strong>4. \u51cf\u5c11\u53c2\u6570\u6570\u91cf<\/strong>,\u51fd\u6570\u7684\u53c2\u6570\u6570\u91cf\u8d8a\u591a\uff0c\u51fd\u6570\u8c03\u7528\u7684\u5f00\u9500\u5c31\u8d8a\u5927\u3002\u5c3d\u53ef\u80fd\u51cf\u5c11\u51fd\u6570\u7684\u53c2\u6570\u6570\u91cf\u3002,<br \/>\n<strong>\u5b9e\u6218\u6848\u4f8b<\/strong>,\u5728\u4e0b\u9762\u8fd9\u4e2a\u57fa\u51c6\u6d4b\u8bd5\u4e2d\uff0c\u6211\u4eec\u6bd4\u8f83\u4e86\u5185\u8054\u51fd\u6570\u548c\u975e\u5185\u8054\u51fd\u6570\u7684\u6027\u80fd\uff1a,\u7ed3\u679c\uff1a,\u6b63\u5982\u6211\u4eec\u6240\u89c1\u7684\uff0c\u5185\u8054\u51fd\u6570\u7684\u5f00\u9500\u663e\u8457\u4f4e\u4e8e\u51fd\u6570\u8c03\u7528\u3002,\u901a\u8fc7\u5e94\u7528\u8fd9\u4e9b\u6280\u672f\uff0c\u4f60\u53ef\u4ee5\u5728 GOLANG \u5e94\u7528\u7a0b\u5e8f\u4e2d\u51cf\u5c11\u51fd\u6570\u8c03\u7528\u5f00\u9500\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002,\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u51cf\u5c11 GOLANG \u51fd\u6570\u8c03\u7528\u5f00\u9500\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8eGOLANG\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff01,\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929GOLANG\u5c31\u6574\u7406\u5206\u4eab\u300a\u5982\u4f55\u51cf\u5c11 GOLANG \u51fd\u6570\u8c03\u7528\u5f00\u9500\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9GOLANG\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7GOLANG\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\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>\u5982\u4f55\u51cf\u5c11 GOLANG \u51fd\u6570\u8c03\u7528\u5f00\u9500<\/span><\/p>\n<h1>\u5982\u4f55\u51cf\u5c11 GOLANG \u51fd\u6570\u8c03\u7528\u5f00\u9500<\/h1>\n<p><span>2024-10-27 14:07:01<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929GOLANG\u5c31\u6574\u7406\u5206\u4eab\u300a\u5982\u4f55\u51cf\u5c11 GOLANG \u51fd\u6570\u8c03\u7528\u5f00\u9500\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9GOLANG\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7GOLANG\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p>\u5728 GOLANG \u4e2d\uff0c\u51cf\u5c11\u51fd\u6570\u8c03\u7528\u5f00\u9500\u7684\u65b9\u6cd5\u5305\u62ec\uff1a1. \u5185\u8054\u51fd\u6570\uff1b2. \u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\uff1b3. \u4f7f\u7528\u51fd\u6570\u503c\uff1b4. \u51cf\u5c11\u53c2\u6570\u6570\u91cf\u3002\u901a\u8fc7\u5e94\u7528\u8fd9\u4e9b\u6280\u672f\uff0c\u53ef\u4ee5\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730009205671dd875edd8e.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u5728 GOLANG \u51fd\u6570\u8c03\u7528\u4e2d\u51cf\u5c11\u5f00\u9500<\/strong><\/p>\n<p>\u5728 GOLANG \u4e2d\uff0c\u51fd\u6570\u8c03\u7528\u4f1a\u5e26\u6765\u5f00\u9500\uff0c\u8fd9\u53ef\u80fd\u5728\u6027\u80fd\u654f\u611f\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6210\u4e3a\u74f6\u9888\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316 GOLANG \u51fd\u6570\u8c03\u7528\u5f00\u9500\u7684\u6280\u672f\uff1a<\/p>\n<p><strong>1. \u5185\u8054\u51fd\u6570<\/strong><\/p>\n<p>\u5185\u8054\u51fd\u6570\u901a\u8fc7\u5c06\u51fd\u6570\u4f53\u76f4\u63a5\u590d\u5236\u5230\u8c03\u7528\u5b83\u7684\u4f4d\u7f6e\u6765\u6d88\u9664\u51fd\u6570\u8c03\u7528\u7684\u5f00\u9500\u3002\u8fd9\u5bf9\u4e8e\u5c0f\u51fd\u6570\u6216\u90a3\u4e9b\u7ecf\u5e38\u8c03\u7528\u7684\u51fd\u6570\u7279\u522b\u6709\u6548\u3002\u53ef\u4ee5\u4f7f\u7528 <code>inline<\/code> \u7f16\u8bd1\u5668\u6807\u5fd7\u6765\u542f\u7528\u5185\u8054\u3002<\/p>\n<pre>\/\/ myfunc.go\npackage mypkg\n\nfunc MyFunc(a int) int {\n    return a + 1\n}\n\n\/\/ main.go\npackage main\n\nimport \"mypkg\"\n\nvar r int\nfunc main() {\n    \/\/ \u5185\u8054 MyFunc\n    r = mypkg.MyFunc(42)\n    \/\/ r \u7684\u503c\u73b0\u5728\u4e3a 43\n}<\/pre>\n<p><strong>2. \u4f7f\u7528\u5c40\u90e8\u53d8\u91cf<\/strong><\/p>\n<p>\u5c06\u53c2\u6570\u4f5c\u4e3a\u5c40\u90e8\u53d8\u91cf\u5b58\u50a8\u53ef\u4ee5\u51cf\u5c11\u51fd\u6570\u8c03\u7528\u671f\u95f4\u4f20\u9012\u503c\u7684\u5f00\u9500\u3002<\/p>\n<pre>\/\/ myfunc.go\npackage mypkg\n\nfunc MyFunc(a, b int) int {\n    tmp := a + b\n    return tmp\n}\n\n\/\/ main.go\npackage main\n\nimport \"mypkg\"\n\nfunc main() {\n    \/\/ \u5b58\u50a8\u53c2\u6570\u4f5c\u4e3a\u5c40\u90e8\u53d8\u91cf\n    tmp := 1\n    tmp += 2\n    \n    r := mypkg.MyFunc(tmp, 3)\n    \/\/ r \u7684\u503c\u73b0\u5728\u4e3a 6\n}<\/pre>\n<p><strong>3. \u51fd\u6570\u503c<\/strong><\/p>\n<p>\u51fd\u6570\u503c\u5141\u8bb8\u5c06\u51fd\u6570\u4f5c\u4e3a\u5176\u4ed6\u51fd\u6570\u7684\u53c2\u6570\u4f20\u9012\u3002\u8fd9\u53ef\u4ee5\u907f\u514d\u5728\u6bcf\u6b21\u8c03\u7528\u51fd\u6570\u65f6\u521b\u5efa\u65b0\u51fd\u6570\u5bf9\u8c61\u3002<\/p>\n<pre>package main\n\nimport \"fmt\"\n\nfunc MyFunc(a int) int {\n    return a + 1\n}\n\nfunc main() {\n    f := MyFunc\n    r := f(42)\n    \n    fmt.Println(r) \/\/ \u8f93\u51fa\uff1a43\n}<\/pre>\n<p><strong>4. \u51cf\u5c11\u53c2\u6570\u6570\u91cf<\/strong><\/p>\n<p>\u51fd\u6570\u7684\u53c2\u6570\u6570\u91cf\u8d8a\u591a\uff0c\u51fd\u6570\u8c03\u7528\u7684\u5f00\u9500\u5c31\u8d8a\u5927\u3002\u5c3d\u53ef\u80fd\u51cf\u5c11\u51fd\u6570\u7684\u53c2\u6570\u6570\u91cf\u3002<\/p>\n<pre>\/\/ \u51fd\u6570\u5177\u6709\u8fc7\u591a\u53c2\u6570\nfunc MyFunc(a, b, c, d int) int {\n    return a + b + c + d\n}\n\n\/\/ \u51cf\u5c11\u53c2\u6570\u6570\u91cf\uff0c\u5c06\u6240\u6709\u53c2\u6570\u5c01\u88c5\u4e3a\u4e00\u4e2a\u7ed3\u6784\u4f53\ntype MyArgs struct {\n    A, B, C, D int\n}\n\nfunc MyFunc(args MyArgs) int {\n    return args.A + args.B + args.C + args.D\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u5728\u4e0b\u9762\u8fd9\u4e2a\u57fa\u51c6\u6d4b\u8bd5\u4e2d\uff0c\u6211\u4eec\u6bd4\u8f83\u4e86\u5185\u8054\u51fd\u6570\u548c\u975e\u5185\u8054\u51fd\u6570\u7684\u6027\u80fd\uff1a<\/p>\n<pre>package main\n\nimport \"testing\"\n\nfunc BenchmarkInlineFunc(b *testing.B) {\n    for i := 0; i &lt; b.N; i++ {\n        inlineFunc(i)\n    }\n}\n\nfunc BenchmarkFuncCall(b *testing.B) {\n    for i := 0; i &lt; b.N; i++ {\n        funcCall(i)\n    }\n}\n\nfunc inlineFunc(n int) {}\nfunc funcCall(n int) {}\n\nfunc main() {\n    testing.Main(m.Run, m.Benchmarker, m.Name)\n}<\/pre>\n<p>\u7ed3\u679c\uff1a<\/p>\n<pre>BenchmarkInlineFunc-12        1000000000             0.613 ns\/op\nBenchmarkFuncCall-12         50000000             26.7 ns\/op<\/pre>\n<p>\u6b63\u5982\u6211\u4eec\u6240\u89c1\u7684\uff0c\u5185\u8054\u51fd\u6570\u7684\u5f00\u9500\u663e\u8457\u4f4e\u4e8e\u51fd\u6570\u8c03\u7528\u3002<\/p>\n<p>\u901a\u8fc7\u5e94\u7528\u8fd9\u4e9b\u6280\u672f\uff0c\u4f60\u53ef\u4ee5\u5728 GOLANG \u5e94\u7528\u7a0b\u5e8f\u4e2d\u51cf\u5c11\u51fd\u6570\u8c03\u7528\u5f00\u9500\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u51cf\u5c11 GOLANG \u51fd\u6570\u8c03\u7528\u5f00\u9500\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8eGOLANG\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5982\u4f55\u5c06\u540c\u4e00\u5f20\u8868\u5185\u7684COUNT GROUP BY\u548cSELECT\u8bed\u53e5\u5408\u5e76\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Java\u51fd\u6570\u5728\u53ef\u9760\u6027\u65b9\u9762\u7684\u4f18\u52bf\u662f\u4ec0\u4e48\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f&#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-205822","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205822","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=205822"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205822\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}