How to Use the any Type in Go
Most Go functions say exactly what type they take. Sometimes a function must accept anything, such as a printing helper or a value parsed from JSON; for that, Go has the type any.
Syntax
func <name>(<value> any) {
// <value> can hold a value of any type
}
| Part | What it does |
|---|---|
any | A type that every value fits into. |
<value> | Holds whatever was passed, together with that value's real type. |
<value>.(<Type>) | A type assertion: gets the value back as <Type> when that's what it holds. |
The Go specification: "For convenience, the predeclared type any is an alias for the empty interface". any and interface{} mean exactly the same thing.
The Example
The example writes a describe function that takes any and prints each value with its type, then calls it with a number, a string, a decimal and a slice.
Prerequisites
- Go.
go runcompiles and runs a single file. This guide uses Go 1.27.4.
Step 1: Write the Code
Create a file named main.go:

package main
import "fmt"
func describe(value any) {
fmt.Printf("%v is a %T\n", value, value)
}
func main() {
describe(42)
describe("hello")
describe(3.5)
describe([]string{"a", "b"})
}
%v prints a value in its default format, and %T prints its type.
Step 2: Run It
go run main.go
42 is a int
hello is a string
3.5 is a float64
[a b] is a []string
![A zsh terminal where go run main.go prints 42 is a int, hello is a string, 3.5 is a float64 and [a b] is a []string.](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/programming/go/2026/go-any-type-zsh.webp)
The same parameter held four different types, and each value kept its real type inside. The command is the same in zsh, bash and PowerShell.
Getting a Concrete Type Back
You can't do arithmetic or call string functions on an any directly; first ask for the type you expect:
if n, ok := value.(int); ok {
fmt.Println(n + 1)
}
ok is false when the value holds another type, so the code is safe either way.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Doing arithmetic on an any, as in value + 1 | invalid operation: value + 1 (mismatched types any and untyped int) | Assert the type first |
Asserting without ok, as in value.(int), on a string | A run-time panic | Use the two-value form, n, ok := value.(int) |
Using any where one type would do | The compiler can no longer check your types | Use the real type |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
panic: interface conversion: interface {} is string, not int | A one-value type assertion met another type | Use n, ok := value.(int) |
invalid operation: … (mismatched types any and …) | The value's type isn't known to the compiler | Assert it to a concrete type |
FAQ
Is any the same as the empty interface?
Yes. any is an alias for interface{}, added in Go 1.18; older code writes interface{}.
Is any used with generics?
Yes, as a constraint: func First[T any](items []T) T accepts a slice of any element type, while keeping that type known.
Conclusion
A parameter of type any accepts a value of any type and keeps its real type inside. Print it with %v and %T, and use a type assertion with ok to get a concrete value back.
Additional Resources
- Interface types, in the Go specification.
- Type assertions, in the Go specification.
