我有一个 Go 对象,它在内存中的地址我想保持不变。在 C# 中,可以固定对象在内存中的位置。有没有办法在 Go 中做到这一点?
最佳答案
您保留引用的对象不会移动。没有句柄或间接,您获得的地址是永久性的。
来自 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
当你设置一个变量时,你可以使用&
运算符读取这个地址,然后你可以传递它。
关于go - 你能用 Go "pin"内存中的对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12098435/