Go Secret — Interface{}: Nil is not Nil
We’ll see how it can keep different types of values, look at what’s inside it, and find out how it checks if two things are the same.

In this section, we will delve deeper into the interface{}
type and gain a comprehensive understanding of how it operates.
Secret series:
Go Secret — Defer: What you know about DEFER in Go is not enough!
Go Secret — Interface{}: Let Me Tell You Another Thing (You’re here)
1. Interface actually holds 2 things, not one.
When you assign a variable of a specific type to an interface{} variable in Go, what actually happens is that Go forms a new structure. This new structure holds both the type information and the variable’s value.
Think of it like a simple container (though, in reality, it’s more complicated):
During runtime, this action is known as “boxing” the value, picture an interface{} structure somewhat like this:
type iface struct {
tab *itab
data unsafe.Pointer
}
Here, the ‘tab’ type descriptor is a tiny structure carrying information like the interface’s method set, the underlying type, and the methods of the underlying type that implement the interface.
type itab struct {
inter *interfacetype
_type *_type
hash uint32 // copy of _type.hash. Used for type switches.
_ [4]byte
fun [1]uintptr // variable sized. fun[0]==0 means _type does not implement inter.
}
The ‘data
’ pointer just points to where the variable’s actual value lives in memory.
When you want to take a value out of an interface{}
, Go “unboxes” it. It reads the type and pointer information, and then it forms a new variable of the right type based on this data.
All this is managed by the Go runtime.
“How did you find out about these inner workings?”