質問

I'm trying to assign the value found in a variable of type uintptr to a uint64 variable in Go. Using

myVar = valFromSystem

gives me

cannot use valFromSystem (type uintptr) as type uint64 in assignment

And trying

myVar = *valFromSystem

gives me

invalid indirect of valFromSystem (type uintptr)

Is there a way to pull the value pointed to by valFromSystem to assign to myVar?

役に立ちましたか?

解決

First, cast valFromSystem into an unsafe.Pointer. An unsafe.Pointer can be casted into any pointer type. Next, cast the unsafe.Pointer into a pointer to whatever type of data valFromSystem points to, e.g. an uint64.

ptrFromSystem = (*uint64)(unsafe.Pointer(valFromSystem))

If you just want to get the value of the pointer (without dereferencing it), you can use a direct cast:

uint64FromSystem = uint64(valFromSystem)

Though remember that you should use the type uintptr when using pointers as integers.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top