The empty interface

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

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

empty-interface.go

  1. package main
  2. import "fmt"
  3. func main() {
  4. var i interface{}
  5. describe(i)
  6. i = 42
  7. describe(i)
  8. i = "hello"
  9. describe(i)
  10. }
  11. func describe(i interface{}) {
  12. fmt.Printf("(%v, %T)\n", i, i)
  13. }