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
", 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 :/