Pointers Golang指针作为方法参数 我用Gangang-Posits和C++做的工作,但似乎不起作用,哪种方法是正确的?还是我做错了什么?谢谢

Pointers Golang指针作为方法参数 我用Gangang-Posits和C++做的工作,但似乎不起作用,哪种方法是正确的?还是我做错了什么?谢谢,pointers,go,Pointers,Go,ftw我正在做AsyncBinaryTrees type Obj interface { Compare(node Obj) int } type Tree struct { Item Obj Rigth, Left *Tree height int16 } func Insert(t *Tree, item Obj) chan struct{} { done := make(chan struct{}, 1) go i

ftw我正在做AsyncBinaryTrees

type Obj interface {
    Compare(node Obj) int
}

type Tree struct {
    Item        Obj
    Rigth, Left *Tree
    height      int16
}

func Insert(t *Tree, item Obj) chan struct{} {
    done := make(chan struct{}, 1)
    go insert(t, item, done)
    return done
}

func insert(t *Tree, item Obj, done chan struct{}) {
    if t == nil {
        t = &Tree{Item: nil, Rigth: nil, Left: nil, height: 0}
        var signal struct{}
        done <- signal
        close(done)
    } else {
        if t.Item.Compare(item) == 1 { //Left
            insert(t.Left, item, done)
        } else if t.Item.Compare(item) == -1 { //Rigth
            insert(t.Right, item, done)
        } else {
            close(done)
            panic
        }
    }
}

//=== testing

func assertSignal(ch_signal chan struct{}, t *testing.T) {
    _, done := <-ch_signal
    if !done {
        t.Error("Error: it should send a signal of empty struct")
    }
}

func TestInsertion(t *testing.T) {
    var tree *Tree
    ch_signal := Insert(tree, newObjInt())
    fmt.PrintLn(t)             //=> <nil>
    assertSignal(ch_signal, t) //=>PASS
    ch_signal = Insert(tree, newObjInt())
    fmt.PrintLn(t)             //=> <nil>
    assertSignal(ch_signal, t) //=>PASS
    ch_signal = Insert(tree, newObjInt())
    fmt.PrintLn(t)             //=> <nil>
    assertSignal(ch_signal, t) //=>PASS
    ch_signal = Insert(tree, newObjInt())
    assertSignal(ch_signal, t) //=>PASS
}
类型Obj接口{
比较(节点Obj)int
}
类型树结构{
项目Obj
右,左*树
高度int16
}
func Insert(t*Tree,item Obj)chan结构{}{
完成:=make(chan结构{},1)
开始插入(t,项目,完成)
返回完成
}
func insert(t*树,项Obj,done chan结构{}){
如果t==nil{
t=&Tree{Item:nil,right:nil,Left:nil,height:0}
变量信号结构{}
通过
ch_信号=插入(树,newObjInt())
fmt.PrintLn(t)/=>
资产信号(通道信号,t)/=>通过
ch_信号=插入(树,newObjInt())
fmt.PrintLn(t)/=>
资产信号(通道信号,t)/=>通过
ch_信号=插入(树,newObjInt())
资产信号(通道信号,t)/=>通过
}


在您的
插入
功能中,测试通过:

func insert(t *Tree, item Obj, done chan struct{}) {
    if t == nil {
        t = &Tree{Item: nil, Rigth: nil, Left: nil, height: 0}
    ...
}
这将更新局部变量
t
,但不会更改在调用范围内传递的变量,因为Go按值传递函数参数。因此,在进行以下调用时:

insert(t.Left, item, done)
如果
t.Left
nil
,则函数调用不会更改其值。如果确实希望它更新变量,则需要将函数参数定义为
t**Tree
,将引用改为set
*t
,并将调用更改为:

insert(&t.Left, item, done)

在通过引用传递函数参数方面,没有与C++等价的语法:相反,传递指针时需要显式。

insert
函数中,您有:

func insert(t *Tree, item Obj, done chan struct{}) {
    if t == nil {
        t = &Tree{Item: nil, Rigth: nil, Left: nil, height: 0}
    ...
}
这将更新局部变量
t
,但不会更改在调用范围内传递的变量,因为Go按值传递函数参数。因此,在进行以下调用时:

insert(t.Left, item, done)
如果
t.Left
nil
,则函数调用不会更改其值。如果确实希望它更新变量,则需要将函数参数定义为
t**Tree
,将引用改为set
*t
,并将调用更改为:

insert(&t.Left, item, done)

在通过引用传递函数参数方面,没有与C++等价的语法:相反,传递指针时需要显式。

insert
函数中,您有:

func insert(t *Tree, item Obj, done chan struct{}) {
    if t == nil {
        t = &Tree{Item: nil, Rigth: nil, Left: nil, height: 0}
    ...
}
这将更新局部变量
t
,但不会更改在调用范围内传递的变量,因为Go按值传递函数参数。因此,在进行以下调用时:

insert(t.Left, item, done)
如果
t.Left
nil
,则函数调用不会更改其值。如果确实希望它更新变量,则需要将函数参数定义为
t**Tree
,将引用改为set
*t
,并将调用更改为:

insert(&t.Left, item, done)

在通过引用传递函数参数方面,没有与C++等价的语法:相反,传递指针时需要显式。

insert
函数中,您有:

func insert(t *Tree, item Obj, done chan struct{}) {
    if t == nil {
        t = &Tree{Item: nil, Rigth: nil, Left: nil, height: 0}
    ...
}
这将更新局部变量
t
,但不会更改在调用范围内传递的变量,因为Go按值传递函数参数。因此,在进行以下调用时:

insert(t.Left, item, done)
如果
t.Left
nil
,则函数调用不会更改其值。如果确实希望它更新变量,则需要将函数参数定义为
t**Tree
,将引用改为set
*t
,并将调用更改为:

insert(&t.Left, item, done)

在通过引用传递函数参数方面,没有与C++相当的语法:相反,传递指针时需要显式。

什么不起作用?好吧,除了调用
insert(t.Left,done)等明显的事情之外
到一个包含三个参数的函数。关闭时不发送任何信号。输入错误我签出了,所以我在这里复制了错误,我正在关闭通道,因为该项已经存在,所以没有信号。您必须首先初始化树结构:将
var Tree*Tree
替换为
Tree:=new(Tree)
。指针是通过值传递的,因此从方法内部进行的结构初始化不会像您预期的那样工作:新引用(
t=&Tree
)不会离开方法范围。请参见插图:什么不起作用?除了调用
insert(t.Left,done)等明显的事情之外
到一个包含三个参数的函数。关闭时不发送任何信号。输入错误我签出了,所以我在这里复制了错误,我正在关闭通道,因为该项已经存在,所以没有信号。您必须首先初始化树结构:将
var Tree*Tree
替换为
Tree:=new(Tree)
。指针是通过值传递的,因此从方法内部进行的结构初始化不会像您预期的那样工作:新引用(
t=&Tree
)不会离开方法范围。请参见插图:什么不起作用?除了调用
insert(t.Left,done)等明显的事情之外
到一个包含三个参数的函数。关闭时不发送任何信号。输入错误我签出了,所以我在这里复制了错误,我正在关闭通道,因为该项已经存在,所以没有信号。您必须首先初始化树结构:将
var Tree*Tree
替换为
Tree:=new(Tree)
。指针是通过值传递的,因此从方法内部进行的结构初始化不会像您预期的那样工作:新引用(
t=&Tree
)不会离开方法范围。请参见插图:什么不起作用?除了调用
insert(t.Left,done)等明显的事情之外
到一个包含三个参数的函数。并且在不发送任何信号的情况下关闭。输入错误我进行了签出,因此复制了错误