-
Book Overview & Buying
-
Table Of Contents
Go Recipes for Developers
By :
Go uses “structural typing.” If a type T defines all the methods of an interface I, then T implements I. This causes some confusion among developers who are well-versed in languages that use nominative typing, such as Java, where you explicitly have to name the constituent types.
Go interfaces are simply method sets. When a data type defines a set of methods, it also automatically implements all interfaces that contain a subset of its methods. For instance, if data type A defines a func (A) F() method, then A also implements the interface { func F() } and interface{} interfaces. If interface A is a subset of interface B, then a data type implementing interface B can be used wherever A is needed.
An interface can be used as a “specification,” or like a “contract” that defines certain functions an implementation should satisfy.
Define an interface or a set of interfaces...