【发布时间】:2017-12-05 20:33:28
【问题描述】:
我正在尝试编写一个树结构,其中每个节点都应该有一个 id 和父 ref/id 参数。
通过扩展节点结构,您应该能够添加一些自定义参数(如标题、图标、颜色...)。以后应该用mgo内联和插入...
您可以在下面或此处找到代码:https://play.golang.org/p/bbvs2iM3ri
我试图避免向 nodeExtension 结构添加方法并通过 node 结构共享它。但是,CreateNode 方法只获取节点数据而不是包装结构。
任何想法如何在不丢失自定义参数的情况下实现此算法(在这种情况下为描述)?
谢谢
package main
import (
"fmt"
)
type item struct {
ID string
}
type node struct {
item `bson:,inline`
Parent string
}
func (t *node) CreateNode() {
fmt.Printf("Node: %+v\n", t)
}
type nodeExtension struct {
node `bson:,inline`
Description string
}
func main() {
i := &nodeExtension{
node: node{
item: item{
ID: "1",
},
Parent: "",
},
Description: "Root node",
}
i.CreateNode()
i = &nodeExtension{
node: node{
item: item{
ID: "2",
},
Parent: "1",
},
Description: "Another node",
}
i.CreateNode()
}
// output:
// Node: &{item:{ID:1} Parent:}
// Node: &{item:{ID:2} Parent:1}
// both without description :/
【问题讨论】: