Golang – 树结构抽象化

huangapple go评论98阅读模式
英文:

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()方法,然后创建一个simpleNodeextendedNode之类的结构体,这可能是您可以考虑的一种方法。

英文:

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?

huangapple
  • 本文由 发表于 2017年7月2日 20:37:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/44870902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定