The Empty interface in Golang
1 min readNov 22, 2020
An empty interface can be used to hold any data and it can be a useful parameter since it can work with any type.
The interface type that specifies zero methods is known as the empty interface:
interface{}
Empty interfaces are used by code that handles values of unknown type. For example, fmt.Print
takes any number of arguments of type interface{}
.
package mainimport "fmt"func main() {
var i interface{}
describe(i)i = 42
describe(i)i = "hello"
describe(i)
}func describe(i interface{}) {
fmt.Printf("(%v, %T)\n", i, i)
}
Output:
(<nil>, <nil>)
(42, int)
(hello, string)
As we can see in the above example describe() method is called for different types(int and string). For more details of the internal implementation of interfaces refer to https://research.swtch.com/interfaces.