Golang – 如何将结构体添加到切片并保持内存地址不变?

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

Golang - How to add struct to slice with keeping memory address?

问题

如下所示,我创建了一个实例并将其添加到切片中。我想要保留实例的内存地址,但实际上并没有保留。

type Person struct {
    name string
}

func main() {
    p := Person{name: "foo"}
    ps1 := []Person{p}
    ps2 := []Person{p}
    
    fmt.Printf("%p\n", &p)      // 0xc000010250
    fmt.Printf("%p\n", &ps1[0]) // 0xc000010260
    fmt.Printf("%p\n", &ps2[0]) // 0xc000010270
}

我知道如果我使用指针和指针切片,是可以实现的。

type Person struct {
    name string
}

func main() {
    p := Person{name: "foo"}
    ps1 := []*Person{&p}
    ps2 := []*Person{&p}

    fmt.Printf("%p\n", &p)   // 0xc00010a210
    fmt.Printf("%p\n", ps1[0]) // 0xc00010a210
    fmt.Printf("%p\n", ps2[0]) // 0xc00010a210
}

然而,由于接下来的方法参数类型(不在这里)的原因,我希望将ps1和ps2的类型设置为[]Person。有没有什么办法可以实现这一点?

英文:

As shown below, I create instance and add to slice. I want to keep memory address of instance but, it is not.

type Person struct {
    name string
}

func main() {
    p := Person{name: "foo"}
    ps1 := []Person{p}
    ps2 := []Person{p}
    
	fmt.Printf("%p\n", &p) // 0xc000010250
    fmt.Printf("%p\n", &ps1[0]) // 0xc000010260
	fmt.Printf("%p\n", &ps2[0]) // 0xc000010270
}

I know it is possible if I use pointer and slice of pointers.

type Person struct {
    name string
}

func main() {
    p := Person{name: "foo"}
    ps1 := []*Person{&p}
    ps2 := []*Person{&p}

    fmt.Printf("%p\n", &p) // 0xc00010a210
    fmt.Printf("%p\n", ps1[0]) // 0xc00010a210
    fmt.Printf("%p\n", ps2[0]) // 0xc00010a210
}

However, I want to make ps1 and ps2 as type of []Person because of the parameter types of the methods that will follow(not here). Is there any way?

答案1

得分: 5

每个在Go程序中定义的变量都占据一个唯一的内存位置。不可能创建一个Go程序,其中两个变量共享相同的内存存储位置。可以创建两个变量,它们的内容指向相同的存储位置,但这与两个共享相同存储位置的变量不同。

所以简而言之,如果你希望ps1[0]ps2[0]指向内存中的同一位置,你必须使用[]*Person

英文:

Each variable defined in a Go program occupies a unique memory location. It is not possible to create a Go program where two variables share the same storage location in memory. It is possible to create two variables whose contents point to the same storage location, but that is not the same thing as two variables who share the same storage location.

So in short, no, if you want ps1[0] and ps2[0] to point to the same location in memory, you must use an []*Person

答案2

得分: -2

不,你不能这样做。切片是一块连续的内存块。局部变量被分配到任意可用的内存地址上。mySlice[0]mySlice[1]将始终在相邻的内存中,它们不能分别指向一些随机位置,这些位置恰好是分配给不同局部变量的。

我想保留实例的内存地址

在实际中没有理由需要或者想要这样做。如果你认为你需要这样做,你应该重新审视你的推理,并确定你实际上需要什么。

英文:

No, you can't. A slice is a contiguous block of memory. Local variables are allocated to arbitrary available memory addresses. mySlice[0] and mySlice[1] will always be in neighboring memory, they can't each point at some random location where different local variables happened to be allocated.

> I want to keep memory address of instance

There is no reason in practice to need or want this. If you think you need this, you should review your reasoning and determine what you actually need.

huangapple
  • 本文由 发表于 2022年7月23日 00:58:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/73083726.html
匿名

发表评论

匿名网友

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

确定