Is it possible to inherit type methods without using inline structures?
The first piece of code is the working code that embeds the Property structure in Node , and I can call node.GetString , which is a method on Properties . I don't like this when I initialize the Node . I have (?) To initialize the Properties structure inside it. Is there any way around this?
package main import "fmt" type Properties map[string]interface{} func (p Properties) GetString(key string) string { return p[key].(string) } type Nodes map[string]*Node type Node struct { *Properties } func main() { allNodes := Nodes{"1": &Node{&Properties{"test": "foo"}}}
Ultimately, I would like to do something like the following. Where Node is of type Properties , and initialization does not require initialization of the Property structure. The following code does not work, but it can be clear what my purpose is.
package main import "fmt" type Properties map[string]interface{} func (p Properties) GetString(key string) string { return p[key].(string) } type Nodes map[string]*Node type Node Properties func main() { allNodes := Nodes{"1": &Node{"test": "foo"}}
I will add more structures that will use the methods of Properties , so I ask. If I had only Node , I would only have methods for Node and executed. But since I will have more than Node , I find it unnecessary to add the same methods to all structures that insert Properties
I think more for a specific problem, I want to use Properties methods from Node without initializing Properties .
source share