指针解引用如何在golang中工作? [英] How does pointer dereferencing work in golang?

查看:198
本文介绍了指针解引用如何在golang中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览 http://tour.golang.org/ 上的golang教程,以及在示例29 中进行了一些尝试。

  package main 

import fmt

类型顶点结构{
X,Y int
}

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


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

它非常基本,展示了如何创建这个新结构的实例, Vertex 。然而,示例28 显示了通过指向顶点的指针操作顶点,所以我修改了一下示例并对输出感到惊讶。这里是修改:

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

并输出:

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

让我感到惊讶的是, t 不是{4,2},这似乎意味着改变 qX 更改指向 q 的结构实例。来自C / C ++的背景,这对我来说似乎是非常奇怪的行为。



那么,这里究竟发生了什么?为什么使用 qX = 4 来更改顶点不会传播到 t

t:= * q 复制了 q指向的结构体的副本



如果您想观察对 q t ,然后粘贴一个指针:

  func main(){
t: = q
qX = 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> 

结构顶点
{
int x;
int y;
};

std :: ostream&运算符<<(lt; ostream& out,const Vertex& v)
{
out<< {<< v.x<< ,<< v.y<< };
退出;
}

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

此C ++代码的输出显示相同的行为:

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

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?

解决方案

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:

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 }

这篇关于指针解引用如何在golang中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆