TL;DR See playground link at bottom.
I have methods defined on a Manager
struct which contains a Context
. The Manager
s are versioned, allowing a new version to only define functions which have changed, and automatically use the functions from the old version if they are not redefined.
type Context struct { ... }
type Manager1 struct{
Context Context
}
type Manager2 struct {
Manager1
Context Context
}
When calling a function on Manager2
which is not defined on Manager2
the Context
is nil. Is there a way to do this where the context will be available?
This example demonstrates the problem better than I can explain it: http://play.golang.org/p/gFe6GgUKEJ
You're misusing embedding. The thing is, you've defined Context
on Manager1
, in spite of that you've redefined it on each subsequent type. In Manager3
you're setting the value for it's Context
instance. When Hello()
is called, it's defined on Manager2
and accesses it's Context
instance which doesn't have a value. Check out this example to demonstrate that http://play.golang.org/p/XebShA9ap4
Money line is: m3 = Manager3{Manager2: Manager2{Context: Context{Value: "testing3"}}}
As you can see, if I instantiate the Manager2
instance embedded in Manager3
and set it's Context
value it gets printed. I would recommend changing your types so that Context
is only defined on Manager1
and then use syntax like that in my example when you initialize your types.
EDIT: To put the design discussed in comments in writing, you would change your types to this;
type Context struct {
Value string
}
type Manager1 struct {
Context Context
}
type Manager2 struct {
Manager1
}
type Manager3 struct {
Manager2
}
Delete the implementation of Hello()
on Manager2
altogether. Then update your composite-literal initialization to this;
m1 := Manager1{Context: Context{Value: "testing1"}}
m2 := Manager2{Manager1: Manager1{Context: Context{Value: "testing2"}}}
m3 := Manager3{Manager2: Manager2{Manager1: Manager1{Context: Context{Value: "testing3"}}}}
You are misunderstanding calling methods of embedded structs via the embedding struct: This is just syntactic sugar!
You are allowed to abbreviate m3.Manager2.Hello()
to m3.Hello()
but the Hello method is never invoked "on" m3 but always on the embedded Manager2
(which has a nil Contex).
Embedding does not allow to "overwrite methods". Embedding is not subclassing, it is syntactic sugar only.
Your Manager3
struct contains a number of Context
members - you're setting Manager3.Context
, but printing Manager2.Context
. What you're probably looking for is removing the redundant Context
members from Manager1
and Manager2
and initializing the structs this way:
var (
m1 = Manager1{Context: Context{Value: "testing1"}}
m2 = Manager2{Manager1: Manager1{Context: Context{Value: "testing2"}}}
m3 = Manager3{Manager2: Manager2{Manager1: Manager1{Context: Context{Value: "testing3"}}}}
)
Full example here: http://play.golang.org/p/13DOYKWN5C
Providing accessors or factory functions might make this a little nicer.