TL;DR
In Go methods aren't just magically inherited with subclassing as you would do in other languages like Python or Java. You can define interfaces and using embedding but you'll have to implement the methods you need for each type. Of course you can just call the method of the embedded from the outer method, however be careful that any changes will occur to the inner object and not the outer one.
From the docs:
There's an important way in which embedding differs from subclassing.
When we embed a type, the methods of that type become methods of the
outer type, but when they are invoked the receiver of the method is
the inner type, not the outer one. In our example, when the Read
method of a bufio.ReadWriter
is invoked, it has exactly the same
effect as the forwarding method written out above; the receiver is the
reader field of the ReadWriter
, not the ReadWriter
itself.
Some more info
Here's some references from the docs:
http://golang.org/doc/faq#Is_Go_an_object-oriented_language
Is Go an object-oriented language?
Yes and no. Although Go has types and methods and allows an
object-oriented style of programming, there is no type hierarchy. The
concept of “interface” in Go provides a different approach that we
believe is easy to use and in some ways more general. There are also
ways to embed types in other types to provide something analogous—but
not identical—to subclassing.
So, you can have interfaces that define what should be implemented in a type but you'll have to implement those methods for each type.
One convenience you have is Embedding:
http://golang.org/doc/effective_go.html#embedding
Go does not provide the typical, type-driven notion of subclassing,
but it does have the ability to “borrow” pieces of an implementation
by embedding types within a struct or interface.
Interface embedding is very simple. We've mentioned the io.Reader
and
io.Writer
interfaces before; here are their definitions.
type Reader interface {
Read(p []byte) (n int, err error)
}
type Writer interface {
Write(p []byte) (n int, err error)
}
The io package also exports several other interfaces that specify
objects that can implement several such methods. For instance, there
is io.ReadWriter
, an interface containing both Read
and Write
. We
could specify io.ReadWriter
by listing the two methods explicitly, but
it's easier and more evocative to embed the two interfaces to form the
new one, like this:
// ReadWriter is the interface that combines the Reader and Writer interfaces.
type ReadWriter interface {
Reader
Writer
}