Google的Go语言是通过价值还是引用来复制矢量分配? [英] Are vector assignments copied by value or by reference in Google's Go language?

查看:105
本文介绍了Google的Go语言是通过价值还是引用来复制矢量分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我创建了一个挂钩游戏,然后在它上面移动,这增加了moveAlreadyDone向量。然后我创建另一个钉谜题,然后在其上做出移动,这增加了它的移动已经完成的向量。当我打印出第二个矢量中的值时,它从第一个移动到第二个移动。谁能告诉我为什么它似乎是通过引用分配而不是价值?

 包主

导入是否通过Google或Go语言的值或引用复制向量分配? fmt
importcontainer / vector

类型移动struct {x0,y0,x1,y1 int}

类型PegPuzzle结构{
movesAlreadyDone * vector.Vector;


func(p * PegPuzzle)InitPegPuzzle(){
p.movesAlreadyDone = vector.New(0);
}

func NewChildPegPuzzle(父* PegPuzzle)* PegPuzzle {
retVal:= new(PegPuzzle);
retVal.movesAlreadyDone = parent.movesAlreadyDone;
return retVal
}

func(p * PegPuzzle)doMove(move Move){
p.movesAlreadyDone.Push(move);


func(p * PegPuzzle)printPuzzleInfo(){
fmt.Printf(----------- START ------ ---------------- \\\
);
fmt.Printf(已经完成的动作:%v \ n,p.movesAlreadyDone);
fmt.Printf(------------ END ----------------------- \\\
) ;


func main(){
p:= new(PegPuzzle);
cp1:= new(PegPuzzle);
cp2:= new(PegPuzzle);

p.InitPegPuzzle();

cp1 = NewChildPegPuzzle(p);
cp1.doMove(Move {1,1,2,3});
cp1.printPuzzleInfo();

cp2 = NewChildPegPuzzle(p);
cp2.doMove(移动{3,2,5,1});
cp2.printPuzzleInfo();
}

任何帮助将不胜感激。谢谢!

解决方案

附带的答案,但是 vector.New has已从近期版本的Go中删除。你需要编写

  func(p * PegPuzzle)InitPegPuzzle(){
p.movesAlreadyDone = new(vector。向量);



$ b $ p
$ b

在你的原始代码中,你正在复制的东西是指向向量的指针。这与C中的指针相同。如果你愿意,可以将它称为通过引用,但它们是指针。



要复制整个矢量,请使用 InsertVector

  func(p * PegPuzzle)InitPegPuzzle(){
p.movesAlreadyDone = new(vector.Vector);
}

func NewChildPegPuzzle(父* PegPuzzle)* PegPuzzle {
retVal:= new(PegPuzzle);
retVal.InitPegPuzzle();
retVal.movesAlreadyDone.InsertVector(0,parent.movesAlreadyDone);
return retVal
}

这提供了一个完整的唯一副本。


In the following code, I create one peg puzzle then do a move on it which adds a move to its movesAlreadyDone vector. Then I create another peg puzzle then do a move on it which adds a move to its movesAlreadyDone vector. When I print out the values in that vector for the second one, it has the move in it from the first one along with the move from the second one. Can anyone tell me why it seems to be assigning by reference and not value? Are vector assignments copied by value or by reference in Google's Go language?

package main

import "fmt"
import "container/vector"

type Move struct { x0, y0, x1, y1 int }

type PegPuzzle struct {
    movesAlreadyDone * vector.Vector;
}

func (p *PegPuzzle) InitPegPuzzle(){
    p.movesAlreadyDone = vector.New(0);
}

func NewChildPegPuzzle(parent *PegPuzzle) *PegPuzzle{
    retVal := new(PegPuzzle);
    retVal.movesAlreadyDone = parent.movesAlreadyDone;
    return retVal
}

func (p *PegPuzzle) doMove(move Move){
    p.movesAlreadyDone.Push(move);
}

func (p *PegPuzzle) printPuzzleInfo(){
    fmt.Printf("-----------START----------------------\n");
    fmt.Printf("moves already done: %v\n", p.movesAlreadyDone);
    fmt.Printf("------------END-----------------------\n");
}

func main() {
    p := new(PegPuzzle);
    cp1 := new(PegPuzzle);
    cp2 := new(PegPuzzle);

    p.InitPegPuzzle();

    cp1 = NewChildPegPuzzle(p);
    cp1.doMove(Move{1,1,2,3});
    cp1.printPuzzleInfo();

    cp2 = NewChildPegPuzzle(p);
    cp2.doMove(Move{3,2,5,1});
    cp2.printPuzzleInfo();
}

Any help will be greatly appreciated. Thanks!

解决方案

Incidental to the answer, but vector.New has been deleted from recent versions of Go. You need to write

func (p *PegPuzzle) InitPegPuzzle(){
    p.movesAlreadyDone = new (vector.Vector);
}

In your original code, the things you are copying are pointers to vectors. This is just the same as pointers in C. You can call it "by reference" if you like, but they're pointers.

To copy an entire vector, use InsertVector:

func (p *PegPuzzle) InitPegPuzzle(){
    p.movesAlreadyDone = new (vector.Vector);
}

func NewChildPegPuzzle(parent *PegPuzzle) *PegPuzzle{
    retVal := new (PegPuzzle);
    retVal.InitPegPuzzle ();
    retVal.movesAlreadyDone.InsertVector (0, parent.movesAlreadyDone);
    return retVal
}

This gives a complete unique copy.

这篇关于Google的Go语言是通过价值还是引用来复制矢量分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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