【发布时间】:2014-10-27 23:24:38
【问题描述】:
我正在尝试执行以下结构的深层复制:
// Ternary Tree
type Tree struct {
Left *Tree
Mid *Tree
Right *Tree
Value interface{}
Parent *Tree
Orientation string
IsTerminal bool
Type string
}
以下是我很抱歉的尝试。看起来我正在根处创建一棵新树,但它的孩子仍然指向内存中的相同地址。
func (tree *Tree) CopyTree() *Tree {
if (tree == nil) {
return nil
} else {
copiedTree := &Tree {
tree.Left.CopyTree(),
tree.Mid.CopyTree(),
tree.Right.CopyTree(),
tree.Value,
tree.Parent.CopyTree(),
tree.Orientation,
tree.IsTerminal,
tree.Type}
return copiedTree
}
}
在 Go 中是否有任何有用的结构可以帮助深度复制结构?如果没有,我将如何自己执行此深层复制?请注意,“deepcopy”包不再有效,因为它使用了一些在 Go 1 中被弃用的函数
【问题讨论】:
-
没有内置任何东西。但是有packages such as DeepCopy that can do it for you(请记住“实验”状态)
-
@SimonWhitehead 我试了一下那个包。不幸的是,它使用了一堆随着 Go 1 的发布而被弃用的函数
-
道歉..我没有意识到(我必须在 Go 1 之前使用它)。
-
您确定您提供的代码演示了您描述的行为吗?似乎有一个无限循环,父复制子复制父。另外,您不是在复制价值:这是有意的吗?
-
@Anonymous 我不确定!而且,不,这不是故意的。