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

Issue

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?

Solution

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.

Answered By – Denys Séguret

Answer Checked By – Terry (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.