Empty Interface Type in Go Programming Language
The type interface{} is known as the empty interface, and it is used to accept values of any type. The empty interface doesn't have any methods that are required to satisfy it, and so every type satisfies it.
Example
package main
import "fmt"
func printType(i interface{}) {
fmt.Println(i)
}
func main() {
var manyType interface{}
manyType = 100
fmt.Println(manyType)
manyType = 200.50
fmt.Println(manyType)
manyType = "Germany"
fmt.Println(manyType)
printType("Go programming language")
var countries = []string{"india", "japan", "canada", "australia", "russia"}
printType(countries)
var employee = map[string]int{"Mark": 10, "Sandy": 20}
printType(employee)
country := [3]string{"Japan", "Australia", "Germany"}
printType(country)
}
The manyType variable is declared to be of the type interface{} and it is able to be assigned values of different types. The printType() function takes a parameter of the type interface{}, hence this function can take the values of any valid type.
Output
100
200.5
Germany
Go programming language
[india japan canada australia russia]
map[Mark:10 Sandy:20]
[Japan Australia Germany]
Most Helpful This Week
How to declare Interface Type in Go Programming Language
Polymorphism in Go Programming Language
Interfaces with similar methods in Go Programming Language
Defining a type that satisfies an interface in Go Programming Language
Interface Accepting Address of the Variable in Golang
Interface embedding another interface in Go Programming Language
Implementing Multiple Interfaces in Go Programming Language