- 首先golang中确实没有提供引用的编程符号(c++ 中有&,建议可以先简单了解c++的引用)
参考
- 疑问1
c := &a
b := c[0:1]
fmt.Printf("a:%p c:%p b:%p", &a, c, &b)
编译不通过,报错: cannot slice c (type *[]int32)
s := make([]int, 20)
ptr := unsafe.Pointer(&s[0])
fmt.Printf("s:%p s type: %#v &s:%p ptr:%p &ptr:%p &s[0]:%p\n ", s, s, &s, ptr, &ptr, &s[0])
ptr = unsafe.Pointer(ptr)
s2t := ((*[20]int)(ptr))
s2 := s2t[0:10]
fmt.Printf("s2t: %p &s2t: %p s2t type:%#v s2: %p &s2: %p s2 type: %#v\n", s2t, &s2t, s2t, s2, &s2, s2)
编译通过,输出如下:
s:0xc420080000 s type: []int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} &s:0xc42000a060 ptr:0xc420080000 &ptr:0xc42000c028 &s[0]:0xc420080000
s2t: 0xc420080000 &s2t: 0xc42000c038 s2t type:&[20]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} s2: 0xc420080000 &s2: 0xc42000a0c0 s2 type: []int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
大家是不是发现了 s2t type:&[20]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
这个&[20]int.. 不是[20]int,而代码强制转换是用的([20]int)
3、疑问2
s := make([]int, 20)
ptr := unsafe.Pointer(&s[0])
var s1 = struct {
addr uintptr
len int
cap int
}{uintptr(ptr), 0, 10}
s3t := (*[]int)(unsafe.Pointer(&s1))
s3 := *s3t
//s4 := s3t[0:10] //cannot slice s3t (type *[]int)
st := &s
//fmt.Printf("s1:%p s3: %p s3 type: %#v s3t:%p &s3t: %p s3t type:%#v s4:%p st:%#v\n", s1, s3, s3, s3t, &s3t, s3t, s4, st)
fmt.Printf("s1:%p s3: %p s3 type: %#v s3t:%p &s3t: %p s3t type:%#v st:%#v\n", s1, s3, s3, s3t, &s3t, s3t, st)
输出如下:
s1:%!p(struct { addr uintptr; len int; cap int }={842350985216 0 10}) s3: 0xc420080000 s3 type: []int{} s3t:0xc420016180 &s3t: 0xc42000c028 s3t type:&[]int{} st:&[]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
注释的编译不通过,报错cannot slice s3t (type *[]int)
。报错原因:
s2t := ((*[20]int)(ptr)) --- s3t := (*[]int)(unsafe.Pointer(&s1))
强转类型不一样,一个是20个元素的slice指针, 一个是slice指针, 所以就会出现报错。
type slice struct {
array unsafe.Pointer
len int
cap int
}
slice的强转操作:第1种:将强转指针赋值到array字段了, 参数20作为len和cap进行设置。
第2种:可以理解为内存空间赋值(例如c语言(64位机器): memcpy(s3t, &s1, 24))
注: 本人能力有限,如有不正之处,欢迎指出