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?
You're wrong about the nil
method, it's the interface
embedded into the struct Bar
that is nil
. When you use the methods of the interface, that is this interface that is called. The trick allow you to override an interface method with our own.
To understand the usage and goals of embedding interfaces
into struct
, the best example is in the sort
package:
type reverse struct {
// This embedded Interface permits Reverse to use the methods of
// another Interface implementation.
Interface
}
// Less returns the opposite of the embedded implementation's Less method.
func (r reverse) Less(i, j int) bool {
return r.Interface.Less(j, i)
}
// Reverse returns the reverse order for data.
func Reverse(data Interface) Interface {
return &reverse{data}
}