Empty Interface

Sometimes we have no idea about the types that can be used by the code. Therefore, Go provides us with ‘empty interfaces’ that let us deal with these values. This lesson shows us how to work with them.

Introduction

The empty or minimal interface has no methods, and so it doesn’t make any demands at all.

type Any interface{}

Any variable, any type implements it (not only reference types that inherit from Object in Java/C#), and any or Any is a good name as an alias and abbreviation! (It is analogous to the class Object in Java and C#, the base class of all classes. So, Obj would also fit.) A variable of that interface type var val interface{} can through assignment receive a variable of any type.

This is illustrated in the program:

Get hands-on with 1200+ tech skills courses.