![image.png](https://static.studygolang.com/181214/80d0d9079d253a93f15541f4dab0f168.png)
slice底层是指针,array是固定数组,某些场合需要动态数组
slice组成ptr
len
cap
package main
import (
"fmt"
)
func main() {
aSlice := []int{1, 2, 3, 4, 5}
fmt.Printf("aSlice length = %d, cap = %d, self = %v\n", len(aSlice), cap(aSlice), aSlice)
aSlice = append(aSlice, 6)
fmt.Printf("aSlice length= %d, cap = %d, self = %v\n", len(aSlice), cap(aSlice), aSlice)
aSlice = append(aSlice, 7)
fmt.Printf("aSlice length= %d, cap = %d, self = %v\n", len(aSlice), cap(aSlice), aSlice)
aSlice = append(aSlice, 7)
fmt.Printf("aSlice length= %d, cap = %d, self = %v\n", len(aSlice), cap(aSlice), aSlice)
aSlice = append(aSlice, 7)
fmt.Printf("aSlice length= %d, cap = %d, self = %v\n", len(aSlice), cap(aSlice), aSlice)
aSlice = append(aSlice, 7)
fmt.Printf("aSlice length= %d, cap = %d, self = %v\n", len(aSlice), cap(aSlice), aSlice)
aSlice = append(aSlice, 8)
fmt.Printf("aSlice length= %d, cap = %d, self = %v\n", len(aSlice), cap(aSlice), aSlice)
fmt.Printf("aSlice length= %d, cap = %d, self = %v\n", len(aSlice), cap(aSlice), aSlice)
fmt.Printf("aSlice length= %d, cap = %d, self = %v\n", len(aSlice), cap(aSlice), aSlice)
}
![image.png](https://static.studygolang.com/181214/659e265d8b74c5871e0af4e81b48d2c0.png)
超过len值会动态去扩容
没有用到Markdown语法
好人一生平安!
下次!
有疑问加站长微信联系(非本文作者)