go - 你能用 Go "pin"内存中的对象吗?

标签 go

我有一个 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/

相关文章:

go - 如何在 app.yaml 中为 Go 传递运行参数?

arrays - 为什么 *S Slice 可以由文字 S 开头?

go - 如何在 GO 中将 interface{} 转换为 map

python - Golang相当于在Python中创建一个子进程

sockets - 如何读取在 Golang 中接收到的 UDP 数据报的输入接口(interface)和远程套接字地址?

for-loop - 暂停for循环的执行

go - 使用 pipe 执行 go binary 时使用 scanln

go - 使用 goroutine 运行 cmd.Wait() 时的错误处理

string - 如何为结构提供字符串格式?

go - 从 opentelemetry 的请求 header 中提取跟踪器的正确方法