1

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

KeyShoe
  • 84
  • 7
  • 2
    Please do not post images of text. – Volker Aug 23 '22 at 06:06
  • The statement `var val interface {} = "Geeks for Geeks"` uses a *"type literal"*, rather than a declared type, to define the type of the variable `val`. You could just as well do `type E interface{}` and then `var val E = "Geeks for Geeks"` and the only difference would be that one has a named type and the other doesn't. But, since these are interface types, named/unnamed matters little if at all. – mkopriva Aug 23 '22 at 06:40

1 Answers1

2

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{}.