-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Go Recipes for Developers
By :
Go uses type composition through embedding, and structural typing through the use of interfaces. Let’s start by examining what these mean.
When you embed an existing type into another, the methods and data fields defined for the embedded type become the methods and data fields of the embedding type. If you have worked with object-oriented languages, this may seem similar to class inheritance, but there is a crucial difference: if a class A is derived from a class B, then A is-a B, meaning wherever B is needed, you can substitute an instance of A. With composition, if A embeds B, A and B are distinct types, and you cannot use A where B is needed.
Tip
There is no type inheritance in Go. Go chooses composition over inheritance. The primary reason for this is the simplicity of combining components to build more complex ones. Most use cases of inheritance in object-oriented languages can be rearchitected using composition, interfaces, and structural typing...