package main
import (
"fmt"
)
func main() {
s1 := []int{1, 2, 3}
fmt.Printf("&s1=%p\n", &s1)
fmt.Printf("&s1[0]=%p\n", &s1[0])
s2 := s1[:2]
s3 := s1
fmt.Println("s2:=S1[:2]")
fmt.Printf("&s2=%p\n", &s2)
fmt.Printf("&s3=%p\n", &s3)
fmt.Printf("&s2[0]=%p\n", &s2[0])
fmt.Printf("&s3[0]=%p\n", &s3[0])
fmt.Println("###typ:")
fmt.Printf("s1 type is:%T\n", s1)
fmt.Printf("s1[0] type is %T\n", s1[0])
fmt.Printf("s2 type is:%T\n", s2)
fmt.Printf("s2[0] type is %T\n", s2[0])
}
#########
&s1=0xc42000a060
&s1[0]=0xc420012180
s2:=S1[:2]
&s2=0xc42000a080
&s3=0xc42000a0a0
&s2[0]=0xc420012180
&s3[0]=0xc420012180
###typ:
s1 type is:[]int
s1[0] type is int
s2 type is:[]int
s2[0] type is int
###
&s1 是 slice的地址 &s1[0] 才是slice中元素的地址。即 slice定义中的指针 array 。
type slice struct {
array unsafe.Pointer
len int
cap int
}