Holding any value and checking its type

An interface lists the methods a type must have. What if the list is empty? An empty list asks for nothing, so every type fits. This empty interface is written interface{}, and Go has a shorter name for it: any. A variable of type any can hold a value of any type.

package main

import "fmt"

func main() {
	var x any = 42
	fmt.Println(x)
	x = "hello"
	fmt.Println(x)

	values := []any{7, "go", true, 2.5}
	fmt.Println(values)
}

Output:

42
hello
[7 go true 2.5]

x held an int and then a string. The slice holds four values of four different types. This is also how fmt.Println can print anything: its parameters have the type any.

But any has a cost. Go only knows that x is "some value", so it does not let you do much with it. Adding a number does not compile, even when the value inside is an int:

package main

import "fmt"

func main() {
	var x any = 10
	fmt.Println(x + 1)
}

Output:

# command-line-arguments
./main.go:7:14: invalid operation: x + 1 (mismatched types any and int)

Type assertions

To get the real value back, you use a type assertion: x.(int) means "I expect an int in x, give it to me as an int."

var x any = 10
n := x.(int)
fmt.Println(n + 1)

Output:

11

If you are wrong about the type, the program crashes:

var x any = 10
s := x.(string)
fmt.Println(s)

Output:

panic: interface conversion: interface {} is int, not string

The message uses the long name of any, which is interface {}. When you are not sure of the type, use the comma-ok form. It returns a second value, ok, that tells you if the assertion worked. When it did not, the first value is the zero value of the type, and nothing crashes:

var x any = 10
s, ok := x.(string)
fmt.Println(s == "", ok)
n, ok := x.(int)
fmt.Println(n, ok)

Output:

true false
10 true

Type switches

To handle several possible types, a chain of comma-ok checks gets long. A type switch does it in one statement:

package main

import "fmt"

func describe(x any) string {
	switch v := x.(type) {
	case int:
		return fmt.Sprintf("int, doubled %d", v*2)
	case string:
		return fmt.Sprintf("string of length %d", len(v))
	case bool:
		return fmt.Sprintf("bool, opposite %v", !v)
	default:
		return fmt.Sprintf("something else: %T", v)
	}
}

func main() {
	values := []any{7, "go", true, 2.5}
	for _, v := range values {
		fmt.Println(describe(v))
	}
}

Output:

int, doubled 14
string of length 2
bool, opposite false
something else: float64

x.(type) can only be written inside a switch. Go checks the cases from top to bottom and runs the first one that matches the type of the value in x. Inside each case, v already has that case's type: in case int, v is an int, so v*2 works. In case string, v is a string, so len(v) works. In default, v is still any, so the program prints its type with %T.

Use any only when you really cannot know the type ahead of time. A specific type, or an interface with methods, lets Go check your code before it runs. The next section, Generics, shows another way to write code that works for many types without giving up those checks.

In the exercise, finish describe with a type switch that handles float64, string and bool values.