Go Interfaces
Internal
Overview
Go has an unique interface system that allows modeling behavior rather than modeling types. An interface declares the behavior of a type. The availability of interfaces in the language allows for polymorphism.
Types may implement interfaces. Pointer types may implement interfaces. These cases are not always identical.
There is no need to declare that an interface is implemented, the compiler does the work of determining whether the values of a type satisfy an interface. Is this a good thing? As far as I can tell, I can't say whether a specific type implements an interface, short of browsing all methods in the package, looking for receivers of that specific type. This does not help code readability. So, if a type implements the methods of an interface, a value of the type can be stored in a value of that interface type and the compiler ensure the assignments are correct at compile-time.
If a method call is made against an interface value, the equivalent method of the stored user-defined value - whatever that happens to be at the moment - it is executed. This is an expression of the polymorphic nature of the language. The user-defined type is often called a concrete type.
Interfaces in Go tend to be small, exposing only a few functions, or just one function.
TODO
- Can only structs be interfaces, or there are other things that can be interfaces? "Go in Action" mentions named types.
- It seems that the values and pointers are interchangeable when used as interface - we can pass a value or a pointer when we invoke an interface method and the compiler will be fine.
- Go_Concepts_-_Functions#Receivers_and_Interfaces
- Go_Concepts_-_Functions#Receiver_Best_Practice
Method Sets
A type may have a method set associated with it.
There are two kinds of method sets: interface method sets and user-defined types method sets.
If a type does not have any of the previously mentioned method sets, it is said to have an empty method set.
In a method set, each method must have a unique non-blank method name.
The language specification defines the method sets here: https://golang.org/ref/spec#Method_sets.
Interface Type
An interface type is a user-defined type whose only purpose is to introduce a method set (the interface method set) and to give it a name.
The interface type declaration starts with the type keyword, to indicated that this is a user-defined type, followed by the interface name and the keyword interface, followed by the interface method set declaration between curly braces. The interface method set declaration consists in a list of method names followed by their signatures. Unlike in the struct's case, we don't define fields.
type MyInterface interface { <function-name-1><function-signature-1> <function-name-2><function-signature-2> ... }
Example:
type A interface { m1(i int) int m2(s string) string }
Interface Name Convention
If the interface type contains only one method, the name of the interface starts with name of the method and ends with the er suffix.
When multiple methods are declared within an interface type, the name of the interface should relate to its general behavior.
Interface Method Set
An interface method set is a list of method declarations (function name and signature), specified in the interface type declaration. The interface type declaration has the only purpose of defining the method set and giving it a name - the interface name.
The method set only contains method names and signatures, so the interface type does not define how the behavior is implemented, but just the behavior's contract. The interfaces allow us to hide the incidental details of the implementation. The behavior implementation details are contained by user-defined types, via methods.
Also, the method signatures from the interface method set are independent on whether the corresponding methods from the concrete types are declared with value or pointer receivers.
Interface
The method set of an interface type it is said to be the type's interface.
User-Defined Type Method Set
Method Set associated with the Values of a Type
The method set associated with the values of a type consists of all methods declared with a value receiver of that type.
The specification defines the method set of a type as the method set associated with the values of the type.
Method Set associated with the Pointers of a Type
The method set associated with the pointers of a type T consists of both all methods declared with a pointer receiver *T and with a value receiver T.
The method set associated with the pointers of a type always includes the method set associated with the values of the type.
Why the Difference between Method Sets associated with Values and Pointers
It is not always possible to get the address of a value, but it is always possible to get the value an address points to.
TODO more, page 130
When does a Type/Pointer Type Implement an Interface?
Note that notion of a type implementing an interface and its corresponding pointer type implementing the same interface both have sense, and are different.
According to the language specification, a user-defined type T implements an interface if its method set (all methods declared with a value receiver of type T) is a superset of the interface.
A pointer type implements an interface when the method set associated with pointers to that type is a superset of the interface. As per User-Defined Type Method Set section, the method set associated with the pointer of a type include the method set associated with the value of the type. A logical conclusion is that if a pointer type implements the interface, then its base type also implement the interface, but the reverse is not always true.
For more details about value and pointer receivers, see
For more details about pointer types see:
Example
The example that follows shows how a type B implements interface A:
// // The interface A declares an interface method set containing a single method m() // type A interface { m() } // // At this point, the type B is not yet linked to the interface A in any way. // Its method set is empty, so it does not implement interface A, it does not // implement any interface. // type B struct { i int } // // We make B implement interface A by declaring B as a value receiver for m(). // This means B's method set includes now m() and it is a superset of A's method // set. // func (b B) m() { // simply reports the value of i fmt.Println(b.i) } ... // // B now implements A, so B instances can be assigned to A variables // var a A a = B{1} a.m() ...
Note that if in the example above we declare
func (b *B) m() { ... }
then the a = B{1} assignment would fail at compile time with:
./main.go:23: cannot use B literal (type B) as type A in assignment: B does not implement A (m method has pointer receiver)
This is because the type B does not implement the interface, because m() is not in the type B method set.
However, if we try
var a A a = &B{1} a.m()
it works, because the pointer type B does implement the interface.
Passing Interfaces to Functions
Interfaces can be used as arguments to functions. Passing an interface instance insures that the function body can rely on the fact the interface methods are available on the passed instance.
With the example above, we declare a function f() that expects an interface A and we pass a B instance when invoking the function:
... func f(a A) { a.m() } ... b := B{1} f(b) ...
Note that the B instance can be passed by value (as in the example above) or by reference (as in the example below). Both cases work:
... b := B{1} f(&b) ...
or (same thing)
... bPtr := new(B) f(bPtr) ...
TODO further discussion on the merits of passing the interface by value and by reference.
Interfaces as Fields
Interfaces can be used as fields.
Implementation Details
An interface variable is a two-word data structure.
The first word contains a pointer to an internal table called iTable, which contains type information about the stored value: the type of the value that has been stored (the concrete type) and a list of methods associated with the value.
The second word is a reference to the stored value.
The combination of type information and pointer binds the relationship between the two values.
More details "Go in Action" page 125.