Mar*_*cos 7 struct types pointers interface go
.Children().Father(){id, FatherId}
我只需要这个接口的三个不同的实现,也许为每个结构做整个事情更方便,但我是 Go 的新手,决定使用这个例子来理解接口。
我来到了一个看起来像这样的界面:
type Node interface{
Equals(nodo *Node) bool
AddChild(child *Node)
SetFather(father *Node)
Children() []Node
Father() *Node
}
Populate
func Populate(plainNodes []Node, HierarchichalNodes *[]Node) {}
普通节点将是定义他父亲 id 的项目:
{id: "Animal", father: ""}
{id: "Plant", father: ""}
{id: "Mammals", father: "Animal"}
结果是分层节点:
Animal
|__Mammals
Plant
"Category"
type Category struct{
children []Category
father Category
}
func (c Category) SetFather(node *Node) {
v, ok = node.(*Category)
c.father = v
}
CategoryCategoryNode
我无法进行转换,我得到:
invalid type assertion: nodo.(*Category) (non-interface type *Node on left)
有任何想法吗?