How do you perform a deep copy of a struct in Go?

I was close. I should have assigned the copiedTree to the parent property.

func (tree *Tree) CopyTree() *Tree {
    if (tree == nil) {
        return nil
    } else {
        copiedTree := &Tree {
            tree.Left.CopyTree(),
            tree.Mid.CopyTree(),
            tree.Right.CopyTree(),
            tree.Value,
            nil,
            tree.Orientation,
            tree.IsTerminal,
            tree.Type,
        }

        if copiedTree.Left != nil {
            copiedTree.Left.Parent = copiedTree
        }
        if copiedTree.Right != nil {
            copiedTree.Right.Parent = copiedTree
        }
        if copiedTree.Mid != nil {
            copiedTree.Mid.Parent = copiedTree
        }
        return copiedTree
    }
}

Tags:

Deep Copy

Go