Empty interface in Golang

Earlier, we discussed the interfaces in Golang and how we can use them in our programs. Interfaces contain a set of method declarations without their implementations, but we can also have interfaces with no method declarations as well, we call them empty interfaces.

An empty interface in Golang is an interface without any method declarations.

Syntax of an empty interface
type interface_name interface{
}

An empty interface doesn’t contain any method declaration and can accept values of any type. In other words, we can say that every type implements an empty interface.

Zero value of an empty interface

What is the zero value of an empty interface? This means what will be chosen as the default value for an empty interface if we don’t assign any value to it.

The zero value of an empty interface is nil.

package main

import "fmt"

type Shape interface {
}

func main() {
	var shape Shape // making an interface's variable

	fmt.Println(shape)
}

Output –

<nil>
Where can we use the empty interfaces?

Sometimes, there might be scenarios where we accept arguments in a function but are unsure about the type that can come through it. In this case, we can use an empty interface as an argument, and the function can now accept any type as an argument.

package main

import "fmt"

// empty interface
type Value interface {
}

// Empty interface is treated as the function argument
func show(v Value) {
	fmt.Println(v)
}

func main() {

	a := 5
	str := "codekru"

	show(a)   // passing integer type value
	show(str) // passing string type value
}

Output –

5
codekru
So, what’s the catch here?

It seems too good to be true that we can pass any type in a function’s argument and use it inside the function.

Actually, we can only use the functions which accept an empty interface in their arguments. We cannot use such functions which accept some specific type like we cannot use the strings package functions because they accept a string type in their arguments.

Pass Empty interface variadic arguments in a variadic function

We can also use an empty interface in a variadic function, and the function would be able to accept as many arguments as possible and of any type.

package main

import (
	"fmt"
)

// empty interface
type Value interface {
}

// Empty interface is treated as the method argument
func show(v ...Value) {
	fmt.Println(v)
}

func main() {

	a := 5
	str := "codekru"

	show(a, str) // passing multiple arguments
}

Output –

[5 codekru]

Here we can use the Println() function because it also accepts an empty interface with variadic arguments.

Below is the implementation of the Println() function
func Println(a ...interface{}) (n int, err error) {
	return Fprintln(os.Stdout, a...)
}

We hope that you liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Related Article –
Liked the article? Share this on

Leave a Comment

Your email address will not be published.