How to use interfaces

Back to Languages/go

An interface is two things: a set of methods and also a type.

Its methods

A core concept of Go's type system is that instead of designing our abstractions in terms of what data a type can hold, abstractions are designed in terms of what actions a type can execute.

The interface{} type

interface{} type is the empty interface. It has no methods, so all types implement the empty interface.

The interface value & method table

An interface value is constructed of two words of data

The method table or interface table, is some metadata about the types involved and a list of function pointers.

Pointers and Interfaces

An interface definition does not explicitly state whether an implementor should implement the interface using a pointer reciever or value reciever