4
如何将**T
类型的变量转换为*unsafe.Pointer
?将** T转换为*不安全。指针
下面的例子将给出编译错误:如果我切换到(*unsafe.Pointer)(&ptr)
&unsafe.Pointer(ptr)
cannot convert &ptr (type **s) to type *unsafe.Pointer
package main
import (
"sync/atomic"
"unsafe"
)
type s struct {
value int
}
func main(){
var ptr *s
a := &s{42}
old := ptr
atomic.CompareAndSwapPointer(
(*unsafe.Pointer)(&ptr), // &unsafe.Pointer(ptr)
unsafe.Pointer(old),
unsafe.Pointer(a))
}
,我会得到这个编译错误:
cannot take the address of unsafe.Pointer(ptr)
诗篇。我选择sync/atomic
作为例子,因为这是您实际上必须进行此类转换的一种情况。
编辑
一个不正确的解决办法是使用一个临时变量:
up := unsafe.Pointer(ptr)
atomic.CompareAndSwapPointer(&up, ...
虽然汇总,CAS只会掉什么是存储在up
而不是ptr
。这不是理想的结果,因为zeebo @#go-nuts指出。