Go interface using `var` keyword?

459 Views Asked by At

I am referring to the code in this link: Interfaces in Golang

Under the "Type assertions" section, the first piece of code has a line like so in the main function: var val interface {} = "Geeks for Geeks"

enter image description here

I did not understand this syntax. Usually, we create an interface type at the package level like

type some_interface interface {
    // methods
}

and then create a variable of this interface type var s some_interface for use. What does this line actually do? Is it an "anonymous interface" (like anonymous struct). What is the use of declaring an interface using this method?

Thanks in advance

1

There are 1 best solutions below

0
AudioBubble On

It is an empty interface, basically any type.

The empty interface

The interface type that specifies zero methods is known as the empty interface:

interface{}

An empty interface may hold values of any type. (Every type implements at least zero methods.)

Empty interfaces are used by code that handles values of unknown type. For example, fmt.Print takes any number of arguments of type interface{}.