你能“钉住”吗? Go的记忆中的对象? [英] Can you "pin" an object in memory with Go?

查看:73
本文介绍了你能“钉住”吗? 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆