7.1 Interfaces as Contracts
All the types we’ve looked at so far have been concrete types.
A concrete
type specifies the exact representation of its values and exposes the
intrinsic operations of that representation, such as arithmetic for
numbers, or indexing, append, and range for slices. A concrete
type may also provide additional behaviors through its methods.
When you have a value of a concrete type, you know exactly what it
is and what you can do with it.
There is another kind of type in Go called an interface type. An interface is an abstract type. It doesn’t expose the representation or internal structure of its values, or the set of basic operations they support; it reveals only some of their methods. When you have a value of ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access