企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 切片 slice 并不是数组或数组指针。它通过内部指针和相关属性引用数组片段,以实现变长方案 ``` <pre class="calibre13">``` <span class="token5">1.</span> 切片:切片是数组的一个引用,因此切片是引用类型。但自身是结构体,值拷贝传递。 <span class="token5">2.</span> 切片的长度可以改变,因此,切片是一个可变的数组。 <span class="token5">3.</span> <span class="token3">切片遍历方式和数组一样,可以用len</span><span class="token1">(</span><span class="token1">)</span>求长度。表示可用元素数量,读写操作不能超过该限制。 <span class="token5">4.</span> cap可以求出slice最大扩张容量,不能超出数组限制。<span class="token5">0</span> <span class="token"><=</span> <span class="token3">len</span><span class="token1">(</span>slice<span class="token1">)</span> <span class="token"><=</span> <span class="token3">len</span><span class="token1">(</span>array<span class="token1">)</span>,其中array是slice引用的数组。 <span class="token5">5.</span> 切片的定义:<span class="token2">var</span> 变量名 <span class="token1">[</span><span class="token1">]</span>类型,比如 <span class="token2">var</span> str <span class="token1">[</span><span class="token1">]</span>string <span class="token2">var</span> arr <span class="token1">[</span><span class="token1">]</span>int。 <span class="token5">6.</span> 如果 slice <span class="token">==</span> nil,那么 len、cap 结果都等于 <span class="token5">0</span>。 ``` ``` ## 数组和切片用法不同的地方 ### 初始化 ``` <pre class="calibre13">``` 数组:是同一种数据类型的固定长度的序列 数组定义:<span class="token2">var</span> a <span class="token1">[</span>len<span class="token1">]</span>int ``` ``` ``` <pre class="calibre13">``` 声明切片: <span class="token2">var</span> s1 <span class="token1">[</span><span class="token1">]</span>int s2 <span class="token1">:</span><span class="token">=</span> <span class="token1">[</span><span class="token1">]</span>int<span class="token1">{</span><span class="token1">}</span> <span class="token2">var</span> s3 <span class="token1">[</span><span class="token1">]</span>int <span class="token">=</span> <span class="token3">make</span><span class="token1">(</span><span class="token1">[</span><span class="token1">]</span>int<span class="token1">,</span> <span class="token5">0</span><span class="token1">)</span> <span class="token2">var</span> s4 <span class="token1">[</span><span class="token1">]</span>int <span class="token">=</span> <span class="token3">make</span><span class="token1">(</span><span class="token1">[</span><span class="token1">]</span>int<span class="token1">,</span> <span class="token5">0</span><span class="token1">,</span> <span class="token5">0</span><span class="token1">)</span> ``` ``` ``` <pre class="calibre14">``` <span class="token6">// 5.从数组切片</span> arr <span class="token1">:</span><span class="token">=</span> <span class="token1">[</span><span class="token5">5</span><span class="token1">]</span>int<span class="token1">{</span><span class="token5">1</span><span class="token1">,</span> <span class="token5">2</span><span class="token1">,</span> <span class="token5">3</span><span class="token1">,</span> <span class="token5">4</span><span class="token1">,</span> <span class="token5">5</span><span class="token1">}</span> <span class="token2">var</span> s6 <span class="token1">[</span><span class="token1">]</span>int <span class="token6">// 前包后不包</span> s6 <span class="token">=</span> arr<span class="token1">[</span><span class="token5">1</span><span class="token1">:</span><span class="token5">4</span><span class="token1">]</span> ``` ```