{"id":10021440,"date":"2026-09-05T13:51:58","date_gmt":"2026-09-05T05:51:58","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021440"},"modified":"2026-09-05T13:51:58","modified_gmt":"2026-09-05T05:51:58","slug":"golang-%e5%86%85%e5%ad%98%e9%80%83%e9%80%b8","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021440","title":{"rendered":"Golang &#8211; \u5185\u5b58\u9003\u9038"},"content":{"rendered":"<blockquote><p>\u5728\u4e0a\u4e00\u7bc7 <a href=\"https:\/\/segmentfault.com\/a\/1190000047177192\" target=\"_blank\" rel=\"noopener noreferrer\">Golang\u5806\u4e0e\u6808\u5185\u5b58<\/a> \u4e2d\uff0c\u5e76\u6ca1\u6709\u5bf9\u5185\u5b58\u9003\u9038\u5c55\u5f00\u8ba8\u8bba\uff0c\u8fd9\u91cc\u6df1\u5165\u63a2\u8ba8\u4e00\u4e0b\u5185\u5b58\u9003\u9038\u8bdd\u9898\u3002<\/p><\/blockquote>\n<p>\u5185\u5b58\u9003\u9038\uff0c\u8fd9\u4e2a\u8bcd\u6c47\u542c\u8d77\u6765\u597d\u50cf\u662f\u4e00\u4e2a\u5371\u9669\u3001\u9700\u8981\u9632\u8303\u7684\u4e1c\u897f\u3002\u5b83\u7684\u786e\u5728\u6027\u80fd\u65b9\u9762\u9700\u8981\u6ce8\u610f\uff0c\u4f46\u5374\u4e0d\u662f\u5371\u9669\u3002\u76f8\u53cd\uff0c\u5b83\u7684\u5b58\u5728\uff0c\u5f88\u5927\u7a0b\u5ea6\u4e0a\u6b63\u662f\u4e3a\u4e86\u5185\u5b58\u5b89\u5168\uff0c\u5b83\u9632\u8303\u7684\u662f\u5728 <code>C\/C++<\/code> \u4e2d\u5e38\u8bf4\u7684\u60ac\u5782\u6307\u9488\u3002<br \/>\u5b83\u662f\u7f16\u8bd1\u9636\u6bb5\u5185\u5b58\u5206\u914d\u3001\u7ba1\u7406\u7684\u4e00\u4e2a\u91cd\u8981\u673a\u5236\uff0c\u4e3b\u8981\u4f5c\u7528\u6709\u4e24\u4e2a\uff0c\u9632\u6b62\u6307\u9488\u60ac\u5782\u3001\u9632\u6b62\u6808\u5185\u5b58\u8fc7\u5927\u3002<\/p>\n<h3>\u4ec0\u4e48\u662f\u5185\u5b58\u9003\u9038<\/h3>\n<p>\u5185\u5b58\u9003\u9038\u6307\u7684\u662f\uff1a\u4e00\u4e2a\u53d8\u91cf\u7684\u5f15\u7528\u8d85\u51fa\u4e86\u5f53\u524d\u51fd\u6570\u6808\u5e27\u53ef\u5b89\u5168\u7ba1\u7406\u7684\u8303\u56f4\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u4ec5\u5c06\u5b83\u653e\u5728\u6808\u4e0a\uff0c\u800c\u9700\u8981\u5c06\u5176\u5206\u914d\u5230\u5806\u4e0a\u3002<\/p>\n<pre><code class=\"go\"> func NewUser() *User {\n     u := User{ID: 1}\n     return &amp;u\n }<\/code><\/pre>\n<p>\u6808\u4e0a\u7684\u6570\u636e\u901a\u5e38\u968f\u51fd\u6570\u8c03\u7528\u8fdb\u5165\u3001\u968f\u51fd\u6570\u8fd4\u56de\u91ca\u653e\uff1b\u5806\u4e0a\u7684\u5bf9\u8c61\u5219\u7531\u5783\u573e\u56de\u6536\u5668\uff08GC\uff09\u5728\u4e0d\u518d\u88ab\u5f15\u7528\u65f6\u56de\u6536\u3002\u4e0a\u9762\u7684 u \u88ab\u8fd4\u56de\u7ed9\u8c03\u7528\u65b9\uff0c\u751f\u547d\u5468\u671f\u8d85\u8fc7\u4e86 NewUser\uff0c\u56e0\u6b64\u5fc5\u987b\u9003\u5230\u5806\u4e0a\u3002<\/p>\n<p>\u9003\u9038\u662f\u7f16\u8bd1\u5668\u7684\u5185\u5b58\u5b89\u5168\u51b3\u7b56\uff0c\u4e0d\u662f\u9519\u8bef\u3002\u8fd4\u56de\u5c40\u90e8\u53d8\u91cf\u6307\u9488\u5728 Go \u4e2d\u5b8c\u5168\u5408\u6cd5\uff0c\u7f16\u8bd1\u5668\u4f1a\u4fdd\u8bc1\u5b83\u5b58\u6d3b\u8db3\u591f\u4e45\u3002<\/p>\n<p>\u4e0d\u540c\u8bed\u8a00\u5bf9\u7c7b\u4f3c\u95ee\u9898\u7684\u5904\u7406\u65b9\u5f0f\u6709\u6240\u4e0d\u540c\uff0cGo\u8bed\u8a00\u5185\u5b58\u9003\u9038\u5206\u6790\u7684\u4fbf\u5229\u5728\u4e8e\u5b89\u5168\u548c\u81ea\u52a8\u5316\u3002\u4f46\u662f\u5f00\u53d1\u8005\u9700\u8981\u5728\u6027\u80fd\u654f\u611f\u573a\u666f\u7406\u89e3\u5206\u914d\u53d1\u751f\u5728\u54ea\u91cc\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u9003\u9038\u4e0d\u7b49\u4e8e\u5185\u5b58\u6cc4\u6f0f\uff0c\u5185\u5b58\u9003\u9038\u8868\u793a\u201c\u5bf9\u8c61\u4ece\u6808\u8f6c\u79fb\u5230\u4e86\u5806\u201d\uff0c\u5e76\u4e0d\u8868\u793a\u201c\u5bf9\u8c61\u6c38\u8fdc\u4e0d\u4f1a\u88ab\u91ca\u653e\u201d\u3002\u8c03\u7528\u65b9\u4e0d\u518d\u6301\u6709\u8fd4\u56de\u503c\u540e\uff0c\u8fd9\u4e2a User \u4f9d\u7136\u53ef\u4ee5\u88ab GC \u56de\u6536\u3002\u5b83\u53ea\u662f\u6bd4\u6808\u5bf9\u8c61\u591a\u7ecf\u5386\u4e86\u4e00\u6b21\u5806\u5206\u914d\u548c\u5783\u573e\u56de\u6536\u6d41\u7a0b\u3002<\/p>\n<p>\u771f\u6b63\u7684\u5185\u5b58\u6cc4\u6f0f\u662f\uff1a\u5bf9\u8c61\u5df2\u7ecf\u6ca1\u6709\u4e1a\u52a1\u4ef7\u503c\uff0c\u5374\u4ecd\u7136\u88ab\u67d0\u6761\u5f15\u7528\u94fe\u6301\u6709\uff0c\u4f8b\u5982\u88ab\u5168\u5c40\u7f13\u5b58\u3001\u672a\u6e05\u7406\u7684map\u3001\u957f\u671f\u8fd0\u884c\u7684 goroutine \u6216\u95ed\u5305\u610f\u5916\u5f15\u7528\u3002\u9003\u9038\u53ef\u80fd\u589e\u52a0\u5806\u4f7f\u7528\u91cf\uff0c\u4f46\u4e0d\u662f\u5185\u5b58\u6cc4\u6f0f\u7684\u540c\u4e49\u8bcd\u3002<\/p>\n<h3>\u7f16\u8bd1\u5668\u5982\u4f55\u5224\u65ad\u9003\u9038<\/h3>\n<p>Go \u7684\u9003\u9038\u5206\u6790\u53d1\u751f\u5728\u7f16\u8bd1\u671f\u3002\u5b83\u5e76\u4e0d\u4f9d\u8d56\u7a0b\u5e8f\u8fd0\u884c\u65f6\u7684\u5b9e\u9645\u6570\u636e\uff0c\u800c\u662f\u6839\u636e\u4ee3\u7801\u4e2d\u7684\u5f15\u7528\u5173\u7cfb\uff0c\u63a8\u5bfc\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u53ef\u80fd\u5728\u5f53\u524d\u51fd\u6570\u8fd4\u56de\u540e\u4ecd\u88ab\u8bbf\u95ee\u3002<\/p>\n<p>\u53ef\u4ee5\u628a\u5b83\u7406\u89e3\u4e3a\u7f16\u8bd1\u5668\u5728\u56de\u7b54\u4e00\u4e2a\u95ee\u9898\uff1a<\/p>\n<blockquote><p>\u8fd9\u4e2a\u53d8\u91cf\u80fd\u5426\u786e\u8ba4\u53ea\u5728\u5f53\u524d\u51fd\u6570\u8c03\u7528\u671f\u95f4\u4f7f\u7528\uff1f<\/p><\/blockquote>\n<p>\u5982\u679c\u7b54\u6848\u662f\u201c\u80fd\u201d\uff0c\u53d8\u91cf\u901a\u5e38\u53ef\u4ee5\u653e\u5728\u6808\u4e0a\uff1b\u5982\u679c\u7b54\u6848\u662f\u201c\u4e0d\u80fd\u786e\u5b9a\u201d\u6216\u201c\u4f1a\u5728\u51fd\u6570\u8fd4\u56de\u540e\u7ee7\u7eed\u88ab\u4f7f\u7528\u201d\uff0c\u7f16\u8bd1\u5668\u5c31\u4f1a\u8ba9\u5b83\u9003\u9038\u5230\u5806\u4e0a\u3002<\/p>\n<h4>1. \u5206\u6790\u53d8\u91cf\u7684\u5f15\u7528\u6d41\u5411<\/h4>\n<p>\u7f16\u8bd1\u5668\u4f1a\u8ffd\u8e2a\u4e00\u4e2a\u53d8\u91cf\u7684\u5730\u5740\u88ab\u4f20\u5230\u4e86\u54ea\u91cc\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"go\">func NewUser() *User {\n    u := User{ID: 1}\n    return &amp;u\n}<\/code><\/pre>\n<p>&#038;u \u88ab\u4f5c\u4e3a\u8fd4\u56de\u503c\u4ea4\u7ed9\u8c03\u7528\u65b9\u3002NewUser \u8fd4\u56de\u540e\uff0c\u8c03\u7528\u65b9\u4ecd\u53ef\u80fd\u901a\u8fc7\u8fd9\u4e2a\u6307\u9488\u8bbf\u95ee u\uff0c\u56e0\u6b64 u \u4e0d\u80fd\u53ea\u5b58\u5728\u4e8e\u5f53\u524d\u51fd\u6570\u6808\u5e27\u4e2d\uff0c\u5fc5\u987b\u5206\u914d\u5230\u5806\u4e0a\u3002<\/p>\n<p>\u4e0e\u4e4b\u76f8\u5bf9\uff1a<\/p>\n<pre><code class=\"go\">func PrintID() {\n    u := User{ID: 1}\n    fmt.Println(u.ID)\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u6ca1\u6709\u5c06 u \u7684\u5730\u5740\u4f20\u51fa\u51fd\u6570\uff0cu \u7684\u751f\u547d\u5468\u671f\u4e0d\u4f1a\u8d85\u8fc7 PrintID\uff0c\u901a\u5e38\u53ef\u4ee5\u76f4\u63a5\u653e\u5728\u6808\u4e0a\u3002<\/p>\n<h4>2. \u5224\u65ad\u5f15\u7528\u662f\u5426\u8de8\u8d8a\u51fd\u6570\u751f\u547d\u5468\u671f<\/h4>\n<p>\u9003\u9038\u7684\u5173\u952e\u4e0d\u662f\u53d8\u91cf\u662f\u5426\u4e3a\u5c40\u90e8\u53d8\u91cf\uff0c\u800c\u662f\u5b83\u7684\u5f15\u7528\u662f\u5426\u8de8\u8d8a\u4e86\u51fd\u6570\u8fb9\u754c\u3002<\/p>\n<pre><code class=\"go\">var current *User\n\nfunc SetCurrent() {\n    u := User{ID: 1}\n    current = &amp;u\n}<\/code><\/pre>\n<p>u \u7684\u5730\u5740\u88ab\u4fdd\u5b58\u5230\u4e86\u5168\u5c40\u53d8\u91cf current\u3002\u5373\u4f7f SetCurrent \u5df2\u7ecf\u8fd4\u56de\uff0c\u5176\u4ed6\u4ee3\u7801\u4ecd\u53ef\u8bbf\u95ee current\u6307\u5411\u7684\u5bf9\u8c61\uff0c\u56e0\u6b64 u \u5fc5\u987b\u9003\u9038\u3002<\/p>\n<p>\u95ed\u5305\u4e5f\u662f\u540c\u6837\u7684\u9053\u7406\uff1a<\/p>\n<pre><code class=\"go\">func Counter() func() int {\n    n := 0\n\n    return func() int {\n        n++\n        return n\n    }\n}<\/code><\/pre>\n<p>\u8fd4\u56de\u7684\u533f\u540d\u51fd\u6570\u6301\u6709\u5bf9 n \u7684\u5f15\u7528\u3002Counter \u7ed3\u675f\u540e\uff0c\u8fd9\u4e2a\u95ed\u5305\u4ecd\u53ef\u80fd\u88ab\u8c03\u7528\uff0c\u6240\u4ee5 n \u4e0d\u80fd\u968f\u7740Counter \u7684\u6808\u5e27\u9500\u6bc1\u3002<\/p>\n<h4>3. \u8ffd\u8e2a\u53c2\u6570\u662f\u5426\u4f1a\u201c\u6cc4\u6f0f\u201d<\/h4>\n<p>\u5bf9\u4e8e\u51fd\u6570\u53c2\u6570\uff0c\u7f16\u8bd1\u5668\u8fd8\u4f1a\u5206\u6790\u53c2\u6570\u662f\u5426\u88ab\u4fdd\u5b58\u3001\u8fd4\u56de\uff0c\u6216\u4f20\u9012\u7ed9\u5176\u4ed6\u53ef\u80fd\u957f\u671f\u6301\u6709\u5b83\u7684\u5bf9\u8c61\u3002<\/p>\n<pre><code class=\"go\">func Keep(u *User) *User {\n    return u\n}<\/code><\/pre>\n<p>\u8fd9\u91cc u \u88ab\u76f4\u63a5\u8fd4\u56de\u3002\u7f16\u8bd1\u5668\u4f1a\u8bb0\u5f55\uff1a\u4f20\u7ed9 Keep \u7684\u53c2\u6570\u53ef\u80fd\u6d41\u5411\u8fd4\u56de\u503c\u3002\u81f3\u4e8e\u8c03\u7528\u5904\u7684\u5b9e\u9645\u5bf9\u8c61\u662f\u5426\u9003\u9038\uff0c\u8fd8\u8981\u7ed3\u5408\u4e0a\u4e0b\u6587\u5224\u65ad\uff1a<\/p>\n<pre><code class=\"go\">func Example() {\n    u := User{ID: 1}\n    _ = Keep(&amp;u)\n}<\/code><\/pre>\n<p>\u5982\u679c\u8fd4\u56de\u503c\u6ca1\u6709\u7ee7\u7eed\u88ab\u4f7f\u7528\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u8bc1\u660e u \u4ecd\u4e0d\u9700\u8981\u9003\u9038\uff1b\u5982\u679c\u8fd4\u56de\u503c\u88ab\u4fdd\u5b58\u5230\u5168\u5c40\u53d8\u91cf\u6216\u4ece\u66f4\u5916\u5c42\u51fd\u6570\u8fd4\u56de\uff0cu \u5c31\u53ef\u80fd\u9003\u9038\u3002<\/p>\n<p>\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u9003\u9038\u5206\u6790\u4e0d\u662f\u7b80\u5355\u5730\u6309\u5355\u4e2a\u51fd\u6570\u673a\u68b0\u5224\u65ad\uff0c\u800c\u4f1a\u7ed3\u5408\u8c03\u7528\u5173\u7cfb\u4e0e\u5185\u8054\u7ed3\u679c\u8fdb\u884c\u63a8\u5bfc\u3002<\/p>\n<h4>4. \u9762\u5bf9\u4e0d\u786e\u5b9a\u8c03\u7528\u65f6\u91c7\u53d6\u4fdd\u5b88\u7b56\u7565<\/h4>\n<p>Go \u7f16\u8bd1\u5668\u9075\u5faa\u4e00\u4e2a\u5b9e\u7528\u539f\u5219\uff1a\u65e0\u6cd5\u8bc1\u660e\u4e0d\u9003\u9038\uff0c\u5c31\u6309\u53ef\u80fd\u9003\u9038\u5904\u7406\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5c06\u5bf9\u8c61\u4f20\u7ed9\u63a5\u53e3\u3001\u53cd\u5c04\u6216\u590d\u6742\u7684\u5916\u90e8\u8c03\u7528\u65f6\uff0c\u7f16\u8bd1\u5668\u6709\u65f6\u65e0\u6cd5\u51c6\u786e\u5224\u65ad\u5bf9\u65b9\u662f\u5426\u4fdd\u5b58\u4e86\u5bf9\u8c61\u5f15\u7528\uff1a<\/p>\n<pre><code class=\"go\">func Log(v any) {\n    fmt.Println(v)\n}\n\nfunc Example() {\n    u := User{ID: 1}\n    Log(&amp;u)\n}<\/code><\/pre>\n<p>u \u662f\u5426\u9003\u9038\u53d6\u51b3\u4e8e Log \u80fd\u5426\u88ab\u5185\u8054\u3001fmt.Println \u7684\u8c03\u7528\u94fe\uff0c\u4ee5\u53ca\u5f53\u524d\u7f16\u8bd1\u5668\u7248\u672c\u7684\u5206\u6790\u80fd\u529b\u3002\u8fd9\u91cc\u4e0d\u80fd\u7b80\u5355\u603b\u7ed3\u4e3a\u201c\u4f20\u7ed9\u63a5\u53e3\u4e00\u5b9a\u9003\u9038\u201d\uff0c\u4f46\u4e0d\u900f\u660e\u7684\u8c03\u7528\u8fb9\u754c\u786e\u5b9e\u4f1a\u63d0\u9ad8\u9003\u9038\u6982\u7387\u3002<\/p>\n<h4>5. \u5bf9\u8c61\u592a\u5927\u4e5f\u53ef\u80fd\u88ab\u653e\u5230\u5806\u4e0a<\/h4>\n<p>\u9003\u9038\u5206\u6790\u4e0d\u4ec5\u8003\u8651\u751f\u547d\u5468\u671f\uff0c\u4e5f\u8003\u8651\u6808\u7a7a\u95f4\u3002<\/p>\n<pre><code class=\"go\">func Process() {\n    var buf [10 * 1024 * 1024]byte\n    _ = buf\n}<\/code><\/pre>\n<p>\u5373\u4f7f buf \u6ca1\u6709\u88ab\u8fd4\u56de\u3001\u4e5f\u6ca1\u6709\u88ab\u5916\u90e8\u5f15\u7528\uff0c\u8fc7\u5927\u7684\u5c40\u90e8\u53d8\u91cf\u4e5f\u53ef\u80fd\u65e0\u6cd5\u5b89\u5168\u6216\u9ad8\u6548\u5730\u653e\u5165goroutine \u6808\u4e2d\uff0c\u6700\u7ec8\u88ab\u5206\u914d\u5230\u5806\u4e0a\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u770b\u5230\u5806\u5206\u914d\u4e0d\u4e00\u5b9a\u610f\u5473\u7740\u201c\u6307\u9488\u9003\u51fa\u4e86\u51fd\u6570\u201d\u3002\u4e5f\u53ef\u80fd\u53ea\u662f\u5bf9\u8c61\u5c3a\u5bf8\u4e0d\u9002\u5408\u6808\u5206\u914d\u3002<\/p>\n<p>\u6700\u7ec8\u53ef\u4ee5\u628a\u9003\u9038\u5206\u6790\u6982\u62ec\u4e3a\u4e00\u53e5\u8bdd\uff1a\u7f16\u8bd1\u5668\u6cbf\u7740\u5f15\u7528\u5173\u7cfb\u8ffd\u8e2a\u5bf9\u8c61\u7684\u53bb\u5411\uff0c\u53ea\u8981\u5bf9\u8c61\u53ef\u80fd\u5728\u5f53\u524d\u6808\u5e27\u7ed3\u675f\u540e\u4ecd\u7136\u5b58\u6d3b\uff0c\u6216\u8005\u4e0d\u9002\u5408\u653e\u5728\u6808\u4e0a\uff0c\u5c31\u4f1a\u9009\u62e9\u5806\u5206\u914d\u3002<\/p>\n<h3>\u5e38\u89c1\u9003\u9038\u573a\u666f<\/h3>\n<p>\u6700\u5178\u578b\u7684\u662f\u8fd4\u56de\u5c40\u90e8\u53d8\u91cf\u6307\u9488\uff1a<\/p>\n<pre><code class=\"go\">func NewUser() *User {\n    u := User{ID: 1}\n    return &amp;u\n}<\/code><\/pre>\n<p>\u95ed\u5305\u6355\u83b7\u5916\u90e8\u53d8\u91cf\u4e5f\u5f88\u5e38\u89c1\uff1a<\/p>\n<pre><code>func Counter() func() int {\n    n := 0\n    return func() int {\n        n++\n        return n\n    }\n}<\/code><\/pre>\n<h3>\u9003\u9038\u4e3a\u4ec0\u4e48\u503c\u5f97\u5173\u6ce8<\/h3>\n<p>\u9003\u9038\u672c\u8eab\u4e0d\u662f\u95ee\u9898\u3002Go \u7684\u5806\u5206\u914d\u5668\u548c GC \u90fd\u7ecf\u8fc7\u4e86\u5927\u91cf\u4f18\u5316\uff0c\u666e\u901a\u4e1a\u52a1\u4ee3\u7801\u4e2d\u5076\u5c14\u521b\u5efa\u51e0\u4e2a\u5806\u5bf9\u8c61\uff0c\u901a\u5e38\u4e0d\u4f1a\u6210\u4e3a\u6027\u80fd\u74f6\u9888\u3002<\/p>\n<p>\u771f\u6b63\u503c\u5f97\u5173\u6ce8\u7684\u662f\uff1a\u67d0\u6bb5<strong>\u9ad8\u9891\u4ee3\u7801<\/strong>\u56e0\u4e3a\u9003\u9038\uff0c<strong>\u6301\u7eed\u4ea7\u751f\u5927\u91cf<\/strong>\u77ed\u547d\u5bf9\u8c61\u3002\u6bd4\u5982\u4e00\u4e2a\u6bcf\u79d2\u6267\u884c\u6570\u5341\u4e07\u6b21\u7684\u8bf7\u6c42\u5904\u7406\u51fd\u6570\uff0c\u54ea\u6015\u6bcf\u6b21\u53ea\u591a\u5206\u914d\u4e00\u4e2a\u51e0\u5341\u5b57\u8282\u7684\u5bf9\u8c61\uff0c\u7d2f\u8ba1\u4e0b\u6765\u4e5f\u53ef\u80fd\u663e\u8457\u589e\u52a0 GC \u538b\u529b\u3002<\/p>\n<h4>\u6808\u5206\u914d\u4e0e\u5806\u5206\u914d\u7684\u6210\u672c\u5dee\u5f02<\/h4>\n<p>\u6808\u5206\u914d\u975e\u5e38\u8f7b\u91cf\u3002\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u53ea\u9700\u8981\u79fb\u52a8\u6808\u6307\u9488\uff0c\u5c31\u80fd\u4e3a\u5c40\u90e8\u53d8\u91cf\u9884\u7559\u7a7a\u95f4\uff1b\u51fd\u6570\u8fd4\u56de\u540e\uff0c\u8fd9\u90e8\u5206\u7a7a\u95f4\u4f1a\u81ea\u7136\u5931\u6548\uff0c\u4e0d\u9700\u8981\u989d\u5916\u56de\u6536\u3002<\/p>\n<p>\u5806\u5206\u914d\u7684\u8def\u5f84\u5219\u66f4\u957f\u4e00\u4e9b\u3002\u8fd0\u884c\u65f6\u9700\u8981\u4ece\u5408\u9002\u7684\u5185\u5b58\u533a\u57df\u5206\u914d\u5bf9\u8c61\uff0c\u5e76\u5728\u672a\u6765\u7531 GC \u5224\u65ad\u8fd9\u4e2a\u5bf9\u8c61\u662f\u5426\u8fd8\u88ab\u5f15\u7528\u3001\u662f\u5426\u53ef\u4ee5\u56de\u6536\u3002\u5355\u6b21\u6210\u672c\u672a\u5fc5\u660e\u663e\uff0c\u4f46\u9ad8\u9891\u7d2f\u79ef\u540e\u4f1a\u5f71\u54cd\u7a0b\u5e8f\u8868\u73b0\u3002<\/p>\n<p>\u53ef\u4ee5\u7b80\u5355\u7406\u89e3\u4e3a\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u5206\u914d\u4f4d\u7f6e<\/th>\n<th>\u5206\u914d\u65b9\u5f0f<\/th>\n<th>\u91ca\u653e\u65b9\u5f0f<\/th>\n<th>\u4e3b\u8981\u6210\u672c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u6808<\/td>\n<td>\u8c03\u6574\u6808\u6307\u9488<\/td>\n<td>\u51fd\u6570\u8fd4\u56de\u65f6\u81ea\u52a8\u91ca\u653e<\/td>\n<td>\u6781\u4f4e<\/td>\n<\/tr>\n<tr>\n<td>\u5806<\/td>\n<td>\u8fd0\u884c\u65f6\u5206\u914d\u5668\u5206\u914d<\/td>\n<td>GC \u5728\u540e\u7eed\u9636\u6bb5\u56de\u6536<\/td>\n<td>\u5206\u914d\u3001\u626b\u63cf\u4e0e\u56de\u6536\u6210\u672c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5e76\u4e0d\u662f\u201c\u6808\u6c38\u8fdc\u5feb\u3001\u5806\u6c38\u8fdc\u6162\u201d\u3002Go \u7684\u5806\u5206\u914d\u5668\u6709\u672c\u5730\u7f13\u5b58\uff0c\u8bb8\u591a\u5c0f\u5bf9\u8c61\u5206\u914d\u5f88\u5feb\u3002\u4f46\u6808\u5206\u914d\u901a\u5e38\u4ecd\u7136\u66f4\u4fbf\u5b9c\uff0c\u4e5f\u4e0d\u4f1a\u76f4\u63a5\u589e\u52a0 GC \u5de5\u4f5c\u91cf\uff0c\u8fdb\u800c\u5bfc\u81f4\u6027\u80fd\u6296\u52a8\u3002<\/p>\n<h3>\u5982\u4f55\u4f18\u5316<\/h3>\n<p>\u4f18\u5316\u5185\u5b58\u9003\u9038\u7684\u76ee\u6807\uff0c\u4e0d\u662f\u8ba9\u6240\u6709\u53d8\u91cf\u90fd\u7559\u5728\u6808\u4e0a\uff0c\u800c\u662f\u51cf\u5c11\u70ed\u70b9\u8def\u5f84\u4e2d\u6ca1\u6709\u4ef7\u503c\u7684\u5806\u5206\u914d\u3002\u4e00\u4e2a\u5b9e\u7528\u539f\u5219\u662f\uff1a\u5148\u786e\u8ba4\u5206\u914d\uff0c\u518d\u5b9a\u4f4d\u6765\u6e90\uff0c\u6700\u540e\u8c03\u6574\u4ee3\u7801\u3002<\/p>\n<h4>1.\u786e\u8ba4\u9003\u9038\u662f\u5426\u503c\u5f97\u4f18\u5316<\/h4>\n<p>\u5148\u67e5\u770b\u7f16\u8bd1\u5668\u7684\u5224\u65ad\uff1a<\/p>\n<blockquote><p>go build -gcflags=&#8221;-m=2&#8243; .<\/p><\/blockquote>\n<p>\u7136\u540e\u901a\u8fc7 benchmark \u89c2\u5bdf\u771f\u5b9e\u5206\u914d\uff1a<\/p>\n<blockquote><p>go test -bench=. -benchmem<\/p><\/blockquote>\n<p>\u7ed3\u679c\u4e2d\u7684\u4e24\u4e2a\u6307\u6807\u5c24\u5176\u91cd\u8981\uff1a<\/p>\n<blockquote><p>120 ns\/op    48 B\/op    2 allocs\/op<\/p><\/blockquote>\n<ul>\n<li>B\/op\uff1a\u6bcf\u6b21\u64cd\u4f5c\u5e73\u5747\u5206\u914d\u7684\u5b57\u8282\u6570\u3002<\/li>\n<li>allocs\/op\uff1a\u6bcf\u6b21\u64cd\u4f5c\u5e73\u5747\u53d1\u751f\u7684\u5206\u914d\u6b21\u6570\u3002<\/li>\n<\/ul>\n<p>\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u9891\u7387\u5f88\u4f4e\uff0c\u5373\u4f7f\u6709\u51e0\u6b21\u5206\u914d\u4e5f\u65e0\u9700\u5728\u610f\uff1b\u5982\u679c\u5b83\u5904\u4e8e\u8bf7\u6c42\u3001\u5e8f\u5217\u5316\u3001\u65e5\u5fd7\u3001\u5faa\u73af\u8ba1\u7b97\u7b49\u70ed\u70b9\u8def\u5f84\uff0c\u5e76\u4e14 allocs\/op \u5f88\u9ad8\uff0c\u5c31\u503c\u5f97\u7ee7\u7eed\u5206\u6790\u3002\u9700\u8981\u6ce8\u610f\uff0c-m=2 \u544a\u8bc9\u4f60\u201c\u4e3a\u4ec0\u4e48\u53ef\u80fd\u9003\u9038\u201d\uff0callocs\/op \u624d\u544a\u8bc9\u4f60\u201c\u5b9e\u9645\u5206\u914d\u662f\u5426\u6784\u6210\u6210\u672c\u201d\u3002<\/p>\n<h4>2. \u907f\u514d\u4e3a\u5c0f\u5bf9\u8c61\u4e60\u60ef\u6027\u8fd4\u56de\u6307\u9488<\/h4>\n<p>\u5bf9\u4e8e\u5f88\u5c0f\u7684\u7ed3\u6784\u4f53\uff0c\u76f4\u63a5\u8fd4\u56de\u503c\u6709\u65f6\u6bd4\u8fd4\u56de\u6307\u9488\u66f4\u5408\u9002\u3002<\/p>\n<p>\u8fd4\u56de\u503c\u4e0d\u4ee3\u8868\u4e00\u5b9a\u53d1\u751f\u590d\u5236\u3002Go \u7f16\u8bd1\u5668\u4f1a\u505a\u5bc4\u5b58\u5668\u4f20\u9012\u3001\u5185\u8054\u7b49\u4f18\u5316\uff1b\u5bf9\u4e8e\u5c0f\u5bf9\u8c61\uff0c\u590d\u5236\u6210\u672c\u901a\u5e38\u5f88\u4f4e\u3002<\/p>\n<p>\u4f46\u4e0d\u8981\u628a\u201c\u8fd4\u56de\u503c\u201d\u5f53\u6210\u56fa\u5b9a\u89c4\u5219\u3002\u5bf9\u4e8e\u8f83\u5927\u7684\u7ed3\u6784\u4f53\u3001\u9700\u8981\u8868\u8fbe\u53ef\u7a7a\u8bed\u4e49\uff0c\u6216\u8005\u8c03\u7528\u65b9\u786e\u5b9e\u9700\u8981\u5171\u4eab\u540c\u4e00\u4e2a\u5bf9\u8c61\u65f6\uff0c\u8fd4\u56de\u6307\u9488\u4ecd\u7136\u66f4\u81ea\u7136\u3002<\/p>\n<h4>3. \u8ba9\u8c03\u7528\u65b9\u63d0\u4f9b\u53ef\u590d\u7528\u7684\u5185\u5b58<\/h4>\n<p>\u9891\u7e41\u521b\u5efa\u4e34\u65f6\u7ed3\u679c\u65f6\uff0c\u53ef\u4ee5\u8ba9\u8c03\u7528\u65b9\u4f20\u5165\u76ee\u6807\u5bf9\u8c61\u6216\u7f13\u51b2\u533a\uff1a<\/p>\n<pre><code class=\"go\">func ParseUser(data []byte) *User {\n    return &amp;User{\n        ID: 1,\n    }\n}<\/code><\/pre>\n<p>\u6539\u4e3a\uff1a<\/p>\n<pre><code class=\"go\"> func ParseUser(data []byte, dst *User) {\n     *dst = User{\n         ID: 1,\n     }\n }<\/code><\/pre>\n<p>\u8c03\u7528\u65b9\u53ef\u4ee5\u5728\u5faa\u73af\u5916\u590d\u7528\u5bf9\u8c61\u3002\u4e0d\u8fc7\uff0c\u8fd9\u79cd\u63a5\u53e3\u4f1a\u628a\u5185\u5b58\u590d\u7528\u8d23\u4efb\u4ea4\u7ed9\u8c03\u7528\u65b9\uff0c\u540c\u65f6\u4e5f\u8981\u517c\u987e\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n<h4>4. \u4e3a slice \u548c map \u9884\u5206\u914d\u5bb9\u91cf<\/h4>\n<p>\u52a8\u6001\u6269\u5bb9\u4f1a\u521b\u5efa\u66f4\u5927\u7684\u5e95\u5c42\u6570\u7ec4\uff0c\u5e76\u590d\u5236\u65e7\u6570\u636e\u3002\u65e7\u6570\u7ec4\u662f\u5426\u80fd\u53ca\u65f6\u91ca\u653e\uff0c\u53d6\u51b3\u4e8e\u662f\u5426\u4ecd\u6709\u5207\u7247\u5f15\u7528\u5b83\u3002<br \/>  \u5982\u679c\u7ed3\u679c\u957f\u5ea6\u53ef\u9884\u77e5\uff0c\u53ef\u4ee5\u9884\u5206\u914d\u3002\u9884\u5206\u914d\u4e0d\u80fd\u4fdd\u8bc1\u201c\u96f6\u9003\u9038\u201d\uff0c\u4f46\u80fd\u51cf\u5c11\u6269\u5bb9\u5bfc\u81f4\u7684\u989d\u5916\u5806\u5206\u914d\u4e0e\u590d\u5236\u3002<\/p>\n<h4>5. \u907f\u514d\u70ed\u70b9\u8def\u5f84\u4e2d\u7684\u901a\u7528\u683c\u5f0f\u5316<\/h4>\n<p>fmt \u7cfb\u5217 API \u5f88\u65b9\u4fbf\uff0c\u4f46\u5b83\u9700\u8981\u5904\u7406\u63a5\u53e3\u3001\u683c\u5f0f\u89e3\u6790\u548c\u591a\u79cd\u52a8\u6001\u7c7b\u578b\uff0c\u5728\u70ed\u70b9\u8def\u5f84\u4e2d\u5f80\u5f80\u6bd4\u4e13\u7528API \u5206\u914d\u66f4\u591a\u3002<\/p>\n<pre><code class=\"go\">func BuildKey(id int64) string {\n    return fmt.Sprintf(&quot;user:%d&quot;, id)\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u6839\u636e\u573a\u666f\u6539\u4e3a\uff1a<\/p>\n<pre><code class=\"go\">func BuildKey(id int64) string {\n    return &quot;user:&quot; + strconv.FormatInt(id, 10)\n}<\/code><\/pre>\n<p>\u82e5\u7ed3\u679c\u5199\u5165\u5b57\u8282\u7f13\u51b2\u533a\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f7f\u7528 strconv.AppendInt\uff1a<\/p>\n<pre><code class=\"go\">func AppendKey(dst []byte, id int64) []byte {\n    dst = append(dst, &quot;user:&quot;...)\n    return strconv.AppendInt(dst, id, 10)\n}<\/code><\/pre>\n<p>\u8fd9\u7c7b\u6539\u52a8\u7684\u6536\u76ca\u901a\u5e38\u6765\u81ea\u51cf\u5c11\u683c\u5f0f\u5316\u8fc7\u7a0b\u4e2d\u7684\u4e2d\u95f4\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u67d0\u4e00\u4e2a\u7279\u5b9a\u53d8\u91cf\u662f\u5426\u9003\u9038\u3002<\/p>\n<h4>7. \u8c28\u614e\u4f7f\u7528\u95ed\u5305\u4e0e\u63a5\u53e3<\/h4>\n<p>\u8fd4\u56de\u95ed\u5305\u3001\u628a\u95ed\u5305\u4ea4\u7ed9\u5f02\u6b65\u4efb\u52a1\uff0c\u901a\u5e38\u4f1a\u5ef6\u957f\u88ab\u6355\u83b7\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\uff1a<\/p>\n<pre><code class=\"go\">func MakeHandler(user *User) func() {\n    return func() {\n        process(user)\n    }\n}<\/code><\/pre>\n<p>\u5982\u679c\u53ea\u9700\u4f20\u9012\u5c11\u91cf\u6570\u636e\uff0c\u53ef\u4ee5\u8003\u8651\u76f4\u63a5\u4f20\u503c\uff0c\u6216\u5c06\u903b\u8f91\u6539\u4e3a\u666e\u901a\u51fd\u6570\u8c03\u7528\uff1a<\/p>\n<pre><code class=\"go\"> func HandleUser(id int64) {\n     processByID(id)\n }<\/code><\/pre>\n<h4>8. \u4f7f\u7528 sync.Pool<\/h4>\n<p>sync.Pool \u80fd\u51cf\u5c11\u91cd\u590d\u5206\u914d,\u9002\u5408\u9ad8\u5e76\u53d1\u3001\u77ed\u751f\u547d\u5468\u671f\u3001\u53ef\u590d\u7528\u4e14\u5206\u914d\u6210\u672c\u660e\u786e\u7684\u4e34\u65f6\u5bf9\u8c61\uff0c\u4f8b\u5982\u7f16\u7801\u7f13\u51b2\u533a\u3001\u538b\u7f29\u7f13\u51b2\u533a\u6216\u5e8f\u5217\u5316\u5bf9\u8c61\u3002<\/p>\n<h3>\u603b\u7ed3<\/h3>\n<p>Go \u7684\u9003\u9038\u5206\u6790\uff0c\u672c\u8d28\u4e0a\u662f\u5728\u6808\u5206\u914d\u6548\u7387\u4e0e\u8de8\u4f5c\u7528\u57df\u5185\u5b58\u5b89\u5168\u4e4b\u95f4\u81ea\u52a8\u6743\u8861\u3002\u5b83\u8ba9\u6211\u4eec\u53ef\u4ee5\u81ea\u7136\u5730\u8fd4\u56de\u6307\u9488\u3001\u4f7f\u7528\u95ed\u5305\u548c\u63a5\u53e3\uff0c\u800c\u4e0d\u5fc5\u624b\u52a8\u91ca\u653e\u5185\u5b58\u3002<\/p>\n<p>\u9003\u9038\u5e76\u4e0d\u53ef\u6015\uff0c\u771f\u6b63\u9700\u8981\u8b66\u60d5\u7684\u662f\u9ad8\u9891\u8c03\u7528\u7684\u4ee3\u7801\u4e2d\u6301\u7eed\u4ea7\u751f\u7684\u5927\u91cf\u77ed\u547d\u5806\u5bf9\u8c61\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000048160065\">https:\/\/segmentfault.com\/a\/1190000048160065<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4e0a\u4e00\u7bc7 Golang\u5806\u4e0e\u6808\u5185\u5b58 \u4e2d\uff0c\u5e76\u6ca1\u6709\u5bf9\u5185\u5b58\u9003\u9038\u5c55\u5f00\u8ba8\u8bba\uff0c\u8fd9\u91cc\u6df1\u5165\u63a2\u8ba8\u4e00\u4e0b\u5185\u5b58\u9003\u9038\u8bdd\u9898\u3002 \u5185\u5b58\u9003\u9038\uff0c\u8fd9\u4e2a [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021440","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021440","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021440"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021440\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}