指针解引用在Go语言中是如何工作的?

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

How does pointer dereferencing work in Go?

问题

我正在阅读http://tour.golang.org/上的Golang教程,并在示例29中进行一些实验。

原始示例如下:

package main

import "fmt"

type Vertex struct {
    X, Y int
}

var (
    p = Vertex{1, 2}  // 类型为Vertex
    q = &Vertex{1, 2} // 类型为*Vertex
    r = Vertex{X: 1}  // Y:0 是隐式的
    s = Vertex{}      // X:0 和 Y:0
)

func main() {
    fmt.Println(p, q, r, s)
}

这是一个非常基本的示例,展示了如何创建这个新的结构体Vertex的实例。然而,示例28展示了如何通过指针来操作一个顶点,所以我对示例进行了一些修改,并对输出结果感到惊讶。以下是修改后的代码:

func main() {
    t := *q
    q.X = 4
    u := *q
    fmt.Println(p, q, r, s, t, u, t == u)
}

输出结果如下:

{1 2} &{4 2} {1 0} {0 0} {1 2} {4 2} false

让我感到惊讶的是,t并不是{4, 2},这似乎意味着修改q.X会改变q指向的结构体实例。作为一个有C/C++背景的人,这对我来说是非常奇怪的行为。

那么,实际上发生了什么呢?为什么使用q.X = 4来改变顶点不会传播到t呢?

英文:

I'm going through the golang tutorials at http://tour.golang.org/, and was experimenting a bit with some things in example 29

For your reference, the original example is copied here:

package main

import "fmt"

type Vertex struct {
    X, Y int
}

var (
    p = Vertex{1, 2}  // has type Vertex
    q = &Vertex{1, 2} // has type *Vertex
    r = Vertex{X: 1}  // Y:0 is implicit
    s = Vertex{}      // X:0 and Y:0
)

func main() {
    fmt.Println(p, q, r, s)
}

It's pretty basic, showing how to create instances of this fancy new struct, Vertex. Example 28, though, shows manipulating a vertex via a pointer to it, so I modified the example a bit and was surprised by the output. Here is the modification:

func main() {
    t := *q
    q.X = 4
    u := *q
    fmt.Println(p, q, r, s, t, u, t == u)
}

And the output:

{1 2} &{4 2} {1 0} {0 0} {1 2} {4 2} false

The thing that surprised me is that t is not {4, 2}, which seems to mean that changing q.X changed the instance of the struct that q pointed to. Coming from a C/C++ background, this seems like extremely strange behavior to me.

So, what's actually going on here? Why does using q.X = 4 to change the Vertex not propagate to t?

答案1

得分: 54

t := *q 会复制 q 指向的结构体。

如果你想通过 t 观察到 q 的变化,那么应该使用指针:

var (
    p = Vertex{1, 2}  // 类型为 Vertex
    q = &Vertex{1, 2} // 类型为 *Vertex
    r = Vertex{X: 1}  // Y:0 是隐式的
    s = Vertex{}      // X:0 和 Y:0
)

func main() {
    t := q
    q.X = 4
    u := *q
    fmt.Println(p, q, r, s, t, u, *t == u)
}

这将产生你可能期望的输出:

{1 2} &{4 2} {1 0} {0 0} &{4 2} {4 2} true

我不确定你觉得什么地方非常奇怪。C 和 C++ 的行为是相同的。考虑以下代码:

#include <iostream>

struct Vertex
{
    int x;
    int y;
};

std::ostream& operator<<(std::ostream& out, const Vertex& v)
{
    out << "{ " << v.x << ", " << v.y << " }"; 
    return out;
}

int main()
{
    Vertex v = Vertex{1, 2};
    Vertex* q = &v;
    Vertex t = *q;
    q->x = 4;
    std::cout << "*q: " << *q << "\n";
    std::cout << " t: " << t << "\n";
}

这段 C++ 代码的输出结果与前面的 Go 代码相同:

*q: { 4, 2 }  
t: { 1, 2 }
英文:

t := *q makes a copy of the struct pointed to by q.

If you want to observe changes to q through t, then stick with a pointer:

var (
    p = Vertex{1, 2}  // has type Vertex
    q = &amp;Vertex{1, 2} // has type *Vertex
    r = Vertex{X: 1}  // Y:0 is implicit
    s = Vertex{}      // X:0 and Y:0
)


func main() {
    t := q
    q.X = 4
    u := *q
    fmt.Println(p, q, r, s, t, u, *t == u)
}

This produces the output you were probably looking for.

{1 2} &amp;{4 2} {1 0} {0 0} &amp;{4 2} {4 2} true

I'm not sure what seems extremely strange to you. C and C++ behave the same way. Consider the following:

#include &lt;iostream&gt;

struct Vertex
{
    int x;
    int y;
};

std::ostream&amp; operator&lt;&lt;(std::ostream&amp; out, const Vertex&amp; v)
{
    out &lt;&lt; &quot;{ &quot; &lt;&lt; v.x &lt;&lt; &quot;, &quot; &lt;&lt; v.y &lt;&lt; &quot; }&quot;; 
    return out;
}

int main()
{
    Vertex v = Vertex{1, 2};
    Vertex* q = &amp;v;
    Vertex t = *q;
    q-&gt;x = 4;
    std::cout &lt;&lt; &quot;*q: &quot; &lt;&lt; *q &lt;&lt; &quot;\n&quot;;
    std::cout &lt;&lt; &quot; t: &quot; &lt;&lt; t &lt;&lt; &quot;\n&quot;;
}

The output of this C++ code shows the same behavior:

*q: { 4, 2 }  
t: { 1, 2 }

huangapple
  • 本文由 发表于 2014年11月23日 09:01:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/27084401.html
匿名

发表评论

匿名网友

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

确定