Go Generics: Everything You Need To Know

Mahfuz Moon
3 min readMar 9, 2023

--

Go, a popular programming language, recently introduced support for generics in its latest release, version 1.17. Generics are a programming concept that allow for the creation of functions and data structures that can work with multiple types, without having to write separate versions for each type. This enables developers to write more reusable and flexible code. In this article, we will cover the basics of Go generics, including syntax, use cases, and limitations.

In essence, generics are a way to write code that can work with any type of data, instead of being tied to a specific type. For instance, if we want to write a function that takes an array of integers and returns the sum of all its elements, we could write a specific function for integers. However, if we want to sum an array of floats, we would have to write a separate function that does exactly the same thing but takes an array of floats instead of integers. This can lead to code duplication and maintenance issues.

Generics address this problem. With generics, we can write a function that takes any type of array, and the function will work with that array regardless of its type. Here’s an example:

r
func sum[T any](arr []T) T {
total := arr[0]
for _, v := range arr[1:] {
total += v
}
return total
}

As you can see, we replaced the specific type int with a type parameter T, which can be any type that satisfies the constraint any. The function still works the same way, but now it can handle arrays of any type.

The syntax for generics in Go is quite different from other programming languages that support generics, such as Java or C#. In Go, generics are implemented using type parameters, which are specified inside square brackets []. Here's an example of a generic function that takes a slice of any type and returns the first element:

r
func first[T any](s []T) T {
return s[0]
}

The type parameter T is defined inside the square brackets, and it is followed by the any keyword, which specifies that T can be any type.

Go also supports type constraints, which allow you to restrict the types that can be used with a generic function. For example, you may want to write a function that only works with types that can be compared using the == operator. Here's an example of a generic function that takes two values of any type and returns true if they are equal:

css
func equal[T comparable](a, b T) bool {
return a == b
}

The type parameter T is followed by the comparable keyword, which specifies that T must be a comparable type. This ensures that the == operator can be used with T.

Generics can also be used with data structures, such as arrays, slices, maps, and structs. Here’s an example of a generic struct that represents a point in 2D space:

r
type Point[T any] struct {
X, Y T
}

The type parameter T is used to specify the type of the X and Y fields. This allows us to create points of any type, such as integers, floats, or strings.

In conclusion, Go generics provide a powerful new tool for developers to write more reusable and flexible code. They allow for the creation of functions and data structures that can work with multiple types, without having to write separate versions for each type. While the syntax may be unfamiliar to some, the benefits of using generics in Go are clear.

--

--

Mahfuz Moon

I am a professional writer.I have been working for signature mind institute for the last 5 years.