I'm working through learning Go, and I've come across embedding Interfaces into structs in Go.
I understand the joys of Interfaces and their implementations, but I'm confused as the reasoning for the current execution of embedding one inside a struct.
When I embed an interface in a struct, the struct gains the methodset of the Interface and can now be used as a value of an Interface type variable, eg:
type Foo interface {
SetBaz(baz)
GetBaz() baz
}
type Bar struct {
Foo
}
So now we have a struct type Bar
which embeds Foo
. Because Bar
embeds Foo
, Bar
now satisfies any receivers or arguments that require type Foo
, even though Bar
hasn't even defined them.
Trying to call Bar.GetBaz()
causes the runtime error: panic: runtime error: invalid memory address or nil pointer dereference
.
Why does Go define nil methods on a struct that embeds an interface instead of explicitly requiring those methods to be defined through the compiler?