Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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{}.
var val1 interface{}
var val2 any // Identical to interface{} at compile time

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.
// Conceptual representation of the runtime eface struct
type eface struct {
    _type *_type
    data  unsafe.Pointer
}
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.
var i interface{} 

i = 42          // Static type: interface{}, Dynamic type: int, Dynamic value: 42
i = "hello"     // Static type: interface{}, Dynamic type: string, Dynamic value: "hello"
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.
// Syntax: value, ok := interfaceVariable.(TargetType)
var i interface{} = "golang"

str, ok := i.(string)
if ok {
    // str is statically typed as string
}
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.
var i interface{} = 3.14

switch v := i.(type) {
case int:
    // v is strictly typed as int
case float64:
    // v is strictly typed as float64
default:
    // v retains the static type interface{}
}

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.
var i interface{} 
// i == nil is true (type = nil, data = nil)

var ptr *int = nil
i = ptr
// i == nil is FALSE (type = *int, data = nil)
Master Go with Deep Grasping Methodology!Learn More