Golang any type
last modified May 8, 2025
This tutorial explains how to use the any
built-in type in Go.
We'll cover type basics with practical examples of flexible type handling.
The any type is a predeclared interface type that represents the
set of all non-interface types. It's an alias for interface{}
introduced in Go 1.18 for better code readability.
In Go, any
is used when you need to work with values of unknown
type. It provides flexibility but requires type assertions or switches to
access the underlying concrete value.
Basic any type example
The simplest use of any
stores values of different types. This
example demonstrates basic usage of the any type.
Note: any can hold values of any type, similar to void* in C.
package main import "fmt" func main() { var a any a = 42 fmt.Printf("Type: %T, Value: %v\n", a, a) a = "hello" fmt.Printf("Type: %T, Value: %v\n", a, a) a = 3.14 fmt.Printf("Type: %T, Value: %v\n", a, a) }
The variable a
can hold different types of values. The %T
format verb shows the dynamic type stored in the any variable.
Type assertion with any
We can extract the concrete value from an any using type assertions. This example shows how to safely perform type assertions.
package main import "fmt" func main() { var val any = "gopher" // Safe type assertion with ok if s, ok := val.(string); ok { fmt.Println("It's a string:", s) } else { fmt.Println("Not a string") } val = 42 if i, ok := val.(int); ok { fmt.Println("It's an int:", i) } else { fmt.Println("Not an int") } }
The ok
boolean indicates whether the assertion succeeded. This
pattern prevents runtime panics from incorrect type assertions.
Type switches with any
Type switches provide a clean way to handle multiple possible types. This example demonstrates using type switches with any variables.
package main import "fmt" func printType(v any) { switch t := v.(type) { case int: fmt.Println("Integer:", t) case float64: fmt.Println("Float:", t) case string: fmt.Println("String:", t) default: fmt.Printf("Unknown type: %T\n", t) } } func main() { printType(42) printType(3.14) printType("hello") printType(true) }
The type switch checks the dynamic type of the any value. Each case handles a different possible type, with default catching all others.
Using any in function parameters
Any is useful for functions that need to accept multiple types. This example shows a function that processes different input types.
package main import "fmt" func processValue(v any) { switch val := v.(type) { case int: fmt.Println("Processing integer:", val*2) case string: fmt.Println("Processing string:", len(val)) case []int: fmt.Println("Processing slice:", len(val)) default: fmt.Println("Unsupported type") } } func main() { processValue(10) processValue("golang") processValue([]int{1, 2, 3}) processValue(3.14) }
The function handles integers, strings, and slices differently. Unsupported types fall through to the default case.
Any with JSON unmarshaling
Any is commonly used with JSON data of unknown structure. This example shows parsing JSON into an any variable.
package main import ( "encoding/json" "fmt" ) func main() { jsonData := `{"name":"Alice","age":30,"active":true,"scores":[90,85,95]}` var data any err := json.Unmarshal([]byte(jsonData), &data) if err != nil { panic(err) } // Type assert to map[string]any if m, ok := data.(map[string]any); ok { fmt.Println("Name:", m["name"]) fmt.Println("Age:", m["age"]) if scores, ok := m["scores"].([]any); ok { fmt.Println("Scores:") for _, s := range scores { fmt.Println("-", s) } } } }
JSON unmarshaling into any produces a hierarchy of maps and slices. Each level requires type assertions to access the concrete values.
Source
This tutorial covered the any
type in Go with practical examples
of flexible type handling and dynamic value processing.
Author
List all Golang tutorials.