Skip to content

The empty interface

The empty interface interface{} (or any in Go 1.18+) has zero methods. Therefore every type satisfies it. It is used when a function can accept values of any type, similar to any in TypeScript. However, to use the underlying value, you must perform a type assertion or type switch.

Accept any value and print it using fmt.Println (which already takes any).

package main
import "fmt"
func describe(i interface{}) {
fmt.Printf("Type = %T, Value = %v\n", i, i)
}
func main() {
describe(42)
describe("hello")
describe(3.14)
}
Terminal window
Type = int, Value = 42
Type = string, Value = hello
Type = float64, Value = 3.14