Go’s ‘any’: The Type That Can Hold Anything!

Curious Inquirer
4 min readMar 15, 2023
Optimus Prime

Go (often referred to as Golang) is a programming language that was first introduced by Google in 2009. One of the most recent additions to the language is the “any” type, which was added in Go 1.18. This type is useful because it allows developers to write more flexible code that can work with a wider range of values.

The “any” type was added as an alias for the “interface{}” type for several reasons. One of the main reasons is that “any” is a more intuitive and descriptive name for this type. The term “interface{}” can be confusing for developers who are new to Go, especially those who are not familiar with the concept of interfaces in object-oriented programming. By using the term “any”, developers can immediately understand that this type can hold any value. It is a placeholder type that can represent any value at runtime. This means that you can assign any value to a variable of type “any” without encountering any type errors.

The “any” type in Go is similar to the “any” or “object” type in other languages. For example, in TypeScript, there is an “any” type that serves a similar purpose as the “interface{}” type in Go.

Example: Dynamic data handling

The “any” type is especially useful when working with dynamic data, such as JSON. In these cases, the structure of the data is not known in advance, so it is difficult to know what type to use when declaring variables. With the “any” type, you can simply declare a variable as “any” and assign it the data, regardless of its type.

Here’s an example that shows how the “any” type can be used to work with dynamic data:

package main

import (
"encoding/json"
"fmt"
)

func main() {
data := `{"name": "Rob", "age": 18}`

var obj any
err := json.Unmarshal([]byte(data), &obj)
if err != nil {
fmt.Println("Error:", err)
return
}

// obj now contains the parsed JSON data, but its type is "any"
// We can use a type assertion to access its properties:
m := obj.(map[string]any)
fmt.Println("Name:", m["name"])
fmt.Println("Age:", m["age"])
}

In this example, we parse some JSON data into a variable of type any named “obj”. Since we don’t know the structure of the JSON data in advance, we can’t declare a struct to hold it. Instead, we use the “any” type to hold the data.

To access the properties of the JSON object, we use a type assertion to convert the “any” type to a map[string]any. This allows us to access the “name” and “age” properties of the JSON data.

Example: Passing Any Type of Value to a Function

package main

import (
"fmt"
)

func printValue(value any) {
fmt.Println(value)
}

func main() {
printValue(42)
printValue("hello")
printValue(true)
}

In this example, we define a function printValue that takes a single argument of type any. This means that we can pass any type of value to the function, and it will be accepted without errors. We can then use the fmt.Println function to print the value to the console. In the main function, we call printValue three times with different types of values: an integer, a string, and a boolean.

Example: Using the Any Type in a Struct

package main

import (
"fmt"
)

type Person struct {
Name string
Age int
Extra any
}

func main() {
person := Person{
Name: "Alice",
Age: 30,
}

person.Extra = "some extra info"
fmt.Printf("%+v\n", person)

person.Extra = 3.14
fmt.Printf("%+v\n", person)
}

In this example, we define a Person struct that has three fields: Name, Age, and Extra. The Extra field is of type any, which means that it can hold any type of value. In the main function, we create a new Person struct and set the Extra field to a string value and then a floating-point value. We then use the fmt.Printf function to print the Person struct to the console, including the value of the Extra field.

Overall, the “any” type is a useful addition to Go because it allows developers to write more flexible code that can work with a wider range of values. It is especially useful when working with dynamic data that doesn’t have a known structure in advance. By using the “any” type, developers can write code that is more robust and easier to maintain, since it can handle a wider range of inputs without encountering type errors.

Lastly, the addition of the “any” type as an alias for “interface{}” also makes the language more accessible and easier to use for developers who are new to Go or who are coming from other programming languages.

--

--

Curious Inquirer

Software developer - creating elegant, user-friendly software. At times, delves into writing on the topic of life.