What Is the Difference Between interface{} and any in Go

`any` is simply a predeclared alias for `interface{}` introduced in Go 1.18; they are functionally identical at runtime but `any` improves code readability by reducing visual noise.

any is simply a predeclared alias for interface{} introduced in Go 1.18; they are functionally identical at runtime but any improves code readability by reducing visual noise. You should use any in new code for better clarity, while existing codebases may still rely on interface{} for legacy reasons.

The Go compiler treats them as the exact same type. If you define a variable as interface{} and assign it to a variable of type any, no type conversion occurs because they are the same underlying type. The only difference is syntactic: any is shorter and explicitly signals to the reader that the type is intentionally generic, whereas interface{} can sometimes look like an empty interface definition.

Here is a practical example showing their interchangeability:

package main

import "fmt"

func main() {
    // Both variables hold the same underlying type
    var a interface{} = 42
    var b any = "hello"

    // They are interchangeable without conversion
    fmt.Printf("Type of a: %T, Value: %v\n", a, a)
    fmt.Printf("Type of b: %T, Value: %v\n", b, b)

    // You can assign one to the other directly
    a = b
    b = a

    // Type assertion works identically on both
    if s, ok := a.(string); ok {
        fmt.Println("String value:", s)
    }
}

You can also see this in function signatures. When writing a generic function or a utility that accepts "anything," any makes the intent clearer:

// Prefer this for new code
func PrintValue(v any) {
    fmt.Println(v)
}

// This is functionally identical but less readable
func PrintValueLegacy(v interface{}) {
    fmt.Println(v)
}

While any is preferred for new development, you will frequently encounter interface{} in older libraries and standard library code written before Go 1.18. You do not need to refactor existing interface{} usage to any unless you are already modifying the file, as the compiler handles them identically. The choice is purely stylistic and semantic, not technical.