英文:
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 = &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} &{4 2} {1 0} {0 0} &{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 <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";
}
The output of this C++ code shows the same behavior:
*q: { 4, 2 }
t: { 1, 2 }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论