Contents
Dig101: dig more, simplify more, know more
Runtime layout behind Go’s interface values — what “everything is an interface” looks like in memory.
An interface is a set of method signatures. It gives you contracts, abstractions, and constraints on implementations.
Values with no methods are the empty interface interface{}; in that sense every concrete value can live in an interface slot. (Before generics, interfaces were the usual stand-in for parametric code.)
Russ Cox on why interfaces matter:
From a language-design perspective, Go’s interfaces are static, checked at compile time, and dynamic when needed. If I could export one Go feature to other languages, it would be interfaces.
Go Data Structures: Interfaces
How is that represented in memory? How does duck typing work? What happens on a type assertion? Those are the questions below.
0x01 Same layout?
Two common ways to declare an interface — do they share the same runtime representation?
1 | // style 1 |
No. They are different structs.
With GDB ptype on the variables:
1 | // empty interface |
Go uses two interface representations; both are two machine words wide.
eface
Empty interface — no method set.
Stores concrete _type and data.

iface
Non-empty interface.
The itab field is richer than bare _type: it links concrete type, interface type, and method dispatch.

0x02 Conversions between types
When you assign into an interface, what fills those words?
1 | type Binary uint64 |
At b = i, the runtime calls something like convT64 from runtime/iface.go.
There are many convXXX helpers — what are they for?
convXXX naming
convFrom2To means convert from From to To.
From/To can be (see cmd/compile/internal/types/type.go:Tie):
- E (eface)
- I (iface)
- T (concrete Type)
The set looks huge until you read specialize convT2x, don’t alloc for zero vals.
Original convT2{I,E} and convI2I
Initially: convT2{I,E} and convI2I.
They allocate (newobject) and copy (typedmemmove). convI2I also uses getitab (type assertions below).
The compiler (walkexpr) optimizes before calling them:
Fewer copies — pointer-shaped types or values that fit in one word can sit directly in
data(main win).Pointer-shaped: ptr, chan, map, func,
unsafe.Pointer. With type/itab stored, you only copy two words.Fewer allocations — zero
bool/bytecan usezerobase/staticbytes; readonly globals; stack temporaries under 1 KiB that do not escape can be stored as{type/itab, &value}.iface → eface — drop everything in
itabexcept_type:
1 | tmp = i.itab |
Specialized convXXX paths
Further tweaks:
Must allocated memory be zeroed? Pointer-shaped types need a clean slab; pointer-free types can be overwritten in place. On 32-bit, skipping
convT64when the value fits in one word keeps the store atomic.Cheaper copy — for
int,string,slice, etc.,*(*Type)(x) = valinstead of a generic memmove.Write barrier? Pointer-shaped
convT2{E,I}usetypedmemmove(GC-aware). Pointer-freeconvT2{E,I}noptruse plainmemmove.
walkexpr picks the function:
1 | // cmd/compile/internal/gc/walk.go:walkexpr |
| fnname | From | Store value by address (needsaddr) |
|---|---|---|
| convI2I | iface | no |
| convT{16,32,64} | integer scalar, no pointers, align ≤ word | no |
| convTstring | string | no |
| convTslice | slice | no |
| convT2E | Type | yes |
| convT2Enoptr | pointer-free Type | yes |
| convT2I | Type | yes |
| convT2Inoptr | pointer-free Type | yes |
No
convE2EorconvE2I.needsaddr: non-pointer types larger than 64 bits or unknown size use the address of the value indata.
0x03 Type assertions
Assertions dynamically check the concrete type. Success yields a typed value for method calls.
Implementations need not declare interfaces explicitly — so how does
1 | type Binary uint64 |
know Binary implements Stringer?
The runtime calls assertE2I2:
assertE2I:v := eface1.(iface1)assertE2I2:v, ok := eface1.(iface1)
The single-value form panics on failure.
Internally this hits the global itabTable mapping (interface type, concrete type) → itab.
1 | const itabInitSize = 512 |
Lookup
getitab(inter, typ, canfail):
- Lock-free read of
itabTable; return if found - Else lock, search again
- Else build
itab, insert, unlock - On mismatch, panic or return
ok == falsepercanfail
itabTable.find(inter, typ) for lookup; itabAdd(m) for insert.
Insert / resize
Before insert,
m.funis filled from the(m.inter, m._type)pair;m.fun[0] == 0means “does not implement”.When
countexceeds 75% of capacity, grow (≥2×, aligned); swapping tables is atomic.Probe from a hash of interface + concrete type into the open-address table (same family as map insertion).
Open addressing: h(i) = h0 + i*(i+1)/2 mod 2^k via itabTable.add(m).
Matching cost
Naive check: interface has ni methods, concrete type nt — double loop is O(ni*nt).
Go sorts itab.inter.mhdr and the concrete method list so one pass is O(ni+nt), then caches in the hash table.
Sketch from runtime/iface.go:init():
1 | j:=0 |
Summary
efacevsiface, both two-word values- Conversions specialize on allocation, copy, and write barriers
- Assertions reuse sorted method lists plus a global
itabcache
See also: InterfaceSlice — why you cannot convert []Concrete to []interface{} directly.
Exercise: why does this assignment not call convT64?
1 | var b Stringer = Binary(1) |
Answer in a follow-up post.
Sample code: NewbMiao/Dig101-Go
Questions? Leave a comment below or email: newbvirgil@gmail.com![]()
