Source:
(1)Go Data Structures: Interfaces
(2)Go Interfaces
Digest:
1. Interface Values:
Languages with methods typically fall into one of two camps: prepare tables for all the method calls statically (as in C++ and Java), or do a method lookup at each call (as in Smalltalk and its many imitators, JavaScript and Python included) and add fancy caching to make that call efficient. Go sits halfway between the two: it has method tables but computes them at run time. ...
Interface values are represented as a two-word pair giving a pointer to information about the type stored in the interface and a pointer to the associated data. ...
The first word in the interface value points at what i call an interface table or itable (pronounced i-table; in the runtime sources, the C implementation name is Itab). The itables begins with some metadata about the types involved and then becomes a list of function pointers. Note that the itable corresponds to the interface type, not the dynamic type. ...
The second word in the interface value points at the actual data. In this case a copy of b. The assignment "var s Stringer = b" makes a copy of b rather than point at b for the same reason that "var c uint64 = b" makes a copy: if b later changes, s and c are supposed to have the original value, not the new one. Values stored in interfaces might be arbitrarily large, but only one word is dedicated to holding the value in the interface structure, so assignment allocates a chunk of memory on the heap and records the pointer in the one-word slot. (There's an obvious optimization when the value does fit in the slot; we'll get to that later.)
...
2. Computing the Itable:
Now we know what the itables look like, but where do they come from? Go's dynamic type conversions mean that it isn't reasonable for the compiler or linker to precompute all possible itables: there are too many (interface type, concrete type) pairs, and most won't be needed. Instead, the compiler generates a type description structure for each concrete type like "Binary" or "int" or "func(map[int]string)". Among other metadata, the type description structure contains a list of the methods implemented by that type. Similarly, the compiler generates a (different) type description structure for each interface type like "Stringer"; it too contains a method list. The interface runtime computes the itable by looking for each method listed in the interface type's method table in the concrete type's method table. The runtime caches the itable after generating it, so that this correspondence need only be computed once.
...
3. Memory Optimizations:
...
4. Method Lookup Performance:
...