Skip to main content

How to Use the any Type in Go

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
func <name>(<value> any) {
// <value> can hold a value of any type
}
PartWhat it does
anyA 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 run compiles and runs a single file. This guide uses Go 1.27.4.

Step 1: Write the Code​

Create a file named main.go:

main.go, which defines describe(value any) printing the value and its type with %v and %T, and calls it with 42, "hello", 3.5 and a slice of two strings.

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​

Terminal
go run main.go
Output
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.

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:

Type assertion
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​

MistakeWhat happensDo this instead
Doing arithmetic on an any, as in value + 1invalid operation: value + 1 (mismatched types any and untyped int)Assert the type first
Asserting without ok, as in value.(int), on a stringA run-time panicUse the two-value form, n, ok := value.(int)
Using any where one type would doThe compiler can no longer check your typesUse the real type

Troubleshooting​

SymptomLikely causeFix
panic: interface conversion: interface {} is string, not intA one-value type assertion met another typeUse n, ok := value.(int)
invalid operation: … (mismatched types any and …)The value's type isn't known to the compilerAssert 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​