{"id":39344,"date":"2025-05-23T09:49:40","date_gmt":"2025-05-23T01:49:40","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/39344.html"},"modified":"2025-05-23T09:49:40","modified_gmt":"2025-05-23T01:49:40","slug":"02-golang-%e5%88%87%e7%89%87%ef%bc%88slice%ef%bc%89%e6%ba%90%e7%a0%81%e5%88%86%e6%9e%90%ef%bc%88%e4%b8%80%e3%80%81%e5%ae%9a%e4%b9%89%e4%b8%8e%e5%9f%ba%e7%a1%80%e6%93%8d%e4%bd%9c%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/39344.html","title":{"rendered":"02.Golang \u5207\u7247\uff08slice\uff09\u6e90\u7801\u5206\u6790\uff08\u4e00\u3001\u5b9a\u4e49\u4e0e\u57fa\u7840\u64cd\u4f5c\u5b9e\u73b0\uff09"},"content":{"rendered":"<h2>Golang \u5207\u7247&#xff08;slice&#xff09;\u6e90\u7801\u5206\u6790&#xff08;\u4e00\u3001\u5b9a\u4e49\u4e0e\u57fa\u7840\u64cd\u4f5c\u5b9e\u73b0&#xff09;<\/h2>\n<p>\u6ce8\u610f\u5f53\u524dgo\u7248\u672c\u4ee3\u7801\u4e3a1.23<\/p>\n<h3>\u4e00\u3001\u5b9a\u4e49<\/h3>\n<p>slice \u7684\u5e95\u5c42\u6570\u636e\u662f\u6570\u7ec4&#xff0c;slice \u662f\u5bf9\u6570\u7ec4\u7684\u5c01\u88c5&#xff0c;\u5b83\u63cf\u8ff0\u4e00\u4e2a\u6570\u7ec4\u7684\u7247\u6bb5\u3002\u4e24\u8005\u90fd\u53ef\u4ee5\u901a\u8fc7\u4e0b\u6807\u6765\u8bbf\u95ee\u5355\u4e2a\u5143\u7d20\u3002<\/p>\n<p>\u6570\u7ec4\u662f\u5b9a\u957f\u7684&#xff0c;\u957f\u5ea6\u5b9a\u4e49\u597d\u4e4b\u540e&#xff0c;\u4e0d\u80fd\u518d\u66f4\u6539\u3002\u5728 Go \u4e2d&#xff0c;\u6570\u7ec4\u662f\u4e0d\u5e38\u89c1\u7684&#xff0c;\u56e0\u4e3a\u5176\u957f\u5ea6\u662f\u7c7b\u578b\u7684\u4e00\u90e8\u5206&#xff0c;\u9650\u5236\u4e86\u5b83\u7684\u8868\u8fbe\u80fd\u529b&#xff0c;\u6bd4\u5982 [3]int \u548c [4]int \u5c31\u662f\u4e0d\u540c\u7684\u7c7b\u578b\u3002<\/p>\n<p>\u800c\u5207\u7247\u5219\u975e\u5e38\u7075\u6d3b&#xff0c;\u5b83\u53ef\u4ee5\u52a8\u6001\u5730\u6269\u5bb9\u3002\u5207\u7247\u7684\u7c7b\u578b\u548c\u957f\u5ea6\u65e0\u5173\u3002<\/p>\n<p>\u6570\u7ec4\u5c31\u662f\u4e00\u7247\u8fde\u7eed\u7684\u5185\u5b58&#xff0c; slice \u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53&#xff0c;\u5305\u542b\u4e09\u4e2a\u5b57\u6bb5&#xff1a;\u957f\u5ea6\u3001\u5bb9\u91cf\u3001\u5e95\u5c42\u6570\u7ec4\u3002<\/p>\n<p>type slice struct {<br \/>\narray unsafe.Pointer<br \/>\nlen   int<br \/>\ncap   int<br \/>\n}<\/p>\n<p>\u6570\u636e\u7ed3\u6784\u5982\u4e0b&#xff1a;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014937-682fd43166f24.png\" alt=\"\u5207\u7247\u6570\u636e\u7ed3\u6784\" \/><\/p>\n<p>\u6ce8\u610f&#xff0c;\u5e95\u5c42\u6570\u7ec4\u662f\u53ef\u4ee5\u88ab\u591a\u4e2a slice \u540c\u65f6\u6307\u5411\u7684&#xff0c;\u56e0\u6b64\u5bf9\u4e00\u4e2a slice \u7684\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\u662f\u6709\u53ef\u80fd\u5f71\u54cd\u5230\u5176\u4ed6 slice \u7684\u3002<\/p>\n<h3>\u4e8c\u3001\u64cd\u4f5c<\/h3>\n<h4>2.1\u521b\u5efa<\/h4>\n<p>src\/runtime\/slice.go \u4e3b\u8981\u903b\u8f91\u5c31\u662f\u57fa\u4e8e\u5bb9\u91cf\u7533\u8bf7\u5185\u5b58\u3002<\/p>\n<p><span class=\"token keyword\">func<\/span> <span class=\"token function\">makeslice<\/span><span class=\"token punctuation\">(<\/span>et <span class=\"token operator\">*<\/span>_type<span class=\"token punctuation\">,<\/span> <span class=\"token builtin\">len<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token builtin\">cap<\/span> <span class=\"token builtin\">int<\/span><span class=\"token punctuation\">)<\/span> unsafe<span class=\"token punctuation\">.<\/span>Pointer <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u8ba1\u7b97\u5207\u7247\u6240\u9700\u7684\u5185\u5b58\u5927\u5c0f&#xff0c;cap \u4e3a\u5207\u7247\u5bb9\u91cf&#xff0c;et.Size_ \u4e3a\u6bcf\u4e2a\u5143\u7d20\u7684\u5927\u5c0f<\/span><br \/>\n        <span class=\"token comment\">\/\/ math.MulUintptr \u6267\u884c\u65e0\u7b26\u53f7\u6574\u6570\u4e58\u6cd5&#xff0c;\u5e76\u8fd4\u56de\u7ed3\u679c\u548c\u4e00\u4e2a\u8868\u793a\u662f\u5426\u53d1\u751f\u6ea2\u51fa\u7684\u5e03\u5c14\u503c<\/span><br \/>\n        mem<span class=\"token punctuation\">,<\/span> overflow <span class=\"token operator\">:&#061;<\/span> math<span class=\"token punctuation\">.<\/span><span class=\"token function\">MulUintptr<\/span><span class=\"token punctuation\">(<\/span>et<span class=\"token punctuation\">.<\/span>Size_<span class=\"token punctuation\">,<\/span> <span class=\"token function\">uintptr<\/span><span class=\"token punctuation\">(<\/span><span class=\"token builtin\">cap<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u68c0\u67e5\u4ee5\u4e0b\u51e0\u79cd\u9519\u8bef\u60c5\u51b5&#xff1a;<\/span><br \/>\n        <span class=\"token comment\">\/\/ 1. overflow&#xff1a;\u5bb9\u91cf\u8ba1\u7b97\u65f6\u53d1\u751f\u6ea2\u51fa<\/span><br \/>\n        <span class=\"token comment\">\/\/ 2. mem &gt; maxAlloc&#xff1a;\u6240\u9700\u7684\u5185\u5b58\u8d85\u8fc7\u6700\u5927\u53ef\u5206\u914d\u5185\u5b58<\/span><br \/>\n        <span class=\"token comment\">\/\/ 3. len &lt; 0&#xff1a;\u5207\u7247\u957f\u5ea6\u4e3a\u8d1f\u6570<\/span><br \/>\n        <span class=\"token comment\">\/\/ 4. len &gt; cap&#xff1a;\u5207\u7247\u957f\u5ea6\u5927\u4e8e\u5bb9\u91cf<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> overflow <span class=\"token operator\">||<\/span> mem <span class=\"token operator\">&gt;<\/span> maxAlloc <span class=\"token operator\">||<\/span> <span class=\"token builtin\">len<\/span> <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span> <span class=\"token operator\">||<\/span> <span class=\"token builtin\">len<\/span> <span class=\"token operator\">&gt;<\/span> <span class=\"token builtin\">cap<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token comment\">\/\/ \u6ce8\u610f&#xff1a;\u5f53\u7528\u6237\u6267\u884c make([]T, bignumber) \u65f6&#xff0c;\u4ea7\u751f &#034;len out of range&#034; \u9519\u8bef&#xff0c;\u800c\u4e0d\u662f &#034;cap out of range&#034; \u9519\u8bef\u3002<\/span><br \/>\n                <span class=\"token comment\">\/\/ \u867d\u7136 &#034;cap out of range&#034; \u4e5f\u662f\u5bf9\u7684&#xff0c;\u4f46\u7531\u4e8e\u5bb9\u91cf\u662f\u9690\u5f0f\u63d0\u4f9b\u7684&#xff0c;\u56e0\u6b64\u63d0\u793a\u957f\u5ea6\u9519\u8bef\u66f4\u6e05\u6670\u3002<\/span><br \/>\n                <span class=\"token comment\">\/\/ \u53c2\u8003&#xff1a;golang.org\/issue\/4085<\/span><br \/>\n                <span class=\"token comment\">\/\/ \u91cd\u65b0\u8ba1\u7b97\u57fa\u4e8e\u957f\u5ea6\u7684\u5185\u5b58\u5927\u5c0f&#xff0c;\u4ee5\u4fbf\u5728\u5bb9\u91cf\u6ea2\u51fa\u65f6\u63d0\u4f9b\u66f4\u51c6\u786e\u7684\u9519\u8bef\u4fe1\u606f&#xff08;\u4f8b\u5982 len \u4e5f\u6ea2\u51fa&#xff09;<\/span><br \/>\n                mem<span class=\"token punctuation\">,<\/span> overflow <span class=\"token operator\">:&#061;<\/span> math<span class=\"token punctuation\">.<\/span><span class=\"token function\">MulUintptr<\/span><span class=\"token punctuation\">(<\/span>et<span class=\"token punctuation\">.<\/span>Size_<span class=\"token punctuation\">,<\/span> <span class=\"token function\">uintptr<\/span><span class=\"token punctuation\">(<\/span><span class=\"token builtin\">len<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n                <span class=\"token comment\">\/\/ \u518d\u6b21\u68c0\u67e5\u6ea2\u51fa\u3001\u5185\u5b58\u8d85\u51fa\u9650\u5236\u4ee5\u53ca\u957f\u5ea6\u4e3a\u8d1f\u6570\u7684\u60c5\u51b5<\/span><br \/>\n                <span class=\"token keyword\">if<\/span> overflow <span class=\"token operator\">||<\/span> mem <span class=\"token operator\">&gt;<\/span> maxAlloc <span class=\"token operator\">||<\/span> <span class=\"token builtin\">len<\/span> <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                        <span class=\"token function\">panicmakeslicelen<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token comment\">\/\/ \u957f\u5ea6\u9519\u8bef&#xff0c;\u89e6\u53d1 panic<\/span><br \/>\n                <span class=\"token punctuation\">}<\/span><br \/>\n                <span class=\"token function\">panicmakeslicecap<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token comment\">\/\/ \u5bb9\u91cf\u9519\u8bef&#xff0c;\u89e6\u53d1 panic<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u8c03\u7528 mallocgc \u5206\u914d\u5185\u5b58\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ mallocgc \u662f Go \u8fd0\u884c\u65f6\u4e2d\u7684\u51fd\u6570&#xff0c;\u7528\u4e8e\u5206\u914d\u5185\u5b58\u5e76\u5728\u5783\u573e\u56de\u6536\u5668\u4e2d\u6ce8\u518c\u8be5\u5185\u5b58\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u53c2\u6570&#xff1a;<\/span><br \/>\n        <span class=\"token comment\">\/\/   mem: \u8981\u5206\u914d\u7684\u5185\u5b58\u5927\u5c0f<\/span><br \/>\n        <span class=\"token comment\">\/\/   et: \u5207\u7247\u5143\u7d20\u7684\u7c7b\u578b<\/span><br \/>\n        <span class=\"token comment\">\/\/   true: \u6307\u793a\u5206\u914d\u7684\u5185\u5b58\u5e94\u8be5\u88ab\u6e05\u96f6<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token function\">mallocgc<\/span><span class=\"token punctuation\">(<\/span>mem<span class=\"token punctuation\">,<\/span> et<span class=\"token punctuation\">,<\/span> <span class=\"token boolean\">true<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h4>2.2\u6269\u5bb9<\/h4>\n<p>\u6838\u5fc3\u6e90\u7801 growslice-\u300bnextslicecap&amp;&amp;roundupsize<\/p>\n<p>\u9996\u5148\u901a\u8fc7nextslicecap\u8ba1\u7b97\u51fa\u4e00\u4e2a\u521d\u6b65\u7684\u6269\u5bb9\u503c&#xff0c;\u901a\u8fc7roundupsize\u5185\u5b58\u5bf9\u9f50\u5f97\u51fa\u6700\u7ec8\u7684\u6269\u5bb9\u503c&#xff0c;\u5e76\u4e0d\u662f\u7f51\u4e0a\u7684\u7b80\u5355\u516c\u5f0f\u3002<\/p>\n<p>\u57281.18\u7248\u672c\u4e4b\u524d&#xff0c;slice\u6e90\u7801\u4e2dnextslicecap\u6269\u5bb9\u4e3b\u8981\u903b\u8f91\u4e3a&#xff1a;<\/p>\n<p>\u5f53\u539f slice \u5bb9\u91cf\u5c0f\u4e8e 1024 \u7684\u65f6\u5019&#xff0c;\u65b0 slice \u5bb9\u91cf\u53d8\u6210\u539f\u6765\u7684 2 \u500d&#xff1b;\u539f slice \u5bb9\u91cf\u8d85\u8fc7 1024&#xff0c;\u65b0 slice \u5bb9\u91cf\u53d8\u6210\u539f\u6765\u76841.25\u500d\u3002<\/p>\n<p>\u57281.18\u7248\u672c\u66f4\u65b0\u4e4b\u540e&#xff0c;slice\u6e90\u7801\u4e2dnextslicecap\u7684\u6269\u5bb9\u4e3b\u8981\u903b\u8f91\u53d8\u4e3a\u4e86&#xff1a;<\/p>\n<p>\u5f53\u539fslice\u5bb9\u91cf(oldcap)\u5c0f\u4e8e256\u7684\u65f6\u5019&#xff0c;\u65b0slice(newcap)\u5bb9\u91cf\u4e3a\u539f\u6765\u76842\u500d&#xff1b;\u539fslice\u5bb9\u91cf\u8d85\u8fc7256&#xff0c;\u65b0slice\u5bb9\u91cfnewcap &#061; oldcap&#043;(oldcap&#043;3*256)\/4<\/p>\n<p>\u4e0b\u8ff0\u4e3ago1.23\u6e90\u7801\u903b\u8f91src\/runtime\/slice.go &#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ growslice \u4e3a\u4e00\u4e2a\u5207\u7247\u5206\u914d\u65b0\u7684\u5e95\u5c42\u5b58\u50a8\u3002<\/span><br \/>\n<span class=\"token comment\">\/\/<\/span><br \/>\n<span class=\"token comment\">\/\/ \u53c2\u6570:<\/span><br \/>\n<span class=\"token comment\">\/\/ oldPtr &#061; \u6307\u5411\u5207\u7247\u5e95\u5c42\u6570\u7ec4\u7684\u6307\u9488<\/span><br \/>\n<span class=\"token comment\">\/\/ newLen &#061; \u65b0\u7684\u957f\u5ea6&#xff08;&#061; oldLen &#043; num&#xff09;<\/span><br \/>\n<span class=\"token comment\">\/\/ oldCap &#061; \u539f\u59cb\u5207\u7247\u7684\u5bb9\u91cf<\/span><br \/>\n<span class=\"token comment\">\/\/ num &#061; \u6b63\u5728\u6dfb\u52a0\u7684\u5143\u7d20\u6570\u91cf<\/span><br \/>\n<span class=\"token comment\">\/\/ et &#061; \u5143\u7d20\u7c7b\u578b<\/span><br \/>\n<span class=\"token comment\">\/\/ \u8fd4\u56de\u503c:<\/span><br \/>\n<span class=\"token comment\">\/\/ newPtr &#061; \u6307\u5411\u65b0\u5e95\u5c42\u5b58\u50a8\u7684\u6307\u9488<\/span><br \/>\n<span class=\"token comment\">\/\/ newLen &#061; \u65b0\u7684\u957f\u5ea6\u4e0e\u4f20\u53c2\u76f8\u540c<\/span><br \/>\n<span class=\"token comment\">\/\/ newCap &#061; \u65b0\u5e95\u5c42\u5b58\u50a8\u7684\u5bb9\u91cf<\/span><br \/>\n<span class=\"token comment\">\/\/<\/span><br \/>\n<span class=\"token comment\">\/\/ \u8981\u6c42 uint(newLen) &gt; uint(oldCap)\u3002<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5047\u8bbe\u539f\u59cb\u5207\u7247\u7684\u957f\u5ea6\u4e3a newLen &#8211; num<\/span><br \/>\n<span class=\"token comment\">\/\/<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5206\u914d\u4e00\u4e2a\u81f3\u5c11\u80fd\u5bb9\u7eb3 newLen \u4e2a\u5143\u7d20\u7684\u65b0\u5e95\u5c42\u5b58\u50a8\u3002<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5df2\u5b58\u5728\u7684\u6761\u76ee [0, oldLen) \u88ab\u590d\u5236\u5230\u65b0\u7684\u5e95\u5c42\u5b58\u50a8\u4e2d\u3002<\/span><br \/>\n<span class=\"token comment\">\/\/ \u6dfb\u52a0\u7684\u6761\u76ee [oldLen, newLen) \u4e0d\u4f1a\u88ab growslice \u521d\u59cb\u5316<\/span><br \/>\n<span class=\"token comment\">\/\/ &#xff08;\u867d\u7136\u5bf9\u4e8e\u5305\u542b\u6307\u9488\u7684\u5143\u7d20\u7c7b\u578b&#xff0c;\u5b83\u4eec\u4f1a\u88ab\u6e05\u96f6&#xff09;\u3002\u8c03\u7528\u8005\u5fc5\u987b\u521d\u59cb\u5316\u8fd9\u4e9b\u6761\u76ee\u3002<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5c3e\u968f\u6761\u76ee [newLen, newCap) \u88ab\u6e05\u96f6\u3002<\/span><br \/>\n<span class=\"token comment\">\/\/<\/span><br \/>\n<span class=\"token comment\">\/\/ growslice \u7684\u7279\u6b8a\u8c03\u7528\u7ea6\u5b9a\u4f7f\u5f97\u8c03\u7528\u6b64\u51fd\u6570\u7684\u751f\u6210\u4ee3\u7801\u66f4\u7b80\u5355\u3002\u7279\u522b\u662f&#xff0c;\u5b83\u63a5\u53d7\u5e76\u8fd4\u56de<\/span><br \/>\n<span class=\"token comment\">\/\/ \u65b0\u7684\u957f\u5ea6&#xff0c;\u8fd9\u6837\u65e7\u7684\u957f\u5ea6\u4e0d\u9700\u8981\u88ab\u4fdd\u7559\/\u6062\u590d&#xff0c;\u800c\u65b0\u7684\u957f\u5ea6\u8fd4\u56de\u65f6\u4e5f\u4e0d\u9700\u8981\u88ab\u4fdd\u7559\/\u6062\u590d\u3002<\/span><br \/>\n<span class=\"token keyword\">func<\/span> <span class=\"token function\">growslice<\/span><span class=\"token punctuation\">(<\/span>oldPtr unsafe<span class=\"token punctuation\">.<\/span>Pointer<span class=\"token punctuation\">,<\/span> newLen<span class=\"token punctuation\">,<\/span> oldCap<span class=\"token punctuation\">,<\/span> num <span class=\"token builtin\">int<\/span><span class=\"token punctuation\">,<\/span> et <span class=\"token operator\">*<\/span>_type<span class=\"token punctuation\">)<\/span> slice <span class=\"token punctuation\">{<\/span><br \/>\n        oldLen <span class=\"token operator\">:&#061;<\/span> newLen <span class=\"token operator\">&#8211;<\/span> num<br \/>\n  <span class=\"token comment\">\/\/ &#8230; \u51fd\u6570\u975e\u6838\u5fc3\u90e8\u5206\u7701\u7565<\/span><\/p>\n<p>        <span class=\"token keyword\">if<\/span> newLen <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token function\">panic<\/span><span class=\"token punctuation\">(<\/span><span class=\"token function\">errorString<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;growslice: len out of range&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><\/p>\n<p>        <span class=\"token keyword\">if<\/span> et<span class=\"token punctuation\">.<\/span>Size_ <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token comment\">\/\/ append\u4e0d\u5e94\u8be5\u521b\u5efa\u4e00\u4e2a\u6307\u9488\u4e3anil\u4f46\u957f\u5ea6\u975e\u96f6\u7684\u5207\u7247\u3002<\/span><br \/>\n                <span class=\"token comment\">\/\/ \u6211\u4eec\u5047\u8bbe\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b&#xff0c;append\u4e0d\u9700\u8981\u4fdd\u7559oldPtr\u3002<\/span><br \/>\n                <span class=\"token keyword\">return<\/span> slice<span class=\"token punctuation\">{<\/span>unsafe<span class=\"token punctuation\">.<\/span><span class=\"token function\">Pointer<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>zerobase<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> newLen<span class=\"token punctuation\">,<\/span> newLen<span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ 1.\u8ba1\u7b97\u65b0\u5bb9\u91cf<\/span><br \/>\n        newcap <span class=\"token operator\">:&#061;<\/span> <span class=\"token function\">nextslicecap<\/span><span class=\"token punctuation\">(<\/span>newLen<span class=\"token punctuation\">,<\/span> oldCap<span class=\"token punctuation\">)<\/span><\/p>\n<p>        <span class=\"token keyword\">var<\/span> overflow <span class=\"token builtin\">bool<\/span><br \/>\n        <span class=\"token keyword\">var<\/span> lenmem<span class=\"token punctuation\">,<\/span> newlenmem<span class=\"token punctuation\">,<\/span> capmem <span class=\"token builtin\">uintptr<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u9488\u5bf9\u5e38\u89c1\u7684 et.Size \u8fdb\u884c\u4f18\u5316<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u5bf9\u4e8e1\u6211\u4eec\u4e0d\u9700\u8981\u4efb\u4f55\u9664\u6cd5\/\u4e58\u6cd5\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u5bf9\u4e8e goarch.PtrSize, \u7f16\u8bd1\u5668\u4f1a\u4f18\u5316\u9664\u6cd5\/\u4e58\u6cd5\u4e3a\u4e00\u4e2a\u5e38\u91cf\u79fb\u4f4d\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u5bf9\u4e8e2\u7684\u5e42&#xff0c;\u4f7f\u7528\u53d8\u91cf\u79fb\u4f4d\u3002<\/span><br \/>\n        noscan <span class=\"token operator\">:&#061;<\/span> <span class=\"token operator\">!<\/span>et<span class=\"token punctuation\">.<\/span><span class=\"token function\">Pointers<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token comment\">\/\/ 2.\u5185\u5b58\u5bf9\u9f50<\/span><br \/>\n        <span class=\"token keyword\">switch<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">case<\/span> et<span class=\"token punctuation\">.<\/span>Size_ <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">:<\/span><br \/>\n                lenmem <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">uintptr<\/span><span class=\"token punctuation\">(<\/span>oldLen<span class=\"token punctuation\">)<\/span><br \/>\n                newlenmem <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">uintptr<\/span><span class=\"token punctuation\">(<\/span>newLen<span class=\"token punctuation\">)<\/span><br \/>\n                capmem <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">roundupsize<\/span><span class=\"token punctuation\">(<\/span><span class=\"token function\">uintptr<\/span><span class=\"token punctuation\">(<\/span>newcap<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> noscan<span class=\"token punctuation\">)<\/span><br \/>\n                overflow <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">uintptr<\/span><span class=\"token punctuation\">(<\/span>newcap<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&gt;<\/span> maxAlloc<br \/>\n                newcap <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">int<\/span><span class=\"token punctuation\">(<\/span>capmem<span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">case<\/span> et<span class=\"token punctuation\">.<\/span>Size_ <span class=\"token operator\">&#061;&#061;<\/span> goarch<span class=\"token punctuation\">.<\/span>PtrSize<span class=\"token punctuation\">:<\/span><br \/>\n                lenmem <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">uintptr<\/span><span class=\"token punctuation\">(<\/span>oldLen<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">*<\/span> goarch<span class=\"token punctuation\">.<\/span>PtrSize<br \/>\n                newlenmem <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">uintptr<\/span><span class=\"token punctuation\">(<\/span>newLen<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">*<\/span> goarch<span class=\"token punctuation\">.<\/span>PtrSize<br \/>\n                capmem <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">roundupsize<\/span><span class=\"token punctuation\">(<\/span><span class=\"token function\">uintptr<\/span><span class=\"token punctuation\">(<\/span>newcap<span class=\"token punctuation\">)<\/span><span class=\"token operator\">*<\/span>goarch<span class=\"token punctuation\">.<\/span>PtrSize<span class=\"token punctuation\">,<\/span> noscan<span class=\"token punctuation\">)<\/span><br \/>\n                overflow <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">uintptr<\/span><span class=\"token punctuation\">(<\/span>newcap<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&gt;<\/span> maxAlloc<span class=\"token operator\">\/<\/span>goarch<span class=\"token punctuation\">.<\/span>PtrSize<br \/>\n                newcap <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">int<\/span><span class=\"token punctuation\">(<\/span>capmem <span class=\"token operator\">\/<\/span> goarch<span class=\"token punctuation\">.<\/span>PtrSize<span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">case<\/span> <span class=\"token function\">isPowerOfTwo<\/span><span class=\"token punctuation\">(<\/span>et<span class=\"token punctuation\">.<\/span>Size_<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n                <span class=\"token keyword\">var<\/span> shift <span class=\"token builtin\">uintptr<\/span><br \/>\n                <span class=\"token keyword\">if<\/span> goarch<span class=\"token punctuation\">.<\/span>PtrSize <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">8<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                        shift <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">uintptr<\/span><span class=\"token punctuation\">(<\/span>sys<span class=\"token punctuation\">.<\/span><span class=\"token function\">TrailingZeros64<\/span><span class=\"token punctuation\">(<\/span><span class=\"token function\">uint64<\/span><span class=\"token punctuation\">(<\/span>et<span class=\"token punctuation\">.<\/span>Size_<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&amp;<\/span> <span class=\"token number\">63<\/span><br \/>\n                <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                        shift <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">uintptr<\/span><span class=\"token punctuation\">(<\/span>sys<span class=\"token punctuation\">.<\/span><span class=\"token function\">TrailingZeros32<\/span><span class=\"token punctuation\">(<\/span><span class=\"token function\">uint32<\/span><span class=\"token punctuation\">(<\/span>et<span class=\"token punctuation\">.<\/span>Size_<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&amp;<\/span> <span class=\"token number\">31<\/span><br \/>\n                <span class=\"token punctuation\">}<\/span><br \/>\n                lenmem <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">uintptr<\/span><span class=\"token punctuation\">(<\/span>oldLen<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&lt;&lt;<\/span> shift<br \/>\n                newlenmem <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">uintptr<\/span><span class=\"token punctuation\">(<\/span>newLen<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&lt;&lt;<\/span> shift<br \/>\n                capmem <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">roundupsize<\/span><span class=\"token punctuation\">(<\/span><span class=\"token function\">uintptr<\/span><span class=\"token punctuation\">(<\/span>newcap<span class=\"token punctuation\">)<\/span><span class=\"token operator\">&lt;&lt;<\/span>shift<span class=\"token punctuation\">,<\/span> noscan<span class=\"token punctuation\">)<\/span><br \/>\n                overflow <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">uintptr<\/span><span class=\"token punctuation\">(<\/span>newcap<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&gt;<\/span> <span class=\"token punctuation\">(<\/span>maxAlloc <span class=\"token operator\">&gt;&gt;<\/span> shift<span class=\"token punctuation\">)<\/span><br \/>\n                newcap <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">int<\/span><span class=\"token punctuation\">(<\/span>capmem <span class=\"token operator\">&gt;&gt;<\/span> shift<span class=\"token punctuation\">)<\/span><br \/>\n                capmem <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">uintptr<\/span><span class=\"token punctuation\">(<\/span>newcap<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&lt;&lt;<\/span> shift<br \/>\n        <span class=\"token keyword\">default<\/span><span class=\"token punctuation\">:<\/span><br \/>\n                lenmem <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">uintptr<\/span><span class=\"token punctuation\">(<\/span>oldLen<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">*<\/span> et<span class=\"token punctuation\">.<\/span>Size_<br \/>\n                newlenmem <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">uintptr<\/span><span class=\"token punctuation\">(<\/span>newLen<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">*<\/span> et<span class=\"token punctuation\">.<\/span>Size_<br \/>\n                capmem<span class=\"token punctuation\">,<\/span> overflow <span class=\"token operator\">&#061;<\/span> math<span class=\"token punctuation\">.<\/span><span class=\"token function\">MulUintptr<\/span><span class=\"token punctuation\">(<\/span>et<span class=\"token punctuation\">.<\/span>Size_<span class=\"token punctuation\">,<\/span> <span class=\"token function\">uintptr<\/span><span class=\"token punctuation\">(<\/span>newcap<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n                capmem <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">roundupsize<\/span><span class=\"token punctuation\">(<\/span>capmem<span class=\"token punctuation\">,<\/span> noscan<span class=\"token punctuation\">)<\/span><br \/>\n                newcap <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">int<\/span><span class=\"token punctuation\">(<\/span>capmem <span class=\"token operator\">\/<\/span> et<span class=\"token punctuation\">.<\/span>Size_<span class=\"token punctuation\">)<\/span><br \/>\n                capmem <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">uintptr<\/span><span class=\"token punctuation\">(<\/span>newcap<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">*<\/span> et<span class=\"token punctuation\">.<\/span>Size_<br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n  <span class=\"token comment\">\/\/ &#8230; \u51fd\u6570\u975e\u6838\u5fc3\u90e8\u5206\u7701\u7565<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u6838\u5fc3\u4ee3\u7801<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token comment\">\/\/ nextslicecap \u8ba1\u7b97\u4e0b\u4e00\u4e2a\u5408\u9002\u7684\u5207\u7247\u5bb9\u91cf\u3002<\/span><br \/>\n<span class=\"token comment\">\/\/ \u8be5\u51fd\u6570\u7528\u4e8e\u5728\u5207\u7247\u9700\u8981\u6269\u5bb9\u65f6&#xff0c;\u786e\u5b9a\u65b0\u7684\u5bb9\u91cf\u5927\u5c0f\u3002<\/span><br \/>\n<span class=\"token comment\">\/\/  newLen: \u5207\u7247\u7684\u65b0\u957f\u5ea6&#xff08;\u6240\u9700\u5bb9\u91cf&#xff09;\u3002<\/span><br \/>\n<span class=\"token comment\">\/\/  oldCap: \u5207\u7247\u7684\u65e7\u5bb9\u91cf\u3002<\/span><br \/>\n<span class=\"token comment\">\/\/ \u8fd4\u56de\u503c: \u65b0\u7684\u5207\u7247\u5bb9\u91cf\u3002<\/span><br \/>\n<span class=\"token keyword\">func<\/span> <span class=\"token function\">nextslicecap<\/span><span class=\"token punctuation\">(<\/span>newLen<span class=\"token punctuation\">,<\/span> oldCap <span class=\"token builtin\">int<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token builtin\">int<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        newcap <span class=\"token operator\">:&#061;<\/span> oldCap <span class=\"token comment\">\/\/ \u5c06\u65b0\u7684\u5bb9\u91cf\u521d\u59cb\u5316\u4e3a\u65e7\u7684\u5bb9\u91cf<\/span><\/p>\n<p>        doublecap <span class=\"token operator\">:&#061;<\/span> newcap <span class=\"token operator\">&#043;<\/span> newcap <span class=\"token comment\">\/\/ \u8ba1\u7b97\u65e7\u5bb9\u91cf\u7684\u4e24\u500d<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u5982\u679c\u65b0\u957f\u5ea6\u5927\u4e8e\u65e7\u5bb9\u91cf\u7684\u4e24\u500d&#xff0c;\u5219\u76f4\u63a5\u4f7f\u7528\u65b0\u957f\u5ea6\u4f5c\u4e3a\u65b0\u5bb9\u91cf<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u8fd9\u662f\u4e3a\u4e86\u907f\u514d\u9891\u7e41\u7684\u6269\u5bb9\u64cd\u4f5c&#xff0c;\u5f53\u6240\u9700\u957f\u5ea6\u8fdc\u5927\u4e8e\u5f53\u524d\u5bb9\u91cf\u65f6&#xff0c;\u76f4\u63a5\u5206\u914d\u6240\u9700\u7684\u7a7a\u95f4<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> newLen <span class=\"token operator\">&gt;<\/span> doublecap <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token keyword\">return<\/span> newLen<br \/>\n        <span class=\"token punctuation\">}<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u8bbe\u7f6e\u4e00\u4e2a\u9608\u503c&#xff0c;\u7528\u4e8e\u533a\u5206\u5c0f\u5207\u7247\u548c\u5927\u5207\u7247<\/span><br \/>\n        <span class=\"token keyword\">const<\/span> threshold <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">256<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u5bf9\u4e8e\u5bb9\u91cf\u5c0f\u4e8e\u9608\u503c\u7684\u5c0f\u5207\u7247&#xff0c;\u65b0\u5bb9\u91cf\u76f4\u63a5\u8bbe\u7f6e\u4e3a\u65e7\u5bb9\u91cf\u7684\u4e24\u500d<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u8fd9\u662f\u56e0\u4e3a\u5c0f\u5207\u7247\u7684\u6269\u5bb9\u6210\u672c\u76f8\u5bf9\u8f83\u4f4e<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> oldCap <span class=\"token operator\">&lt;<\/span> threshold <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token keyword\">return<\/span> doublecap<br \/>\n        <span class=\"token punctuation\">}<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u5bf9\u4e8e\u5bb9\u91cf\u5927\u4e8e\u7b49\u4e8e\u9608\u503c\u7684\u5927\u5207\u7247&#xff0c;\u91c7\u7528\u66f4\u4fdd\u5b88\u7684\u6269\u5bb9\u7b56\u7565<\/span><br \/>\n        <span class=\"token keyword\">for<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token comment\">\/\/  \u4ece2\u500d\u589e\u957f&#xff08;\u5c0f\u5207\u7247&#xff09;\u8fc7\u6e21\u52301.25\u500d\u589e\u957f&#xff08;\u5927\u5207\u7247&#xff09;\u3002<\/span><br \/>\n                <span class=\"token comment\">\/\/  \u8be5\u516c\u5f0f\u5728\u4e24\u8005\u4e4b\u95f4\u63d0\u4f9b\u5e73\u6ed1\u7684\u8fc7\u6e21\u3002<\/span><br \/>\n                <span class=\"token comment\">\/\/  (newcap &#043; 3*threshold) &gt;&gt; 2 \u7b49\u4ef7\u4e8e (newcap &#043; 3*threshold) \/ 4<\/span><br \/>\n                <span class=\"token comment\">\/\/  \u8fd9\u4f7f\u5f97\u65b0\u5bb9\u91cf\u7684\u589e\u957f\u6bd4\u4f8b\u57281.25\u52302\u4e4b\u95f4&#xff0c;\u5e76\u968f\u7740\u5207\u7247\u5bb9\u91cf\u7684\u589e\u5927\u800c\u9010\u6e10\u63a5\u8fd11.25<\/span><br \/>\n                newcap <span class=\"token operator\">&#043;&#061;<\/span> <span class=\"token punctuation\">(<\/span>newcap <span class=\"token operator\">&#043;<\/span> <span class=\"token number\">3<\/span><span class=\"token operator\">*<\/span>threshold<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&gt;&gt;<\/span> <span class=\"token number\">2<\/span><\/p>\n<p>                <span class=\"token comment\">\/\/ \u9700\u8981\u68c0\u67e5 &#096;newcap &gt;&#061; newLen&#096; \u4ee5\u53ca &#096;newcap&#096; \u662f\u5426\u6ea2\u51fa\u3002<\/span><br \/>\n                <span class=\"token comment\">\/\/ newLen \u4fdd\u8bc1\u5927\u4e8e\u96f6&#xff0c;\u56e0\u6b64\u5f53 newcap \u6ea2\u51fa\u65f6&#xff0c;&#096;uint(newcap) &gt; uint(newLen)&#096; \u4e0d\u6210\u7acb\u3002<\/span><br \/>\n                <span class=\"token comment\">\/\/ \u8fd9\u5141\u8bb8\u4f7f\u7528\u76f8\u540c\u7684\u6bd4\u8f83\u6765\u68c0\u67e5\u4e24\u8005\u3002<\/span><br \/>\n                <span class=\"token comment\">\/\/ \u4f7f\u7528uint\u7c7b\u578b\u8fdb\u884c\u6bd4\u8f83\u662f\u4e3a\u4e86\u5904\u7406\u6ea2\u51fa\u60c5\u51b5\u3002\u5982\u679cnewcap\u6ea2\u51fa\u53d8\u6210\u8d1f\u6570&#xff0c;\u8f6c\u6362\u4e3auint\u7c7b\u578b\u540e\u4f1a\u53d8\u6210\u4e00\u4e2a\u5f88\u5927\u7684\u6b63\u6570&#xff0c;\u4ece\u800c\u4f7f\u5f97\u6bd4\u8f83\u4ecd\u7136\u6709\u6548\u3002<\/span><br \/>\n                <span class=\"token keyword\">if<\/span> <span class=\"token function\">uint<\/span><span class=\"token punctuation\">(<\/span>newcap<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&gt;&#061;<\/span> <span class=\"token function\">uint<\/span><span class=\"token punctuation\">(<\/span>newLen<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                        <span class=\"token keyword\">break<\/span> <span class=\"token comment\">\/\/ \u5982\u679c\u65b0\u5bb9\u91cf\u8db3\u591f\u5927&#xff0c;\u5219\u9000\u51fa\u5faa\u73af<\/span><br \/>\n                <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/  \u5f53 newcap \u8ba1\u7b97\u6ea2\u51fa\u65f6&#xff0c;\u5c06 newcap \u8bbe\u7f6e\u4e3a\u8bf7\u6c42\u7684\u5bb9\u91cf\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/  \u5982\u679c newcap \u5c0f\u4e8e\u7b49\u4e8e 0&#xff0c;\u8bf4\u660e\u53d1\u751f\u4e86\u6ea2\u51fa<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> newcap <span class=\"token operator\">&lt;&#061;<\/span> <span class=\"token number\">0<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token keyword\">return<\/span> newLen<br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> newcap <span class=\"token comment\">\/\/ \u8fd4\u56de\u8ba1\u7b97\u51fa\u7684\u65b0\u5bb9\u91cf<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/ roundupsize \u8fd4\u56de mallocgc \u4e3a\u6307\u5b9a\u5927\u5c0f\u5206\u914d\u7684\u5185\u5b58\u5757\u7684\u5927\u5c0f&#xff0c;\u51cf\u53bb\u4efb\u4f55\u7528\u4e8e\u5143\u6570\u636e\u7684\u5185\u8054\u7a7a\u95f4\u3002<\/span><br \/>\n<span class=\"token comment\">\/\/  size: \u8bf7\u6c42\u5206\u914d\u7684\u5185\u5b58\u5927\u5c0f\u3002<\/span><br \/>\n<span class=\"token comment\">\/\/  noscan:  \u5982\u679c\u4e3a true&#xff0c;\u5219\u8868\u793a\u8be5\u5185\u5b58\u5757\u4e0d\u9700\u8981\u5783\u573e\u56de\u6536\u626b\u63cf\u3002<\/span><br \/>\n<span class=\"token comment\">\/\/ \u8fd4\u56de\u503c: mallocgc \u5b9e\u9645\u5206\u914d\u7684\u5185\u5b58\u5757\u5927\u5c0f\u3002<\/span><br \/>\n<span class=\"token keyword\">func<\/span> <span class=\"token function\">roundupsize<\/span><span class=\"token punctuation\">(<\/span>size <span class=\"token builtin\">uintptr<\/span><span class=\"token punctuation\">,<\/span> noscan <span class=\"token builtin\">bool<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">(<\/span>reqSize <span class=\"token builtin\">uintptr<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        reqSize <span class=\"token operator\">&#061;<\/span> size <span class=\"token comment\">\/\/ \u521d\u59cb\u5316\u8bf7\u6c42\u5927\u5c0f<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u5904\u7406\u5c0f\u5bf9\u8c61&#xff08;\u5c0f\u4e8e\u7b49\u4e8e maxSmallSize-mallocHeaderSize&#xff09;<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> reqSize <span class=\"token operator\">&lt;&#061;<\/span> maxSmallSize<span class=\"token operator\">&#8211;<\/span>mallocHeaderSize <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token comment\">\/\/ \u5c0f\u5bf9\u8c61\u3002<\/span><br \/>\n                <span class=\"token comment\">\/\/ \u5982\u679c\u9700\u8981\u5783\u573e\u56de\u6536\u626b\u63cf (noscan \u4e3a false) \u5e76\u4e14\u5927\u5c0f\u5927\u4e8e minSizeForMallocHeader&#xff0c;\u5219\u6dfb\u52a0 mallocHeaderSize \u7528\u4e8e\u5b58\u50a8\u5143\u6570\u636e\u3002<\/span><br \/>\n                <span class=\"token comment\">\/\/ heapBitsInSpan(reqSize) \u7528\u4e8e\u68c0\u67e5\u5bf9\u8c61\u662f\u5426\u8db3\u591f\u5c0f\u5230\u53ef\u4ee5\u5b58\u50a8\u5728\u5806\u7684\u4f4d\u56fe\u4e2d&#xff0c;\u5982\u679c\u53ef\u4ee5&#xff0c;\u5219\u4e0d\u9700\u8981 mallocHeader\u3002<\/span><br \/>\n                <span class=\"token keyword\">if<\/span> <span class=\"token operator\">!<\/span>noscan <span class=\"token operator\">&amp;&amp;<\/span> reqSize <span class=\"token operator\">&gt;<\/span> minSizeForMallocHeader <span class=\"token punctuation\">{<\/span> <span class=\"token comment\">\/\/ !noscan &amp;&amp; !heapBitsInSpan(reqSize)<\/span><br \/>\n                        reqSize <span class=\"token operator\">&#043;&#061;<\/span> mallocHeaderSize<br \/>\n                <span class=\"token punctuation\">}<\/span><\/p>\n<p>                <span class=\"token comment\">\/\/ (reqSize &#8211; size) \u4e3a mallocHeaderSize \u6216 0\u3002\u5982\u679c\u6dfb\u52a0\u4e86 mallocHeaderSize&#xff0c;\u6211\u4eec\u9700\u8981\u4ece\u7ed3\u679c\u4e2d\u51cf\u53bb\u5b83&#xff0c;\u56e0\u4e3a mallocgc \u4f1a\u518d\u6b21\u6dfb\u52a0\u5b83\u3002<\/span><br \/>\n                <span class=\"token comment\">\/\/ \u8fd9\u91cc\u662f\u4e3a\u4e86\u786e\u4fdd\u8fd4\u56de\u7684\u5927\u5c0f\u662f mallocgc \u5b9e\u9645\u5206\u914d\u7684\u5927\u5c0f&#xff0c;\u800c\u4e0d\u662f\u5305\u542b\u4e86\u5934\u90e8\u4e4b\u540e\u7684\u5927\u5c0f\u3002<\/span><\/p>\n<p>                <span class=\"token comment\">\/\/ \u8fdb\u4e00\u6b65\u533a\u5206\u66f4\u5c0f\u7684\u5bf9\u8c61\u548c\u4e2d\u7b49\u5927\u5c0f\u7684\u5bf9\u8c61&#xff0c;\u4f7f\u7528\u4e0d\u540c\u7684\u67e5\u627e\u8868\u8fdb\u884c\u5411\u4e0a\u53d6\u6574<\/span><br \/>\n                <span class=\"token keyword\">if<\/span> reqSize <span class=\"token operator\">&lt;&#061;<\/span> smallSizeMax<span class=\"token operator\">&#8211;<\/span><span class=\"token number\">8<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                        <span class=\"token comment\">\/\/ \u5bf9\u4e8e\u975e\u5e38\u5c0f\u7684\u5bf9\u8c61&#xff0c;\u4f7f\u7528 size_to_class8 \u548c class_to_size \u67e5\u627e\u8868\u8fdb\u884c\u5411\u4e0a\u53d6\u6574&#xff0c;\u4ee5 8 \u5b57\u8282\u4e3a\u7c92\u5ea6\u3002<\/span><br \/>\n                        <span class=\"token comment\">\/\/ divRoundUp(reqSize, smallSizeDiv) \u8ba1\u7b97 reqSize \u5728 smallSizeDiv \u7c92\u5ea6\u4e0b\u7684\u5411\u4e0a\u53d6\u6574\u503c\u3002<\/span><br \/>\n                        <span class=\"token comment\">\/\/ class_to_size[&#8230;] \u83b7\u53d6\u5bf9\u5e94\u5927\u5c0f\u7c7b\u7684\u5b9e\u9645\u5206\u914d\u5927\u5c0f\u3002<\/span><br \/>\n                        <span class=\"token comment\">\/\/ \u6700\u540e\u51cf\u53bb (reqSize &#8211; size) \u79fb\u9664\u4e4b\u524d\u53ef\u80fd\u6dfb\u52a0\u7684 mallocHeaderSize\u3002<\/span><br \/>\n                        <span class=\"token keyword\">return<\/span> <span class=\"token function\">uintptr<\/span><span class=\"token punctuation\">(<\/span>class_to_size<span class=\"token punctuation\">[<\/span>size_to_class8<span class=\"token punctuation\">[<\/span><span class=\"token function\">divRoundUp<\/span><span class=\"token punctuation\">(<\/span>reqSize<span class=\"token punctuation\">,<\/span> smallSizeDiv<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#8211;<\/span> <span class=\"token punctuation\">(<\/span>reqSize <span class=\"token operator\">&#8211;<\/span> size<span class=\"token punctuation\">)<\/span><br \/>\n                <span class=\"token punctuation\">}<\/span><br \/>\n                <span class=\"token comment\">\/\/ \u5bf9\u4e8e\u4e2d\u7b49\u5927\u5c0f\u7684\u5bf9\u8c61&#xff0c;\u4f7f\u7528 size_to_class128 \u548c class_to_size \u67e5\u627e\u8868\u8fdb\u884c\u5411\u4e0a\u53d6\u6574&#xff0c;\u4ee5 128 \u5b57\u8282\u4e3a\u7c92\u5ea6\u3002<\/span><br \/>\n                <span class=\"token keyword\">return<\/span> <span class=\"token function\">uintptr<\/span><span class=\"token punctuation\">(<\/span>class_to_size<span class=\"token punctuation\">[<\/span>size_to_class128<span class=\"token punctuation\">[<\/span><span class=\"token function\">divRoundUp<\/span><span class=\"token punctuation\">(<\/span>reqSize<span class=\"token operator\">&#8211;<\/span>smallSizeMax<span class=\"token punctuation\">,<\/span> largeSizeDiv<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#8211;<\/span> <span class=\"token punctuation\">(<\/span>reqSize <span class=\"token operator\">&#8211;<\/span> size<span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u5904\u7406\u5927\u5bf9\u8c61&#xff08;\u5927\u4e8e maxSmallSize-mallocHeaderSize&#xff09;<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u5927\u5bf9\u8c61\u3002\u5c06 reqSize \u5411\u4e0a\u5bf9\u9f50\u5230\u4e0b\u4e00\u9875\u3002\u68c0\u67e5\u6ea2\u51fa\u3002<\/span><br \/>\n        reqSize <span class=\"token operator\">&#043;&#061;<\/span> pageSize <span class=\"token operator\">&#8211;<\/span> <span class=\"token number\">1<\/span> <span class=\"token comment\">\/\/ \u5c06 reqSize \u589e\u52a0\u5230\u4e0b\u4e00\u9875\u8fb9\u754c\u4e4b\u524d<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u68c0\u67e5\u6ea2\u51fa\u3002\u5982\u679c reqSize \u52a0\u4e0a pageSize &#8211; 1 \u540e\u53cd\u800c\u53d8\u5c0f\u4e86&#xff0c;\u8bf4\u660e\u53d1\u751f\u4e86\u6ea2\u51fa\u3002<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> reqSize <span class=\"token operator\">&lt;<\/span> size <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token keyword\">return<\/span> size <span class=\"token comment\">\/\/ \u8fd4\u56de\u539f\u59cb\u5927\u5c0f&#xff0c;\u907f\u514d\u5206\u914d\u8fc7\u5927\u7684\u5185\u5b58<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u901a\u8fc7\u6309\u4f4d\u4e0e\u8fd0\u7b97\u5c06 reqSize \u5bf9\u9f50\u5230\u4e0b\u4e00\u9875\u8fb9\u754c\u3002<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> reqSize <span class=\"token operator\">&amp;^<\/span> <span class=\"token punctuation\">(<\/span>pageSize <span class=\"token operator\">&#8211;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>\u4e09\u3001\u95ee\u9898<\/h3>\n<p>\u3010\u5f15\u75331\u3011 [3]int \u548c [4]int \u662f\u540c\u4e00\u4e2a\u7c7b\u578b\u5417&#xff1f;<\/p>\n<p>\u4e0d\u662f\u3002\u56e0\u4e3a\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u7c7b\u578b\u7684\u4e00\u90e8\u5206&#xff0c;\u8fd9\u662f\u4e0e slice \u4e0d\u540c\u7684\u4e00\u70b9\u3002<\/p>\n<p>\u3010\u5f15\u75332\u3011 \u4e0b\u9762\u7684\u4ee3\u7801\u8f93\u51fa\u662f\u4ec0\u4e48&#xff1f;<\/p>\n<p>\u8bf4\u660e&#xff1a;\u4f8b\u5b50\u6765\u81ea\u96e8\u75d5\u5927\u4f6c\u300aGo\u5b66\u4e60\u7b14\u8bb0\u300b\u7b2c\u56db\u7248&#xff0c;P43\u9875\u3002\u8fd9\u91cc\u6211\u4f1a\u8fdb\u884c\u6269\u5c55&#xff0c;\u5e76\u4f1a\u4f5c\u56fe\u8be6\u7ec6\u5206\u6790\u3002<\/p>\n<p>package main<\/p>\n<p>import &#034;fmt&#034;<\/p>\n<p>func main() {<br \/>\nslice :&#061; []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}<br \/>\ns1 :&#061; slice[2:5]<br \/>\ns2 :&#061; s1[2:6:7]<\/p>\n<p>s2 &#061; append(s2, 100)<br \/>\ns2 &#061; append(s2, 200)<\/p>\n<p>s1[2] &#061; 20<\/p>\n<p>fmt.Println(s1)<br \/>\nfmt.Println(s2)<br \/>\nfmt.Println(slice)<br \/>\n}<\/p>\n<p>\u7ed3\u679c&#xff1a;<\/p>\n<p>[2 3 20]<br \/>\n[4 5 6 7 100 200]<br \/>\n[0 1 2 3 20 5 6 7 100 9]<\/p>\n<p>s1 \u4ece slice \u7d22\u5f152&#xff08;\u95ed\u533a\u95f4&#xff09;\u5230\u7d22\u5f155&#xff08;\u5f00\u533a\u95f4&#xff0c;\u5143\u7d20\u771f\u6b63\u53d6\u5230\u7d22\u5f154&#xff09;&#xff0c;\u957f\u5ea6\u4e3a3&#xff0c;\u5bb9\u91cf\u9ed8\u8ba4\u5230\u6570\u7ec4\u7ed3\u5c3e&#xff0c;\u4e3a8\u3002 s2 \u4ece s1 \u7684\u7d22\u5f152&#xff08;\u95ed\u533a\u95f4&#xff09;\u5230\u7d22\u5f156&#xff08;\u5f00\u533a\u95f4&#xff0c;\u5143\u7d20\u771f\u6b63\u53d6\u5230\u7d22\u5f155&#xff09;&#xff0c;\u5bb9\u91cf\u5230\u7d22\u5f157&#xff08;\u5f00\u533a\u95f4&#xff0c;\u771f\u6b63\u5230\u7d22\u5f156&#xff09;&#xff0c;\u4e3a5\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014937-682fd4317ebf8.png\" alt=\"slice origin\" \/><\/p>\n<p>\u63a5\u7740&#xff0c;\u5411 s2 \u5c3e\u90e8\u8ffd\u52a0\u4e00\u4e2a\u5143\u7d20 100&#xff1a;<\/p>\n<p>s2 <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">append<\/span><span class=\"token punctuation\">(<\/span>s2<span class=\"token punctuation\">,<\/span> <span class=\"token number\">100<\/span><span class=\"token punctuation\">)<\/span> <\/p>\n<p>s2 \u5bb9\u91cf\u521a\u597d\u591f&#xff0c;\u76f4\u63a5\u8ffd\u52a0\u3002\u4e0d\u8fc7&#xff0c;\u8fd9\u4f1a\u4fee\u6539\u539f\u59cb\u6570\u7ec4\u5bf9\u5e94\u4f4d\u7f6e\u7684\u5143\u7d20\u3002\u8fd9\u4e00\u6539\u52a8&#xff0c;\u6570\u7ec4\u548c s1 \u90fd\u53ef\u4ee5\u770b\u5f97\u5230\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014937-682fd431ee61b.png\" alt=\"append 100\" \/><\/p>\n<p>\u518d\u6b21\u5411 s2 \u8ffd\u52a0\u5143\u7d20200&#xff1a;<\/p>\n<p>s2 <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">append<\/span><span class=\"token punctuation\">(<\/span>s2<span class=\"token punctuation\">,<\/span> <span class=\"token number\">200<\/span><span class=\"token punctuation\">)<\/span> <\/p>\n<p>\u8fd9\u65f6&#xff0c;s2 \u7684\u5bb9\u91cf\u4e0d\u591f\u7528&#xff0c;\u8be5\u6269\u5bb9\u4e86\u3002\u4e8e\u662f&#xff0c;s2 \u53e6\u8d77\u7089\u7076&#xff0c;\u5c06\u539f\u6765\u7684\u5143\u7d20\u590d\u5236\u65b0\u7684\u4f4d\u7f6e&#xff0c;\u6269\u5927\u81ea\u5df1\u7684\u5bb9\u91cf\u3002\u5e76\u4e14\u4e3a\u4e86\u5e94\u5bf9\u672a\u6765\u53ef\u80fd\u7684 append \u5e26\u6765\u7684\u518d\u4e00\u6b21\u6269\u5bb9&#xff0c;s2 \u4f1a\u5728\u6b64\u6b21\u6269\u5bb9\u7684\u65f6\u5019\u591a\u7559\u4e00\u4e9b buffer&#xff0c;\u5c06\u65b0\u7684\u5bb9\u91cf\u5c06\u6269\u5927\u4e3a\u539f\u59cb\u5bb9\u91cf\u76842\u500d&#xff0c;\u4e5f\u5c31\u662f10\u4e86\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014938-682fd43269223.png\" alt=\"append 200\" \/><\/p>\n<p>\u6700\u540e&#xff0c;\u4fee\u6539 s1 \u7d22\u5f15\u4e3a2\u4f4d\u7f6e\u7684\u5143\u7d20&#xff1a;<\/p>\n<p>s1<span class=\"token punctuation\">[<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">20<\/span><\/p>\n<p>\u8fd9\u6b21\u53ea\u4f1a\u5f71\u54cd\u539f\u59cb\u6570\u7ec4\u76f8\u5e94\u4f4d\u7f6e\u7684\u5143\u7d20\u3002\u5b83\u5f71\u54cd\u4e0d\u5230 s2 \u4e86&#xff0c;\u4eba\u5bb6\u5df2\u7ecf\u8fdc\u8d70\u9ad8\u98de\u4e86\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014938-682fd432db66a.png\" alt=\"s1[2]&#061;20\" \/><\/p>\n<p>\u518d\u63d0\u4e00\u70b9&#xff0c;\u6253\u5370 s1 \u7684\u65f6\u5019&#xff0c;\u53ea\u4f1a\u6253\u5370\u51fa s1 \u957f\u5ea6\u4ee5\u5185\u7684\u5143\u7d20\u3002\u6240\u4ee5&#xff0c;\u53ea\u4f1a\u6253\u5370\u51fa3\u4e2a\u5143\u7d20&#xff0c;\u867d\u7136\u5b83\u7684\u5e95\u5c42\u6570\u7ec4\u4e0d\u6b623\u4e2a\u5143\u7d20\u3002<\/p>\n<h3>\u53c2\u8003\u94fe\u63a5<\/h3>\n<p>1.Go \u7a0b\u5e8f\u5458\u9762\u8bd5\u7b14\u8bd5\u5b9d\u5178<\/p>\n<p>2.\u300aGo\u5b66\u4e60\u7b14\u8bb0\u300b<\/p>\n<p>3.golangSlice\u7684\u6269\u5bb9\u89c4\u5219<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e15\u6b21\uff0c\u6536\u85cf27\u6b21\u3002slice \u7684\u5e95\u5c42\u6570\u636e\u662f\u6570\u7ec4\uff0cslice \u662f\u5bf9\u6570\u7ec4\u7684\u5c01\u88c5\uff0c\u5b83\u63cf\u8ff0\u4e00\u4e2a\u6570\u7ec4\u7684\u7247\u6bb5\u3002\u4e24\u8005\u90fd\u53ef\u4ee5\u901a\u8fc7\u4e0b\u6807\u6765\u8bbf\u95ee\u5355\u4e2a\u5143\u7d20\u3002\u6570\u7ec4\u662f\u5b9a\u957f\u7684\uff0c\u957f\u5ea6\u5b9a\u4e49\u597d\u4e4b\u540e\uff0c\u4e0d\u80fd\u518d\u66f4\u6539\u3002\u5728 Go \u4e2d\uff0c\u6570\u7ec4\u662f\u4e0d\u5e38\u89c1\u7684\uff0c\u56e0\u4e3a\u5176\u957f\u5ea6\u662f\u7c7b\u578b\u7684\u4e00\u90e8\u5206\uff0c\u9650\u5236\u4e86\u5b83\u7684\u8868\u8fbe\u80fd\u529b\uff0c\u6bd4\u5982 [3]int \u548c [4]int \u5c31\u662f\u4e0d\u540c\u7684\u7c7b\u578b\u3002\u800c\u5207\u7247\u5219\u975e\u5e38\u7075\u6d3b\uff0c\u5b83\u53ef\u4ee5\u52a8\u6001\u5730\u6269\u5bb9\u3002\u5207\u7247\u7684\u7c7b\u578b\u548c\u957f\u5ea6\u65e0\u5173\u3002\u6570\u7ec4\u5c31\u662f\u4e00\u7247\u8fde\u7eed\u7684\u5185\u5b58\uff0c slice \u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53\uff0c\u5305\u542b\u4e09\u4e2a\u5b57\u6bb5\uff1a\u957f\u5ea6\u3001\u5bb9\u91cf\u3001\u5e95\u5c42\u6570\u7ec4\u3002<\/p>\n","protected":false},"author":2,"featured_media":39339,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[746,81,292,190],"topic":[],"class_list":["post-39344","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-golang","tag-python","tag-292","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>02.Golang \u5207\u7247\uff08slice\uff09\u6e90\u7801\u5206\u6790\uff08\u4e00\u3001\u5b9a\u4e49\u4e0e\u57fa\u7840\u64cd\u4f5c\u5b9e\u73b0\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/39344.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"02.Golang \u5207\u7247\uff08slice\uff09\u6e90\u7801\u5206\u6790\uff08\u4e00\u3001\u5b9a\u4e49\u4e0e\u57fa\u7840\u64cd\u4f5c\u5b9e\u73b0\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e15\u6b21\uff0c\u6536\u85cf27\u6b21\u3002slice \u7684\u5e95\u5c42\u6570\u636e\u662f\u6570\u7ec4\uff0cslice \u662f\u5bf9\u6570\u7ec4\u7684\u5c01\u88c5\uff0c\u5b83\u63cf\u8ff0\u4e00\u4e2a\u6570\u7ec4\u7684\u7247\u6bb5\u3002\u4e24\u8005\u90fd\u53ef\u4ee5\u901a\u8fc7\u4e0b\u6807\u6765\u8bbf\u95ee\u5355\u4e2a\u5143\u7d20\u3002\u6570\u7ec4\u662f\u5b9a\u957f\u7684\uff0c\u957f\u5ea6\u5b9a\u4e49\u597d\u4e4b\u540e\uff0c\u4e0d\u80fd\u518d\u66f4\u6539\u3002\u5728 Go \u4e2d\uff0c\u6570\u7ec4\u662f\u4e0d\u5e38\u89c1\u7684\uff0c\u56e0\u4e3a\u5176\u957f\u5ea6\u662f\u7c7b\u578b\u7684\u4e00\u90e8\u5206\uff0c\u9650\u5236\u4e86\u5b83\u7684\u8868\u8fbe\u80fd\u529b\uff0c\u6bd4\u5982 [3]int \u548c [4]int \u5c31\u662f\u4e0d\u540c\u7684\u7c7b\u578b\u3002\u800c\u5207\u7247\u5219\u975e\u5e38\u7075\u6d3b\uff0c\u5b83\u53ef\u4ee5\u52a8\u6001\u5730\u6269\u5bb9\u3002\u5207\u7247\u7684\u7c7b\u578b\u548c\u957f\u5ea6\u65e0\u5173\u3002\u6570\u7ec4\u5c31\u662f\u4e00\u7247\u8fde\u7eed\u7684\u5185\u5b58\uff0c slice \u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53\uff0c\u5305\u542b\u4e09\u4e2a\u5b57\u6bb5\uff1a\u957f\u5ea6\u3001\u5bb9\u91cf\u3001\u5e95\u5c42\u6570\u7ec4\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/39344.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-23T01:49:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014937-682fd43166f24.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/39344.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/39344.html\",\"name\":\"02.Golang \u5207\u7247\uff08slice\uff09\u6e90\u7801\u5206\u6790\uff08\u4e00\u3001\u5b9a\u4e49\u4e0e\u57fa\u7840\u64cd\u4f5c\u5b9e\u73b0\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-23T01:49:40+00:00\",\"dateModified\":\"2025-05-23T01:49:40+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/39344.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/39344.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/39344.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"02.Golang \u5207\u7247\uff08slice\uff09\u6e90\u7801\u5206\u6790\uff08\u4e00\u3001\u5b9a\u4e49\u4e0e\u57fa\u7840\u64cd\u4f5c\u5b9e\u73b0\uff09\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"02.Golang \u5207\u7247\uff08slice\uff09\u6e90\u7801\u5206\u6790\uff08\u4e00\u3001\u5b9a\u4e49\u4e0e\u57fa\u7840\u64cd\u4f5c\u5b9e\u73b0\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/39344.html","og_locale":"zh_CN","og_type":"article","og_title":"02.Golang \u5207\u7247\uff08slice\uff09\u6e90\u7801\u5206\u6790\uff08\u4e00\u3001\u5b9a\u4e49\u4e0e\u57fa\u7840\u64cd\u4f5c\u5b9e\u73b0\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e15\u6b21\uff0c\u6536\u85cf27\u6b21\u3002slice \u7684\u5e95\u5c42\u6570\u636e\u662f\u6570\u7ec4\uff0cslice \u662f\u5bf9\u6570\u7ec4\u7684\u5c01\u88c5\uff0c\u5b83\u63cf\u8ff0\u4e00\u4e2a\u6570\u7ec4\u7684\u7247\u6bb5\u3002\u4e24\u8005\u90fd\u53ef\u4ee5\u901a\u8fc7\u4e0b\u6807\u6765\u8bbf\u95ee\u5355\u4e2a\u5143\u7d20\u3002\u6570\u7ec4\u662f\u5b9a\u957f\u7684\uff0c\u957f\u5ea6\u5b9a\u4e49\u597d\u4e4b\u540e\uff0c\u4e0d\u80fd\u518d\u66f4\u6539\u3002\u5728 Go \u4e2d\uff0c\u6570\u7ec4\u662f\u4e0d\u5e38\u89c1\u7684\uff0c\u56e0\u4e3a\u5176\u957f\u5ea6\u662f\u7c7b\u578b\u7684\u4e00\u90e8\u5206\uff0c\u9650\u5236\u4e86\u5b83\u7684\u8868\u8fbe\u80fd\u529b\uff0c\u6bd4\u5982 [3]int \u548c [4]int \u5c31\u662f\u4e0d\u540c\u7684\u7c7b\u578b\u3002\u800c\u5207\u7247\u5219\u975e\u5e38\u7075\u6d3b\uff0c\u5b83\u53ef\u4ee5\u52a8\u6001\u5730\u6269\u5bb9\u3002\u5207\u7247\u7684\u7c7b\u578b\u548c\u957f\u5ea6\u65e0\u5173\u3002\u6570\u7ec4\u5c31\u662f\u4e00\u7247\u8fde\u7eed\u7684\u5185\u5b58\uff0c slice \u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53\uff0c\u5305\u542b\u4e09\u4e2a\u5b57\u6bb5\uff1a\u957f\u5ea6\u3001\u5bb9\u91cf\u3001\u5e95\u5c42\u6570\u7ec4\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/39344.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-23T01:49:40+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014937-682fd43166f24.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/39344.html","url":"https:\/\/www.wsisp.com\/helps\/39344.html","name":"02.Golang \u5207\u7247\uff08slice\uff09\u6e90\u7801\u5206\u6790\uff08\u4e00\u3001\u5b9a\u4e49\u4e0e\u57fa\u7840\u64cd\u4f5c\u5b9e\u73b0\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-23T01:49:40+00:00","dateModified":"2025-05-23T01:49:40+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/39344.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/39344.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/39344.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"02.Golang \u5207\u7247\uff08slice\uff09\u6e90\u7801\u5206\u6790\uff08\u4e00\u3001\u5b9a\u4e49\u4e0e\u57fa\u7840\u64cd\u4f5c\u5b9e\u73b0\uff09"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/39344","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=39344"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/39344\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/39339"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=39344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=39344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=39344"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=39344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}