Go: The empty interface

The empty interface in Go corresponds to a void pointer in C or an Object reference in Java. It specifies zero methods:

interface{}

A variable of empty interface type can hold values of any type since every type implements at least zero methods:

var a interface{}

a = 24
fmt.Printf("[%v, %T]\n", a, a) // "[24, int]"

a = &Point{1, 2}
fmt.Printf("[%v, %T]\n", a, a) // "[(1,2), *main.Point]"

The fmt.Println function is a chief example from the standard library. It takes any number of arguments of any type:

func Println(a ...interface{}) (n int, err error)

Comments

Be the first to comment!