Skip to main content
An empty interface in Go is an interface type that specifies zero methods. Because interface satisfaction in Go is implicit, and every type implements at least zero methods, the empty interface is satisfied by any value of any type. It acts as the universal container type within Go’s statically typed system.

Syntax and Aliasing

The empty interface is declared using empty curly braces. As of Go 1.18, the predeclared identifier any is an exact type alias for interface{}.

Internal Representation

At the runtime level, an empty interface is not simply a void pointer. It is represented by a two-word data structure (internally known as eface in the Go runtime):
  1. _type: A pointer to a runtime data structure containing the dynamic type information of the stored value.
  2. data: A pointer to the actual underlying dynamic value.
Because the data field is a pointer, assigning a value type (like an int or a struct) to an empty interface often forces the Go compiler to allocate memory on the heap to store the value, allowing the interface to hold a pointer to that memory.

Static vs. Dynamic Typing

When a variable is declared as an empty interface, its static type is always interface{}. However, it possesses a dynamic type and a dynamic value based on what is assigned to it.
Because the static type dictates what operations are permitted at compile time, you cannot directly invoke methods or perform operations (like arithmetic) on an empty interface, even if the underlying dynamic type supports them.

Extracting Values

To access the underlying dynamic value and restore its static type, you must use a type assertion or a type switch. Type Assertion A type assertion extracts the value by explicitly checking the dynamic type at runtime. It returns the underlying value and a boolean indicating success.
Type Switch A type switch permits branching logic based on the dynamic type of the empty interface. The .(type) syntax is exclusively available within a switch statement.

Nil Behavior

An empty interface is strictly evaluated as nil only if both its dynamic type and dynamic value are nil. If an interface holds a typed nil pointer, the interface itself is not nil because its _type field is populated.
Tired of Poor Go Skills? Fix That With Deep Grasping!Learn More