How can I get the pointer of a map

huangapple go评论65阅读模式
英文:

How can I get the pointer of a map

问题

我想要获取 Go 语言中 map 的内存地址,但它返回的是一个连接的 ampersand 和 map 的输出。以下是示例代码:

package main

import "fmt"

var name = map[string]string{
    "name":     "John",
    "lastname": "Doe",
}

func main() {
    fmt.Println(&name)
}

它打印的结果是:

&map[lastname:Doe name:John]

我知道 map 是通过引用进行复制的,不需要获取地址,但是我如何检索地址呢?

英文:

I want to get the memory location address of the map in Go but it returns a concatenated ampersand and the map output.
Here is the example code:

package main

import "fmt"

var name = map[string]string{
	"name":     "John",
	"lastname": "Doe",
}

func main() {
	fmt.Println(&name)

}

what it prints is:

&map[lastname:Doe name:John]

I know that maps are copied by references and there is no need to get the address, but how could I retrieve the address?

答案1

得分: 2

Println将根据类型使用默认格式进行打印,你已经看到了默认格式是什么。你可以使用Printf来打印指针:

func main() {
	fmt.Printf("%p\n", &name)
}
英文:

Println will use the default format based on type, and you already saw what that default format is. You can use Printf to print the pointer:

func main() {
	fmt.Printf("%p\n", &name)

}

答案2

得分: 0

在Go语言中,可以使用内置的&运算符获取字符串变量的内存位置指针,因为Go中的字符串被表示为指向包含字符串字符的连续内存块的指针。因此,可以使用&运算符来获取指向该内存位置的指针。

另一方面,Go中的映射(maps)是比字符串更复杂的数据结构,它们由指向包含映射元数据和底层数据指针的运行时结构的指针表示。这个运行时映射对象的确切结构在不同版本的Go运行时中可能会有所改变。因此,映射对象的指针不能保证与其他类型兼容,并且可能不安全地使用,除非进行类型转换或使用unsafe包。

有两种方法可以获取地址:

第一种解决方案是使用格式说明符,这对于打印指针很有帮助。第二种方法是使用unsafe包:

package main

import (
	"fmt"
	"unsafe"
)

var name = map[string]string{
	"name":     "John",
	"lastname": "Doe",
}

func main() {
	ptr := unsafe.Pointer(&name)
	fmt.Println(ptr)

}

unsafe包提供了直接访问和操作内存的低级功能,允许您绕过类型系统并访问通常不可访问的内存位置。然而,这样做会带来安全性和可移植性的代价,因此应该小心使用,只在绝对必要时使用。

英文:

Well, in Go you can get a memory location pointer of a string variable using the built-in & operator because strings in Go are represented as a pointer to a contiguous block of memory that contains the string's characters. Therefore, you can use the & operator to get a pointer to this memory location.

On the other hand, maps in Go are more complex data structures than strings, and they are represented by a pointer to a runtime structure that contains the map's metadata and the pointer to the underlying data. The exact structure of this runtime map object is subject to change in different versions of the Go runtime. Therefore, the pointer to a map object is not guaranteed to be compatible with other types and may not be used safely without casting or using the unsafe package.

There are two ways to reach the address:

First solution as Burak mentioned is using a format specifier which is helpful if printing the pointer is intended. And the second one is to using unsafe package:

package main

import (
	"fmt"
	"unsafe"
)

var name = map[string]string{
	"name":     "John",
	"lastname": "Doe",
}

func main() {
	ptr := unsafe.Pointer(&name)
	fmt.Println(ptr)

}

The unsafe package provides low-level facilities to access and manipulate memory directly, which allows you to work around the type system and access memory locations that are not normally accessible. However, this comes at the cost of safety and portability, so it should be used with care and only when absolutely necessary.

huangapple
  • 本文由 发表于 2023年2月16日 09:36:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467028.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定