Generics in Golang


Golang, also known as Go, is a popular open-source programming language known for its simplicity and concurrency. However, it lacked one important feature that other modern programming languages have, which is generics. Generics allow a programmer to write flexible, reusable code that works with different types without sacrificing type safety. In this article, we'll explore the introduction, syntax, and examples of generics in Golang.

Introduction to Generics in Golang

Generics are a way to write code that can work with any type, rather than being restricted to a specific type. In other words, generics allow you to write a function or data structure that can operate on different types, making it more flexible and reusable.

Prior to Go 1.18, Golang did not support generics. However, with the release of Go 1.18, Golang finally added support for generics. This has been a highly anticipated feature among Golang developers, and it brings Golang closer to modern programming languages that already have generics, such as Java and C#.

Syntax for Generics in Golang

The syntax for generics in Golang uses a new keyword, type, which is used to define a generic type parameter. Here's an example −

func swap[T any](x, y T) (T, T) {
   return y, x
}

In this example, T is the type parameter, and it is defined using the any keyword. The swap function takes two arguments, x and y, both of type T, and returns a tuple of two values of type T.

The any keyword is used to specify that any type can be used as the type parameter. However, you can also restrict the type parameter to a specific set of types using constraints. Here's an example −

Example

package main

import "fmt"

func find[T comparable](arr []T, val T) int {
   for i, v := range arr {
      if v == val {
         return i
      }
   }
   return -1
}

func main() {
   arr := []int{1, 2, 3, 4, 5}
   index := find(arr, 3)
   fmt.Println(index) // Output: 2

   arr2 := []string{"foo", "bar", "baz"}
   index2 := find(arr2, "bar")
   fmt.Println(index2) // Output: 1
}

Output

2
1

In this example, T is the type parameter, and it is restricted to types that are comparable. The find function takes an array of type []T and a value of type T, and returns the index of the first occurrence of val in the array, or -1 if val is not found.

Examples of Generics in Golang

Let's look at some examples of how to use generics in Golang.

Example 1: Swap Function

package main

import "fmt"

func swap[T any](x, y T) (T, T) {
   return y, x
}

func main() {
   a, b := 1, 2
   a, b = swap(a, b)
   fmt.Println(a, b) // Output: 2 1
   
   c, d := "hello", "world"
   c, d = swap(c, d)
   fmt.Println(c, d) // Output: world hello
}

Output

2 1
world hello

Example 2: Find Function

package main

import "fmt"

func find[T comparable](arr []T, val T) int {
   for i, v := range arr {
      if v == val {
         return i
      }
   }
   return -1
}

func main() {
   arr := []int{1, 2, 3, 4, 5}
   index := find(arr, 3)
   fmt.Println(index) // Output: 2
   
   arr2 := []string{"foo", "bar", "baz"}
   index2 := find(arr2, "bar")
   fmt.Println(index2) // Output: 1
}

Output

2
1

In this example, we define the find function using generics. The find function takes an array of type []T and a value of type T, and returns the index of the first occurrence of val in the array, or -1 if val is not found. We call the find function twice with different types, int and string, and it works as expected, returning the index of the value we were looking for.

Conclusion

Generics are a powerful feature that allows you to write flexible and reusable code in Golang. With the addition of generics in Go 1.18, Golang has become more competitive with other modern programming languages that already have generics, such as Java and C#. Generics are a great tool to have in your toolbox, and you should consider using them in your Golang projects.

Updated on: 18-Apr-2023

169 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements