你能“钉住”吗? Go的记忆中的对象? [英] Can you "pin" an object in memory with Go?
问题描述
我有一个Go对象,它的地址在内存中我想保持不变。在C#中,可以将对象的位置固定在内存中。有没有办法在Go中做到这一点?
保留引用的对象不会移动。没有句柄或间接,你得到的地址是永久的。
从文档:注意,与C不同的是,返回
局部变量的地址是完全可以的;与变量相关的存储在函数返回后仍然存在
当你设置一个变量时,你可以使用这个地址来读取这个地址&
运算符,并且您可以传递它。
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?
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.
这篇关于你能“钉住”吗? Go的记忆中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!