Reputation: 21
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 :/
Upvotes: 0
Views: 673
Reputation: 51
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?
Upvotes: 1