数组和切片
数组类型的值的长度是固定的
而切片类型的值是可变长的
做个不算特别正确的对比 可以理解为python当中的元组和列表
切片类型属于引用类型,同属引用类型的还有字典类型、通道类型、函数类型等;
数组类型属于值类型,同属值类型的还有基础数据类型以及结构体类型
如果传递的值是引用类型,就是“传引用”
内建函数 len 取长度
内建函数 cap 取容量
数组的长度永远等于长度,切片的容量却不是
扩容 在1024之后 调整的倍率从2下调到1.25
切片的数组永远不会被替换
几个例子
1 | package main |
输出是
The length of s1: 5
The capacity of s1: 5
The value of s1: [0 0 0 0 0]
The length of s2: 5
The capacity of s2: 8
The value of s2: [0 0 0 0 0]
切片的扩容
生成新的切片 会做拷贝
1 |
|
输出如下:
The capacity of s6: 0
s6(1): len: 1, cap: 1
s6(2): len: 2, cap: 2
s6(3): len: 3, cap: 4
s6(4): len: 4, cap: 4
s6(5): len: 5, cap: 8
The capacity of s7: 1024
s7e1: len: 1224, cap: 1280
s7e2: len: 1424, cap: 1696 // 为啥不是1600
s7e3: len: 1624, cap: 2048 //为啥不是2000
The capacity of s8: 10
s8a: len: 21, cap: 22
s8b: len: 44, cap: 44
s8c: len: 89, cap: 96
package main
import "fmt"
func main() {
// 示例1。
a1 := [7]int{1, 2, 3, 4, 5, 6, 7}
fmt.Printf("a1: %v (len: %d, cap: %d)\n",
a1, len(a1), cap(a1))
s9 := a1[1:4]
//s9[0] = 1
fmt.Printf("s9: %v (len: %d, cap: %d)\n",
s9, len(s9), cap(s9))
for i := 1; i <= 5; i++ {
s9 = append(s9, i)
fmt.Printf("s9(%d): %v (len: %d, cap: %d)\n",
i, s9, len(s9), cap(s9))
}
fmt.Printf("a1: %v (len: %d, cap: %d)\n",
a1, len(a1), cap(a1))
fmt.Println()
}
输入如下所示
a1: [1 2 3 4 5 6 7] (len: 7, cap: 7)
s9: [2 3 4] (len: 3, cap: 6)
s9(1): [2 3 4 1] (len: 4, cap: 6)
s9(2): [2 3 4 1 2] (len: 5, cap: 6)
s9(3): [2 3 4 1 2 3] (len: 6, cap: 6)
s9(4): [2 3 4 1 2 3 4] (len: 7, cap: 12)
s9(5): [2 3 4 1 2 3 4 5] (len: 8, cap: 12)
a1: [1 2 3 4 1 2 3] (len: 7, cap: 7)
数组和切片的几个不同点???