$Search: dynamic type

Technically speaking, an empty interface (interface{}) is an interface with no methods.

What follows from that is that every type conforms to interface{}.

In practice, empty interface is Go’s version of object type in Java or C# in that it combines a type and its value.

Empty interface is effectively a dynamic type in a static language.

Empty interface is also a way to implement union types in Go.

Since every type conforms to interface{}, you can assign any value to a variable of interface{} type.

At that point, you can no longer tell what is the real type at compile time.

Zero value of empty interface is nil.

Basic example:

https://codeeval.dev/gist/03627f233489bf1171cc1488ff73f432

At compile time, when you have a variable whose type is interface (including empty interface) you don’t know what is the real, underlying type.

You can access underlying type at runtime using type assertion.

https://codeeval.dev/gist/87f4bb1c4f744040d73a55f239108c41

Type assertion

Type assertion allows you to check if empty interface value is of a given type.

For completness, you can use short version of type switch: v := iv.(int) (vs. v, ok := iv.(int)).

The difference is that the short version will panic if iv is not of the asserted type:

https://codeeval.dev/gist/c2b1c9a1ee37bf3edb9c81d2b7a15289