package mainimport ("fmt""unsafe"
)func main() {
//指针//符号: 取址& 取值*//go指针只能读,不能修改指针的值addr := "沙河"addrP := &addrfmt.Printf("类型:%T,变量值:%v,指向的值:%v \n", addrP, addrP, *addrP)//类型:*string,变量值:0xc00004e230,指向的值:沙河fmt.Println("=============")//修改指针的值:var arr = []int32{
11, 22, 33}fmt.Println("1.获得arr[0]的指针:")var ptr = &arr[0]fmt.Printf("&arr[0]的类型是:%T,值是:%v\n", ptr, ptr) //*int32 0xc0000140d0fmt.Println("2.将*int32指针转换成unsafe.Pointer类型的指针:")ptr_first_unsafe_point := unsafe.Pointer(ptr)fmt.Printf("ptr_first_unsafe_point的类型是:%T,值是:%v\n", ptr_first_unsafe_point, ptr_first_unsafe_point) //unsafe.Pointer 0xc0000140d0fmt.Println("3.将unsafe.Pointer指针转换成uintptr类型的指针:")ptr_uintptr_first_unsafe_point := uintptr(ptr_first_unsafe_point)fmt.Printf("ptr_uintptr_first_unsafe_point的类型是:%T,值是:%v\n", ptr_uintptr_first_unsafe_point, ptr_uintptr_first_unsafe_point) //uintptr 824633802960fmt.Println("4.将uintptr指针修改,值加4:")//int32 一个数的大小对应 4个字节,所以加4获得数组的第二个元素:22;ptr_uintptr_first_unsafe_point += 4fmt.Printf("ptr_uintptr_first_unsafe_point指针值是%v\n", ptr_uintptr_first_unsafe_point) //824633802964fmt.Println("5.将uintptr指针转换成unsafe.Pointer类型的指针:")ptr_uintptr_second_unsafe_point := unsafe.Pointer(ptr_uintptr_first_unsafe_point)fmt.Printf("ptr_uintptr_second_unsafe_point的类型是:%T,值是:%v\n", ptr_uintptr_second_unsafe_point, ptr_uintptr_second_unsafe_point) //unsafe.Pointer 0xc0000140d4fmt.Println("6.将unsafe.Pointer指针转换成*int32类型的指针:")ptr_after_change := (*int32)(ptr_uintptr_second_unsafe_point)fmt.Printf("ptr_after_change指针值是%v\n", ptr_after_change) //0xc0000140d4fmt.Printf("ptr_after_change指针指向的值是%v\n", *ptr_after_change) //22/* 结果:1.获得arr[0]的指针:&arr[0]的类型是:*int32,值是:0xc0000140d02.将*int32指针转换成unsafe.Pointer类型的指针:ptr_first_unsafe_point的类型是:unsafe.Pointer,值是:0xc0000140d03.将unsafe.Pointer指针转换成uintptr类型的指针:ptr_uintptr_first_unsafe_point的类型是:uintptr,值是:8246338029604.将uintptr指针修改,值加4:ptr_uintptr_first_unsafe_point指针值是8246338029645.将uintptr指针转换成unsafe.Pointer类型的指针:ptr_uintptr_second_unsafe_point的类型是:unsafe.Pointer,值是:0xc0000140d46.将unsafe.Pointer指针转换成*int32类型的指针:ptr_after_change指针值是0xc0000140d4ptr_after_change指针指向的值是22*//*- unsafe.Pointer只是单纯的通用指针类型,用于转换不同类型指针,它不可以参与指针运算;- 而uintptr是用于指针运算的,GC 不把 uintptr 当指针,也就是说 uintptr 无法持有对象, uintptr 类型的目标会被回收;- unsafe.Pointer 可以和 普通指针 进行相互转换;- unsafe.Pointer 可以和 uintptr 进行相互转换。*/
}
详细解决方案
Golang 指针(unsafe.Pointer、uintptr)
热度:22 发布时间:2023-11-26 22:24:20.0
相关解决方案
- uses unchecked or unsafe operations??
- 为什么程序执行后回多出来Null pointer assignment 这句话
- 为什么程序执行后回多出来Null pointer assignment 这句话
- [求助]遇到难题null pointer assignment
- [求助]为什么绘出现Null pointer assignment错误
- 好急好急呀 又是null pointer assignment 的问题
- 好急好急呀 又是null pointer assignment 的问题
- cursor:hand 跟 cursor:pointer
- pointer-events属性实现上面元素可点击
- cursor:hand 与 cursor:pointer 的差别
- 将鼠标的式样变成一个"手"的样子. cursor:pointer
- 为什么(又见空指针错误null pointer exception 给100分)
- android应用程序 nkd jni error: dereferencing pointer to incomplete type解决办法
- Debug Current Instruction Pointer 有关问题,
- Pointer 分配内存的 有关问题 很严重
- 编译出错!Note: E:\design-houq\TongJi.java uses unchecked or unsafe operations,该如何解决
- 编译报错unchecked or unsafe operations,该怎么解决
- 未获受权(PERMISSION_SET = UNSAFE)。 当符合以下两个条件之一时,将对程序集授权: 数据库所有者(DBO)具有 UNSAFE ASSEMB
- error: pointer targets in passing argument 三 of 'accept' differ in signedness [-Werror=pointer-sign
- Golang 在电商即时通讯服务建设中的实践
- Golang: 什么时候nil != nil
- golang(4)使用beego + ace admin 开发后台系统 CRUD
- 解决C++错误 C4996 'strtok': This function or variable may be unsafe.
- VS 报错error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead.
- Line 1033: Char 34: runtime error: applying non-zero offset 18446744073709551612 to null pointer (st
- golang-类型断言
- golang-reflect反射三定律
- golang-reflect(Typeof()和Kind())
- golang-反射reflect
- golang-nil切片和空切片