Can the pointer in a struct pointer method be reassigned to another instance?
<p>I've been looking into Golang and have been implementing a few data structures to learn how the language works.  I've come across the following issue while writing the code for an AVL tree:</p>

<p>Assigning the primary pointer from a struct pointer method seems to have no effect outside the scope of the function.  E.g. <code>tree.rotateLeftToRoot()</code> doesn't result in <code>tree.left</code> becoming the new tree.</p>

<p><strong>Question:</strong> Is there a way to reassign the pointer in a struct pointer method in Golang, or is this generally discouraged?  In the example this would be the <code>"tree = prevLeft"</code> line.</p>

<p>Code snippet:</p>

<pre><code>//Graphical representation of t.rotateLeftToRoot():
//      t                  L
//   L     R     -&gt;    LL     t
//LL LR                     LR  R
func (tree *AvlTree) rotateLeftToRoot() {
   if tree == nil {
      return
   }
   prevLeft := tree.left
   if prevLeft != nil {
      tree.left = prevLeft.right //tree.left passed root its right branch
      prevLeft.right = tree      //tree becomes tree.left's right branch
      tree.updateHeight()
      prevLeft.updateHeight()
      tree = prevLeft            //desired behaviour: tree.left becomes the new tree
                                 //actual behaviour: no effect when function returns
   }
}
</code></pre>

<p>I've tried other combinations of setting the value or address of tree, and none of them had the intended effect. For example, <code>*tree = *prevLeft</code> results in an infinite loop.</p>

<p>Additional note: Returning <code>tree</code> and setting <code>"tree = tree.rotateLeftToRoot()"</code> avoids the issue.  This works, but it seems dirty to be mixing effects and requiring assignment to returned values, when the caller really just wants to be able to call a function to update the tree.</p>

<p>Can the <code>tree</code> be set to <code>prevLeft</code> from within the function?</p>