{"id":3626,"date":"2021-09-10T17:04:30","date_gmt":"2021-09-10T09:04:30","guid":{"rendered":"https:\/\/wyxxt.org.cn\/?p=3626"},"modified":"2023-12-04T15:01:47","modified_gmt":"2023-12-04T07:01:47","slug":"go%e6%9c%9d%e8%8a%b1%e5%a4%95%e6%8b%be","status":"publish","type":"post","link":"https:\/\/wyxxt.org.cn\/?p=3626","title":{"rendered":"go\u671d\u82b1\u5915\u62fe"},"content":{"rendered":"<h2>\u5f15\u7528\u7c7b\u578b<\/h2>\n<p>slice map channel interface func\u90fd\u662f\u5f15\u7528\u7c7b\u578b<\/p>\n<p>\u9664\u5f15\u7528\u7c7b\u578b\u5916\u7684\u5176\u4ed6\u7c7b\u578b\u90fd\u662f\u503c\u7c7b\u578b<\/p>\n<blockquote><p>\n  nil slice \u4e0e \u7a7a slice \u7684\u5904\u7406\u662f\u4e0d\u4e00\u81f4\u7684<\/p>\n<p>  var slice []int \u53ea\u662f\u58f0\u540d\u4e86slice\uff0c\u5374\u6ca1\u6709\u7ed9\u5b9e\u4f8b\u8bdd\u7684\u5bf9\u8c61<\/p>\n<p>  \u8fd9\u79cd\u60c5\u51b5\u53ef\u4ee5\u7528\u4e8e\u8fd4\u56de slice \u7684\u51fd\u6570\uff0c\u5f53\u51fd\u6570\u5f02\u5e38\u65f6\uff0c\u4fdd\u8bc1\u51fd\u6570\u4f9d\u7136\u4f1a\u6709 nil \u7684\u8fd4\u56de\u503c<\/p>\n<p>  empty slice \u662f\u6307 slice \u4e0d\u4e3a nil\uff0c\u4f46\u662f slice \u6ca1\u6709\u503c\uff0cslice \u7684\u5e95\u5c42\u7a7a\u95f4\u662f\u7a7a\u7684<\/p>\n<p>  Slice := make([]int, 0)<\/p>\n<p>  <code>nil slice<\/code> \u548c <code>empty slice<\/code>\u662f\u4e0d\u540c\u7684\u4e1c\u897f,\u9700\u8981\u6211\u4eec\u52a0\u4ee5\u533a\u5206\u7684\n<\/p><\/blockquote>\n<h2>Golang \u5185\u5b58\u6a21\u578b<\/h2>\n<blockquote><p>\n  \u5c0f\u5bf9\u8c61\u8fc7\u591a\u4f1a\u9020\u6210GC\u538b\u529b<\/p>\n<p>  \u56e0\u4e3a\u5c0f\u5bf9\u8c61\u8fc7\u591a\u4f1a\u5bfc\u81f4GC\u4e09\u8272\u6807\u8bb0\u6cd5\u6d88\u8017\u8fc7\u591a\u7684GPU\u3002<\/p>\n<p>  \u4f18\u5316\u601d\u8def\uff0c\u51cf\u5c11\u5bf9\u8c61\u5206\u914d\u3002\u5982\u4f7f\u7528\u7ed3\u6784\u4f53\u53d8\u91cf\uff0c\u51cf\u5c11\u503c\u4f20\u9012\n<\/p><\/blockquote>\n<h2>\u6570\u636e\u7ade\u4e89\u95ee\u9898<\/h2>\n<blockquote><p>\n  Data Race \u95ee\u9898\u53ef\u4ee5\u4f7f\u7528\u4e92\u65a5\u9501sync.Mutex \u6216\u8005\u4e5f\u53ef\u4ee5\u901a\u8fc7 CAS \u65e0\u9501\u5e76\u53d1\u89e3\u51b3<\/p>\n<p>  golang \u4e2d\u5f15\u5165\u4e86\u7ade\u4e89\u68c0\u6d4b\u673a\u5236\uff0c\u53ef\u4ee5\u4f7f\u7528 go run -race \u6216\u8005 go build -race \u6765\u8fdb\u884c\u9759\u6001\u68c0\u6d4b<\/p>\n<p>  \u7ade\u4e89\u68c0\u6d4b\u5668\u5df2\u7ecf\u5b8c\u5168\u96c6\u6210\u5230 Go \u5de5\u5177\u94fe\u4e2d\uff0c\u4ec5\u4ec5\u6dfb\u52a0 -race \u6807\u5fd7\u5c31\u884c\u4e86<\/p>\n<p>  \u8981\u89e3\u51b3\u6570\u636e\u7ade\u4e89\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528 sync.Mutex \u4e5f\u53ef\u4ee5\u4f7f\u7528 channel \uff0c\u4f7f\u7528 channel \u7684\u6548\u7387\u6bd4 sync.Mutex \u7684\u6548\u7387\u9ad8\n<\/p><\/blockquote>\n<h2>Go\u8bed\u8a00 atomic \u539f\u5b50\u64cd\u4f5c<\/h2>\n<blockquote><p>\n  atomic\u662f\u6700\u8f7b\u91cf\u7ea7\u7684\u9501\uff0ccpu\u539f\u8bed<\/p>\n<p>  \u539f\u5b50\u64cd\u4f5c\u67095\u79cd\uff0c\u589e\u6216\u51cf  \u6bd4\u8f83\u5e76\u4ea4\u6362  \u8f7d\u5165  \u5b58\u50a8  \u4ea4\u6362<\/p>\n<p>  atomic.AddUint32()<\/p>\n<p>  atomic.CompareAndSwapUint32()<\/p>\n<p>  atomic.LoadUint32() \u5f53\u8bfb\u53d6\u65f6\uff0c\u8ba1\u7b97\u673a\u4e2d\u7684\u4efb\u4f55 cpu \u90fd\u4e0d\u4f1a\u8fdb\u884c\u9488\u5bf9\u6b64\u503c\u7684\u8bfb\u5199\u64cd\u4f5c<\/p>\n<p>  atomic.SotreUint32() \u5f53\u5b58\u50a8\u65f6\uff0c\u4efb\u4f55 cpu \u90fd\u4e0d\u4f1a\u8fdb\u884c\u9488\u5bf9\u9488\u8fd9\u4e2a\u503c\u7684\u8bfb\u5199\u64cd\u4f5c<\/p>\n<p>  atomic.SwapUint32(*unit32,unit32) uint32\n<\/p><\/blockquote>\n<h2>GC\u89e6\u53d1\u7684\u6761\u4ef6<\/h2>\n<blockquote>\n<ol>\n<li>\u4e3b\u52a8\u89e6\u53d1\uff0c runtime.GC \u963b\u585e\u64cd\u4f5c<\/li>\n<li>\u88ab\u52a8\u89e6\u53d1 \u5b9a\u65f6\u89e6\u53d1\uff082min\uff09\u4e0e\u5185\u5b58\u5206\u914d\u6bd4\u4f8b\u89e6\u53d1\u914d\u5408\u5b8c\u6210<\/li>\n<\/ol>\n<\/blockquote>\n<h2>channel\u662f\u540c\u6b65\u8fd8\u662f\u5f02\u6b65<\/h2>\n<blockquote><p>\n  channel \u662f\u5f02\u6b65\u7684\uff0c\u5b58\u57283\u79cd\u72b6\u6001<\/p>\n<p>  nil\uff0c\u672a\u521d\u59cb\u5316\u7684\u72b6\u6001\uff0c\u53ea\u8fdb\u884c\u4e86\u58f0\u660e\uff0c\u6216\u8005\u624b\u52a8\u8d4b\u503c\u4e3anil<\/p>\n<p>  active\uff0c\u6b63\u5e38\u7684 channel \uff0c\u53ef\u8bfb\u53ef\u5199<\/p>\n<p>  closed\uff0c\u5df2\u5173\u95ed\uff0c\u5343\u4e07\u4e0d\u8981\u8ba4\u4e3a\u5173\u95ed channel \u540e\uff0cchannel \u7684\u503c\u662f nil\n<\/p><\/blockquote>\n<table>\n<thead>\n<tr>\n<th>\u64cd\u4f5c<\/th>\n<th>nil<\/th>\n<th>active<\/th>\n<th>closed<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u5173\u95ed<\/td>\n<td>panic<\/td>\n<td>\u6210\u529f\u5173\u95ed\uff08\u901a\u77e5\u6240\u6709\uff09<\/td>\n<td>panic<\/td>\n<\/tr>\n<tr>\n<td>\u53d1\u9001\u6570\u636e<\/td>\n<td>\u6c38\u4e45\u963b\u585e<\/td>\n<td>\u963b\u585e\u6216\u53d1\u9001\u6210\u529f<\/td>\n<td>panic<\/td>\n<\/tr>\n<tr>\n<td>\u63a5\u6536\u6570\u636e<\/td>\n<td>\u6c38\u4e45\u963b\u585e<\/td>\n<td>\u963b\u585e\u6216\u63a5\u6536\u6210\u529f<\/td>\n<td>\u6c38\u4e0d\u963b\u585e _,ok := &lt;-ch ok is false<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Go \u7684 struct \u80fd\u4e0d\u80fd\u6bd4\u8f83<\/h2>\n<blockquote><p>\n  \u76f8\u540c\u7684 struct \u7c7b\u578b\u53ef\u4ee5\u6bd4\u8f83<\/p>\n<p>  \u4e0d\u540c\u7684 struct \u7c7b\u578b\u4e0d\u53ef\u4ee5\u6bd4\u8f83\n<\/p><\/blockquote>\n<h2>Go reutn \u7684\u6267\u884c\u6b65\u9aa4<\/h2>\n<blockquote>\n<ol>\n<li>\u7ed9\u8fd4\u56de\u503c\u8d4b\u503c\uff08rval\uff09<\/li>\n<li>\u8c03\u7528 defer \u8868\u8fbe\u5f0f\uff08\u51fd\u6570\u4e2d\u6709 defer \u8bed\u53e5\u65f6\uff09\u4e0e <strong>defer<\/strong> \u6709\u5173<\/li>\n<li>\u8fd4\u56de\u7ed9\u8c03\u7528\u51fd\u6570\uff08ret\uff09<\/li>\n<\/ol>\n<\/blockquote>\n<h2>Go \u7684 select<\/h2>\n<blockquote><p>\n  Golang \u7684 select \u673a\u5236\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u662f\u5728<strong>\u8bed\u8a00\u5c42\u9762<\/strong>\uff08\u7528\u6237\u7a7a\u95f4\u5b9e\u73b0\uff0c\u6ca1\u6709\u7528\u6237\u6001\u548c\u5185\u6838\u6001\u7684\u5207\u6362\uff09\u5b9e\u73b0\u4e86\u548c select\uff0cpoll\uff0cepoll \u76f8\u4f3c\u7684\u529f\u80fd\u3002\u76d1\u542c\u591a\u4e2a\u63cf\u8ff0\u7b26\u7684\u8bfb\/\u5199\u4e8b\u4ef6\uff0c\u4e00\u65e6\u67d0\u4e2a\u63cf\u8ff0\u7b26\u5c31\u7eea\uff08\u4e00\u822c\u662f\u8bfb\u6216\u5199\u4e8b\u4ef6\u53d1\u751f\uff09\uff0c\u5c31\u80fd\u591f\u5c06\u53d1\u751f\u7684\u4e8b\u4ef6\u901a\u77e5\u7ed9\u5173\u5fc3\u7684\u5e94\u7528\u7a0b\u5e8f\u53bb\u5904\u7406\u8be5\u4e8b\u4ef6\u3002golang \u7684 select \u673a\u5236\u662f\uff0c\u76d1\u542c\u591a\u4e2a <strong>channel<\/strong>\uff0c\u6bcf\u4e00\u4e2a case \u662f\u4e00\u4e2a\u4e8b\u4ef6\uff0c\u53ef\u4ee5\u662f\u8bfb\u4e8b\u4ef6\u4e5f\u53ef\u4ee5\u662f\u5199\u4e8b\u4ef6\uff0c\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u6267\u884c\uff0c\u53ef\u4ee5\u8bbe\u7f6e default\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u5f53\u76d1\u542c\u7684\u591a\u4e2a\u4e8b\u4ef6\u963b\u585e\u4f4f\u4f1a\u6267\u884c default \u7684\u903b\u8f91\u3002\n<\/p><\/blockquote>\n<h2>Go \u7684 Slice \u5982\u4f55\u6269\u5bb9<\/h2>\n<pre><code class=\"language-go line-numbers\">type slice struct {\n    array unsafe.Pointer \/\/ \u6307\u5411\u4e00\u4e2a\u6570\u7ec4\u7684\u6307\u9488\n    len   int \/\/ \u957f\u5ea6\n    cap   int \/\/ \u5bb9\u91cf\n}\n<\/code><\/pre>\n<blockquote><p>\n  \u6211\u4eec\u5728\u5bf9 slice \u8fdb\u884c append \u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9020\u6210 slice \u7684\u81ea\u52a8\u6269\u5bb9\u3002<\/p>\n<ul>\n<li>\u5982\u679c\u5207\u7247\u7684\u5bb9\u91cf\u5c0f\u4e8e1024\u4e2a\u5143\u7d20\uff0c\u90a3\u4e48\u6269\u5bb9\u7684\u65f6\u5019slice\u7684cap\u5c31\u7ffb\u756a\uff0c\u4e58\u4ee52\uff1b\u4e00\u65e6\u5143\u7d20\u4e2a\u6570\u8d85\u8fc71024\u4e2a\u5143\u7d20\uff0c\u589e\u957f\u56e0\u5b50\u5c31\u53d8\u62101.25\uff0c\u5373\u6bcf\u6b21\u589e\u52a0\u539f\u6765\u5bb9\u91cf\u7684\u56db\u5206\u4e4b\u4e00<\/li>\n<li>\u5982\u679c\u6269\u5bb9\u4e4b\u540e\uff0c\u8fd8\u6ca1\u6709\u89e6\u53ca\u539f\u6570\u7ec4\u7684\u5bb9\u91cf\uff0c\u90a3\u4e48\uff0c\u5207\u7247\u4e2d\u7684\u6307\u9488\u6307\u5411\u7684\u4f4d\u7f6e\uff0c\u5c31\u8fd8\u662f\u539f\u6570\u7ec4\uff0c\u5982\u679c\u6269\u5bb9\u4e4b\u540e\uff0c\u8d85\u8fc7\u4e86\u539f\u6570\u7ec4\u7684\u5bb9\u91cf\uff0c\u90a3\u4e48\uff0cGo\u5c31\u4f1a\u5f00\u8f9f\u4e00\u5757\u65b0\u7684\u5185\u5b58\uff0c\u628a\u539f\u6765\u7684\u503c\u62f7\u8d1d\u8fc7\u6765\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e1d\u6beb\u4e0d\u4f1a\u5f71\u54cd\u5230\u539f\u6570\u7ec4<\/li>\n<\/ul>\n<\/blockquote>\n<h2>Go \u5185\u5b58\u9003\u9038\u5206\u6790<\/h2>\n<pre><code class=\"language-go line-numbers\">go run -gcflags \"-m -l\" main.go \n<\/code><\/pre>\n<blockquote><p>\n  \u5728Go\u4e2d\u9003\u9038\u5206\u6790\u662f\u4e00\u79cd\u786e\u5b9a\u6307\u9488\u52a8\u6001\u8303\u56f4\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5206\u6790\u5728\u7a0b\u5e8f\u7684\u54ea\u4e9b\u5730\u65b9\u53ef\u4ee5\u8bbf\u95ee\u5230\u6307\u9488\u3002\u5b83\u6d89\u53ca\u5230\u6307\u9488\u5206\u6790\u548c\u5f62\u72b6\u5206\u6790\u3002<\/p>\n<p>  \u5bfc\u81f4\u5185\u5b58\u9003\u9038\u7684\u60c5\u51b5\u6bd4\u8f83\u591a\uff0c\u6709\u4e9b\u53ef\u80fd\u8fd8\u662f\u5b98\u65b9\u672a\u80fd\u591f\u5b9e\u73b0\u7cbe\u786e\u7684\u5206\u6790\u9003\u9038\u60c5\u51b5\u7684 bug\uff0c\u901a\u5e38\u6765\u8bb2\u5c31\u662f\u5982\u679c\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4e0d\u4f1a\u6269\u5927\u5e76\u4e14\u5176\u884c\u4e3a\u6216\u8005\u5927\u5c0f\u80fd\u591f\u5728\u7f16\u8bd1\u7684\u65f6\u5019\u786e\u5b9a\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u90fd\u662f\u5206\u914d\u5230\u6808\u4e0a\uff0c\u5426\u5219\u5c31\u53ef\u80fd\u53d1\u751f\u5185\u5b58\u9003\u9038\u5206\u914d\u5230\u5806\u4e0a\u3002<\/p>\n<p>  \u5185\u5b58\u9003\u9038\u7684\u4e94\u79cd\u60c5\u51b5\uff1a<\/p>\n<ol>\n<li>channel\uff0c\u7531\u4e8e\u65e0\u6cd5\u5728\u7f16\u8bd1\u9636\u6bb5\u786e\u5b9a\u5176\u4f5c\u7528\u4e8e\u4e0e\u4f20\u9012\u7684\u8def\u5f84\uff0c\u6240\u4ee5\u4e00\u822c\u4f1a\u9003\u9038\u5230\u5806\u4e0a\u5206\u914d<\/li>\n<li>slice \u4e2d\u7684\u503c\u5305\u542b\u6307\u9488\uff0c\u5982[]*string\u7684\u7c7b\u578b\u3002\u5373\u4f7f\u5207\u7247\u7684\u5e95\u5c42\u5b58\u50a8\u6570\u7ec4\u4ecd\u7136\u53ef\u80fd\u5728\u6808\u4e0a\uff0c\u6570\u636e\u7684\u5f15\u7528\u4e5f\u4f1a\u8f6c\u79fb\u5230\u5806\u4e2d<\/li>\n<li>slice \u7531\u4e8e append \u64cd\u4f5c\u8d85\u51fa\u5176\u5bb9\u91cf\u3002\u5728\u7f16\u8bd1\u65f6\uff0cslice \u521d\u59cb\u5927\u5c0f\u5df2\u77e5\uff0c\u5c06\u4f1a\u5206\u914d\u5728\u6808\u4e0a\u3002\u5982\u679c\u5728\u8fd0\u884c\u65f6\u6269\u5bb9\uff0c\u5219\u5b83\u5c06\u88ab\u5206\u914d\u5230\u5806\u4e0a<\/li>\n<li>\u8c03\u7528\u63a5\u53e3\u7c7b\u578b\u7684\u65b9\u6cd5\u3002\u63a5\u53e3\u7c7b\u578b\u7684\u65b9\u6cd5\u8c03\u7528\u65f6\u52a8\u6001\u8c03\u7528\uff0c\u5b9e\u9645\u4f7f\u7528\u7684\u5177\u4f53\u5b9e\u73b0\u53ea\u80fd\u5728\u8fd0\u884c\u65f6\u786e\u5b9a<\/li>\n<li>\u5c3d\u7ba1\u80fd\u591f\u7b26\u5408\u5206\u914d\u5230\u6808\u7684\u573a\u666f\uff0c\u4f46\u662f\u5176\u5927\u5c0f\u4e0d\u80fd\u591f\u5728\u5728\u7f16\u8bd1\u65f6\u5019\u786e\u5b9a\u7684\u60c5\u51b5\uff0c\u4e5f\u4f1a\u5206\u914d\u5230\u5806\u4e0a<\/li>\n<\/ol>\n<p>  \u603b\u7ed3\u4e0b\u6765\u5c31\u662f\uff0c\u7f16\u8bd1\u65f6\u80fd\u591f\u786e\u5b9a\u7684\u5c5e\u4e8e\u9759\u6001\u5185\u5b58\uff0c\u5206\u914d\u5728\u6808\u4e0a\u3002\u7f16\u8bd1\u65f6\u65e0\u6cd5\u786e\u5b9a\u7684\u5c5e\u4e8e\u52a8\u6001\u5185\u5b58\uff0c\u5206\u914d\u5728\u5806\u4e0a\u3002\u8fd0\u884c\u65f6\u5927\u5c0f\u53d1\u751f\u53d8\u5316\u7684\uff0c\u6216\u8fd0\u884c\u65f6\u624d\u80fd\u786e\u5b9a\uff0c\u5206\u914d\u5728\u5806\u4e0a\u3002\n<\/p><\/blockquote>\n<h2>Go \u7684\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u600e\u4e48\u5206\u914d<\/h2>\n<blockquote>\n<ol>\n<li>\u6808\u5185\u5b58\u7684\u5206\u914d\n<p>  \u6808\u548c\u5806\u5728\u865a\u62df\u5185\u5b58\u4e0a2\u5757\u4e0d\u540c\u529f\u80fd\u7684\u5185\u5b58\u533a\u57df<\/p>\n<\/li>\n<\/ol>\n<ul>\n<li>\u6808\u5728\u9ad8\u5730\u5740\uff0c\u4ece\u9ad8\u5730\u5740\u5411\u4f4e\u5730\u5740\u589e\u957f<\/li>\n<li>\n<p>\u5806\u5728\u4f4e\u5730\u5740\uff0c\u4ece\u4f4e\u5730\u5740\u5411\u9ad8\u5730\u5740\u6574\u5f20<\/p>\n<p>  \u6808\u548c\u5806\u7684\u6bd4\u8f83\u4f18\u52bf\uff1a<\/p>\n<\/li>\n<li>\n<p>\u6808\u5185\u5b58\u6bd4\u8f83\u7b80\u5355\uff0c\u5206\u914d\u6bd4\u5806\u4e0a\u5feb<\/p>\n<\/li>\n<li>\u6808\u7684\u5185\u5b58\u4e0d\u9700\u8981\u56de\u6536\uff0c\u800c\u5806\u9700\u8981\u3002\u9700\u8981\u800c\u5916\u82b1\u8d39cpu\uff0c\u6709stw<\/li>\n<li>\u6808\u4e0a\u7684\u5185\u5b58\u6709\u66f4\u597d\u7684\u5c40\u90e8\u6027\u539f\u7406\uff0c\u5806\u5185\u5b58\u8bbf\u95ee\u76842\u5757\u6570\u636e\u53ef\u80fd\u5728\u4e0d\u540c\u7684\u9875\u4e0a<\/li>\n<\/ul>\n<ol start=\"2\">\n<li>\n<p>\u5806\u5185\u5b58\u7684\u5206\u914d<\/p>\n<p>  \u5185\u5b58\u7ba1\u7406GC\u95ee\u9898\uff0c\u4e3b\u8981\u662f\u8ba8\u8bba\u5806\u5185\u5b58\u7684\u7ba1\u7406\uff0c\u5206\u4e3a3\u90e8\u5206\uff1a<\/p>\n<\/li>\n<\/ol>\n<ul>\n<li>\u5206\u914d\u5185\u5b58\u5757<\/li>\n<li>\u56de\u6536\u5185\u5b58\u5757<\/li>\n<li>\u7ec4\u7ec7\u5185\u5b58\u5757<\/li>\n<\/ul>\n<p>Go \u7684\u5185\u5b58\u5206\u914d\u7c7b\u4f3c TCMalloc \uff0c\u5c0f\u5bf9\u8c61\u548c\u5927\u5bf9\u8c61\u53ea\u6709\u5927\u5c0f\u533a\u5206\uff0c\u65e0\u5176\u4ed6\u533a\u5206\u3002\u6309\u5927\u5c0f\u5206\u4e3a67\u7c7b\uff0c\u6700\u592732kb\u3002\u8d85\u8fc732kb\u7684\u5927\u5bf9\u8c61\uff0c\u76f4\u63a5\u4ece mheap \u5206\u914d<\/p>\n<p>  Go \u7684\u5185\u5b58\u5206\u914d\u539f\u5219\uff1a<\/p>\n<p>  Go \u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\uff0c\u4f1a\u5148\u5411\u64cd\u4f5c\u7cfb\u7edf\u7533\u8bf7\u4e00\u5757\u5185\u5b58\uff08\u865a\u62df\u5730\u5740\u7a7a\u95f4\uff09\uff0c\u5207\u6210\u5c0f\u5757\u540e\u81ea\u5df1\u7ba1\u7406\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/oss.wyxxt.org.cn\/images\/2021\/09\/18\/wp_editor_md_ea5493bbfabc0e9046503865bcb83077.jpg\" alt=\"\" \/><\/p>\n<blockquote><p>\n  aeana \u533a\u57df\u5c31\u662f\u5806\u533a\uff0cGo \u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u90fd\u5728\u8fd9\u4e2a\u533a\u57df\uff0c\u5b83\u628a\u5185\u5b58\u5206\u5272\u62108kb\u5927\u5c0f\u7684\u9875\uff0c\u4e00\u4e9b\u9875\u7ec4\u5408\u8d77\u6765\u6210\u4e3a mspan\u3002<\/p>\n<p>  bitmap \u533a\u57df\u6807\u8bc6 arena \u533a\u57df\u54ea\u4e9b\u5730\u65b9\u4fdd\u5b58\u4e86\u5bf9\u8c61\uff0c\u5e76\u4e14\u75284bit\u6807\u5fd7\u4f4d\u8868\u793a\u5bf9\u8c61\u662f\u5426\u5305\u542b\u6307\u9488\u3001GC\u6807\u8bb0\u4fe1\u606f\u3002bitmap \u4e2d\u4e00\u4e2abyte \u5927\u5c0f\u7684\u5185\u5b58\u5bf9\u5e94 arena \u533a\u57df\u4e2d4\u4e2a\u6307\u9488\u5927\u5c0f\uff088B\uff09\u7684\u5185\u5b58\uff0c\u6240\u4ee5 bitmap \u7684\u5927\u5c0f\u662f 512GB \/ (4 * 8B) = 16GB\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/oss.wyxxt.org.cn\/images\/2021\/09\/18\/wp_editor_md_5c8b2db83699ad2a596dd6ac2e855aee.jpg\" alt=\"\" \/><\/p>\n<h2>Go \u5185\u5b58\u6cc4\u6f0f<\/h2>\n<ul>\n<li>\u9884\u671f\u80fd\u88ab\u5feb\u901f\u91ca\u653e\u7684\u5185\u5b58\u56e0\u88ab\u6839\u5bf9\u8c61\u5f15\u7528\u800c\u6ca1\u6709\u5f97\u5230\u8fc5\u901f\u91ca\u653e<\/li>\n<li>goroutine \u6cc4\u6f0f<\/li>\n<\/ul>\n<h2>Go new \u548c make \u7684\u533a\u522b<\/h2>\n<blockquote><p>\n  \u503c\u7c7b\u578b\uff1aint   float   bool    string  struct  \u548c   array<\/p>\n<p>  \u53d8\u91cf\u76f4\u63a5\u5b58\u50a8\u503c\uff0c\u5206\u914d\u6808\u533a\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u8fd9\u4e9b\u53d8\u91cf\u6240\u5360\u636e\u7684\u7a7a\u95f4\u5728\u51fd\u6570\u88ab\u8c03\u7528\u5b8c\u6210\u540e\u4f1a\u81ea\u52a8\u91ca\u653e<\/p>\n<p>  \u5f15\u7528\u7c7b\u578b\uff1aslice    map chan    interface   func    \u548c   \u6307\u9488<\/p>\n<p>  \u53d8\u91cf\u5b58\u50a8\u7684\u662f\u4e00\u4e2a\u5730\u5740\uff08\u6216\u8005\u7406\u89e3\u4e3a\u6307\u9488\uff09\uff0c\u6307\u9488\u6307\u5411\u5185\u5b58\u4e2d\u771f\u6b63\u5b58\u50a8\u6570\u636e\u7684\u9996\u5730\u5740\u3002\u5185\u5b58\u901a\u5e38\u5728\u5806\u4e0a\u5206\u914d\uff0c\u901a\u8fc7 GC \u56de\u6536<\/p>\n<p>  \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5bf9\u4e8e\u5f15\u7528\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u6211\u4eec\u4e0d\u4ec5\u8981\u58f0\u660e\u53d8\u91cf\uff0c\u66f4\u91cd\u8981\u7684\u662f\uff0c\u6211\u4eec\u5f97\u624b\u52a8\u4e3a\u5b83\u5206\u914d\u7a7a\u95f4<\/p>\n<p>  \u56e0\u6b64\uff0cnew\u8be5\u65b9\u6cd5\u7684\u53c2\u6570\u8981\u6c42\u4f20\u5165\u4e00\u4e2a\u7c7b\u578b\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u503c\uff0c\u5b83\u4f1a\u7533\u8bf7\u8be5\u7c7b\u578b\u5927\u5c0f\u7684\u7a7a\u95f4\uff0c\u5e76\u4f1a\u521d\u59cb\u5316\u4e3a\u5bf9\u5e94\u7684\u96f6\u503c\uff0c\u8fd4\u56de\u8be5\u5185\u5b58\u7a7a\u95f4\u7684\u4e00\u4e2a\u6307\u9488\n<\/p><\/blockquote>\n<pre><code class=\"language-go line-numbers\">func new(Type) *Type\n<\/code><\/pre>\n<blockquote><p>\n  \u800cmake\u4e5f\u662f\u7528\u4e8e\u5185\u5b58\u5206\u914d\uff0c\u4f46\u662f\u548cnew \u4e0d\u540c\uff0c\u53ea\u80fd\u7528\u4e8e\u5f15\u7528\u5bf9\u8c61\u7684\u5185\u5b58\u521b\u5efa\uff0c\u5b83\u8fd4\u56de\u7684\u7c7b\u578b\u5c31\u662f\u7c7b\u578b\u672c\u7701\uff0c\u800c\u4e0d\u662f\u5b83\u4eec\u7684\u6307\u9488\n<\/p><\/blockquote>\n<pre><code class=\"language-go line-numbers\">func make(t Type, size ...IntergerType) Type\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u7528\u7c7b\u578b slice map channel interface func\u90fd\u662f\u5f15\u7528\u7c7b\u578b \u9664\u5f15\u7528\u7c7b\u578b\u5916\u7684\u5176\u4ed6\u7c7b\u578b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[15],"tags":[396],"class_list":["post-3626","post","type-post","status-publish","format-standard","hentry","category-15","tag-396"],"_links":{"self":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/3626","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3626"}],"version-history":[{"count":2,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/3626\/revisions"}],"predecessor-version":[{"id":3633,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/3626\/revisions\/3633"}],"wp:attachment":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}