Syntax and Aliasing
The empty interface is declared using empty curly braces. As of Go 1.18, the predeclared identifierany 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 aseface in the Go runtime):
_type: A pointer to a runtime data structure containing the dynamic type information of the stored value.data: A pointer to the actual underlying dynamic value.
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 alwaysinterface{}. However, it possesses a dynamic type and a dynamic value based on what is assigned to it.
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) syntax is exclusively available within a switch statement.
Nil Behavior
An empty interface is strictly evaluated asnil 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





