The empty interface
Context
Section titled “Context”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.
Example
Section titled “Example”Accept any value and print it using fmt.Println (which already takes any).
Code example
Section titled “Code example”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)}Output
Section titled “Output”Type = int, Value = 42Type = string, Value = helloType = float64, Value = 3.14