any
any is a predeclared alias for interface{}. It means "a value of some type" in ordinary code, and "no constraint" on a type parameter. It does not add methods or runtime checks.
Detailed Explanation
Go 1.18 added any so the empty interface has a name that reads like English. var v any and var v interface{} are the same type. func Print(v any) is the same as func Print(v interface{}). Prefer any in new code; you will still see interface{} in older files and in error messages from old toolchains.
Two jobs, one word. As a variable type, any is a box. You assign a concrete value in, you assert it out. As a type-parameter constraint, any means the parameter may be instantiated with any type, and the function body may not assume methods or operators beyond those that work on every type. func First[T any](s []T) T does not box each element; T is the element type.
It is not TypeScript any. TypeScript any turns checking off. Go any is a real interface type. v + 1 does not compile when v is any. v.Foo() does not compile. You must assert or use reflect. The compiler is still there; it just does not know the concrete type.
It is not unknown either. TypeScript unknown forces a check before use. Go any also forces a check before you treat the value as a concrete type, but the empty interface has been around so long that people pass any through without asserting. That is legal and loses the benefit.
JSON and fmt. map[string]any is the usual decoded object. []any is a decoded array. fmt.Printf("%v", v) accepts any. These are the legitimate mixed-type APIs. Your domain functions should take User, io.Reader, or a type parameter instead.
What it costs. Same as the empty interface: boxing, assertions, and a []any that cannot be used as []T. In generics, any as a constraint is free — there is no box unless you assign T to an any variable.
Code Examples
package main
import "fmt"
func main() {
var v any = 3
n, ok := v.(int)
fmt.Println(n, ok)
}func First[T any](s []T) (T, bool) {
var zero T
if len(s) == 0 {
return zero, false
}
return s[0], true
}func decode() map[string]any {
return map[string]any{"n": 1.0, "ok": true}
}Common Gotchas
`any` does not disable the type checker
arithmetic and field access still require an assertion or a tighter type.
`[]User` is not `[]any`
copy elements if you need a mixed slice.
`any` and `interface{}` are aliases
converting between them is a no-op. Pick one spelling per file; `any` is the current spelling.
As a constraint, `any` forbids `==` unless you also need `comparable`
`func eq[T any](a, b T) bool { return a == b }` does not compile.