亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

調用結構屬性方法時 Golang 結構指針引用丟失

調用結構屬性方法時 Golang 結構指針引用丟失

Go
慕少森 2023-05-22 17:16:35
我正在嘗試使用 astruct來管理樹上的訪問節點。每當我訪問父節點的子節點的方法時,后續調用的父引用就會丟失(即parent.child.method(child) -> [parent becomes nil]-> parent(the previous child).child ... etc)。這是我文件中的錯誤片段。type Node struct {    Left *Node    Right *Node    value int}func (parent *Node) determineSide(child *Node) (Node, Node) {    if child.Value < parent.Value {        if parent.hasLeftNode() {            return parent.Left.determineSide(child)        }        return parent.addLeftNode(child)    } else if child.Value > parent.Value {        if parent.hasRightNode() {           return parent.Right.determineSide(child)        }        return parent.addRightNode(child)    }    return *child, *parent }我試圖通過嘗試找到一種方法來通知新引用應該是的方法來解決這個問題parent.Left。*parent.Leftusing和之類的東西&parent.Left似乎不正確。一個解決方案可能是將此代碼移出,struct并讓另一個函數處理結果以進行快速修復,但我想了解為什么這不是開箱即用的。這里的思維過程受到使用的影響this.child.determineSide(child)。完整代碼在這里。
查看完整描述

2 回答

?
達令說

TA貢獻1821條經驗 獲得超6個贊

好吧,我終于知道你到底在問什么了。

New()?方法返回一個值,而不是指針,這意味著您看不到調用者以后的更改。調用者得到的只是 Node.js 的一個值副本。所以你打印的父級將永遠是{Left:<nil> Right:<nil> Value:2}。

所以和addLeftNode()一樣addRightNode()。

只需使用指針,而不是價值來實現您的目標。


我認為這只是Visit()問題所在的方法。

  1. 當您在訪問左孩子后立即返回時,它永遠不會訪問右孩子。

  2. 左右孩子不互斥,所以第二個 if 子句不應該使用else if,應該是if。

  3. 訪問順序也有問題。

前:


// Visit will automatically walk through the Child Nodes of the accessed Parent Node.

func (parent *Node) Visit() (Node, int) {

? ? fmt.Println("Node value:", parent.Value)

? ? if parent.hasLeftNode() {

? ? ? ? return parent.Left.Visit()

? ? } else if parent.hasRightNode() {

? ? ? ? return parent.Right.Visit()

? ? }

? ? return *parent, parent.Value

}

修改的:


// Visit will automatically walk through the Child Nodes of the accessed Parent Node.

func (parent *Node) Visit() (Node, int) {

? ? if parent.hasLeftNode() {

? ? ? ? parent.Left.Visit()

? ? }

? ? fmt.Println("Node value:", parent.Value)

? ? if parent.hasRightNode() {

? ? ? ? parent.Right.Visit()

? ? }

? ? return *parent, parent.Value

}

另外,對我來說,Visit()不應該返回任何值。


查看完整回答
反對 回復 2023-05-22
?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

前:


// Visit will automatically walk through the Child Nodes of the accessed Parent Node.

func (parent *Node) Visit() (Node, int) {

    fmt.Println("Node value:", parent.Value)

    if parent.hasLeftNode() {

        return parent.Left.Visit()

    } else if parent.hasRightNode() {

        return parent.Right.Visit()

    }

    return *parent, parent.Value

}

修改的:


// Visit will automatically walk through the Child Nodes of the accessed Parent Node.

func (parent *Node) Visit() (Node, int) {

    if parent.hasLeftNode() {

        parent.Left.Visit()

    }

    fmt.Println("Node value:", parent.Value)

    if parent.hasRightNode() {

        parent.Right.Visit()

    }

    return *parent, parent.Value

}

另外,對我來說,Visit()不應該返回任何值。


查看完整回答
反對 回復 2023-05-22
  • 2 回答
  • 0 關注
  • 236 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號