英文:
golang - tree structure abstraction
问题
我正在尝试编写一个树结构,其中每个节点都应该有一个id和父节点的引用/ id参数。
通过扩展一个节点结构,您应该能够添加一些自定义参数(如标题、图标、颜色...)。它应该是内联的,并在稍后使用mgo插入...
您可以在下面或此处找到代码:https://play.golang.org/p/bbvs2iM3ri
我正在尝试避免向nodeExtension结构添加方法,并通过node结构共享它。但是,CreateNode方法只接收节点数据,而不是包装结构。
有没有办法在不丢失自定义参数(在这种情况下是Description)的情况下实现这个算法?
谢谢
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()
}
输出:
Node: &{item:{ID:1} Parent:}
Node: &{item:{ID:2} Parent:1}
两者都没有描述 :/
英文:
i'm trying to code a tree structure where each node should have an id and parent ref/id params.
by extending a node struct you should be able to add some custom parameters (like title, icon, color...). it should be inlined and inserted with mgo later on...
you can find the code below or here: https://play.golang.org/p/bbvs2iM3ri
i'm trying to avoid adding methods to the nodeExtension struct and sharing it through the node struct. but, the CreateNode method is getting only the node data and not the wrapping struct.
any ideas how to implement this algorithm without loosing the custom parameters (Description in this case)?
thanks
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 :/
答案1
得分: 1
您的CreateNode()
函数操作的是一个名为node
的类型,该类型没有Description
字段。
将函数签名更改为:
func (t *nodeExtension) CreateNode()
在这种情况下,它将按您希望的方式操作:它将打印出整个nodeExtension
结构:
https://play.golang.org/p/nLxblNySB9
我不完全确定您在这里想要实现什么,但也许您可以考虑使用一个类型为node
的接口,并实现一个String()
方法,然后创建一个simpleNode
和extendedNode
之类的结构体,这可能是您可以考虑的一种方法。
英文:
Your CreateNode()
function operates on a type of node
, which does not have a Description
field.
Changing the signature to:
func (t *nodeExtension) CreateNode()
will operate how you want it to in this scenario: it will print out the entire nodeExtension
struct:
https://play.golang.org/p/nLxblNySB9
I'm not entirely sure what you're trying to accomplish here, but maybe having an interface of type node
and which implements a String()
method and then have like a simpleNode
and extendedNode
would be something you could look at?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论