Empty Interface{}

Learn via video courses
Topics Covered

Overview

The Go programming language lacks classes and inheritance to implement object orientation. To accomplish this, Go provides an interface, which is an abstract type. The interface is a custom type in Go that is used to specify a set of one or more method signatures. As it is abstract, you cannot create an instance of it. However, you are permitted to create an interface type variable that can be assigned with a concrete type value that contains the methods required by the interface. In other words, the interface is both a collection of methods and a custom type.

Empty Interface

Interfaces, as we know, are used to store a set of methods that do not have an implementation. But In the Go language, we can also create interfaces with no methods, which are referred to as empty interfaces. In short, an interface that has zero methods is called an empty interface. Here is an example of an empty interface:

Variable of Empty Interface

In the Go programming language, we are allowed to create variables of the empty interface type. Here is an example of the variable of the empty interface type.

Example

Output

Explanation

In the above example, we created a variable of the empty interface type. We get nil as output when we print the value of the variable.

Pass Function Variable of Any Type

As we know in functions, when values are passed to a function's parameters, the function definition requires that the parameters' data type be specified. On the other hand, we are free to pass any type of data as a parameter through an empty interface. Let us understand it through an example.

Example

Output

Explanation

We used an empty interface a as the parameter to the PrintValue() function in the above example, and the same function now works for any type of parameter, such as string, integer, boolean, and so on.

Pass Function Variable - Any Number of Arguments

As we have seen previously, we can pass parameters of any data type through an empty interface. In addition, we can also pass any number of arguments to the function definition by using an empty interface. Let us understand it through an example.

Example

Output

Explanation

We used an empty interface a as the parameter to the PrintValue() function in the given example and the same function (PrintValue(a... interface{}) now works for any number of parameters of any type.

Conclusion

  • Go language lacks classes and inheritance to implement object orientation. To accomplish this, Go provides an interface, which is an abstract type.
  • The interface is a custom type in Go that is used to specify a set of one or more method signatures.
  • The empty interface is the interface type that specifies zero methods. It is represented as interface{}.
  • In the Go language, we are allowed to create variables of the empty interface type.
  • In the Go language, We are free to pass any type of data as a parameter through an empty interface.
  • In the Go language, We can also pass any number of arguments to the function definition by using an empty interface.