数组和切片有什么异同
// runtime/slice.go
type slice struct {
array unsafe.Pointer // 元素指针
len int // 长度
cap int // 容量
}




Last updated
Was this helpful?
// runtime/slice.go
type slice struct {
array unsafe.Pointer // 元素指针
len int // 长度
cap int // 容量
}




Last updated
Was this helpful?
Was this helpful?
package main
import "fmt"
func main() {
slice := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
s1 := slice[2:5]
s2 := s1[2:6:7]
s2 = append(s2, 100)
s2 = append(s2, 200)
s1[2] = 20
fmt.Println(s1)
fmt.Println(s2)
fmt.Println(slice)
}[2 3 20]
[4 5 6 7 100 200]
[0 1 2 3 20 5 6 7 100 9]s2 = append(s2, 100)s2 = append(s2, 100)s1[2] = 20