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
(typeuintptr
) as typeuint64
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With