Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you "pin" an object in memory with Go?

Tags:

go

I have a Go object whose address in memory I would like to keep constant. in C# one can pin an object's location in memory. Is there a way to do this in Go?

like image 371
TSL Avatar asked Aug 23 '12 19:08

TSL


1 Answers

An object on which you keep a reference won't move. There is no handle or indirection, and the address you get is permanent.

From the documentation :

Note that, unlike in C, it's perfectly OK to return the address of a local variable; the storage associated with the variable survives after the function returns

When you set a variable, you can read this address using the & operator, and you can pass it.

like image 51
Denys Séguret Avatar answered Oct 19 '22 16:10

Denys Séguret