1,已知:切片的本质是对底层数组的封装,它包含三个信息(底层数组的指针,切片长度和切片容量)
问题:底层数组的指针指的是什么?是我们用“*”定义的那个指针吗?
2,如下代码所示
定义切片 a,定义,然后定义切片s,让切片s越界,为什么没有自动将切片a的容量加长一倍?
'''
package main
import "fmt"
func main() {
var a = []int{1, 2, 3, 4, 5, 6}
var s = a[2:6]
fmt.Printf("this%d\n", a)
fmt.Printf("this%d\n", s)
fmt.Printf("this%d\n", t)
fmt.Printf("a长度%d,a容量%d\n", len(a), cap(a))
fmt.Printf("s长度%d,s容量%d\n", len(s), cap(s))
}
'''
3.然后在在改一下,修改切片是,避切片s越界,定义切片t,设置切片t越界s,这时切片t会将切片a视为底层数组,不报错t越界s,为什么?
'''
package main
import "fmt"
func main() {
var a = []int{1, 2, 3, 4, 5, 6, 7}
var s = a[2:5]
var t = s[2:5]
fmt.Printf("this%d\n", a)
fmt.Printf("this%d\n", s)
fmt.Printf("this%d\n", t)
fmt.Printf("a长度%d,a容量%d\n", len(a), cap(a))
fmt.Printf("s长度%d,s容量%d\n", len(s), cap(s))
fmt.Printf("t长度%d,t容量%d\n", len(t), cap(t))
}