unsafe.Pointer称为通用指针,官方文档对该类型有四个重要描述:
(1)任何类型的指针都可以被转化为Pointer
(2)Pointer可以被转化为任何类型的指针
(3)uintptr可以被转化为Pointer
(4)Pointer可以被转化为uintptr
-
package main
-
-
import (
-
"fmt"
-
"unsafe"
-
)
-
-
func main(){
-
-
a := [4]int{1,2,3,4}
-
p1 := unsafe.Pointer(&a)
-
p2 := uintptr(p1)
-
p3 := p2 + 1 * unsafe.Sizeof(p1)
-
p4 := (*int)(unsafe.Pointer(p3))
-
*p4 = 8
-
fmt.Printf("%p %p %d %d %v \n",&a,p1,p2,unsafe.Sizeof(p1),a)
-
}
输出:
0xc000060140 0xc000060140 824634114368 8 [1 8 3 4]
阅读(1536) | 评论(0) | 转发(0) |