Skip to content
Related Articles
Open in App
Not now

Related Articles

Generics in Golang

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 13 Dec, 2022
Improve Article
Save Article

Generics are essentially template/boilerplate code written in a form to use on the Go with types that can be added later. The main aim of generics is to achieve greater flexibility in terms of writing code with the addition of fewer lines. The concept of Generics has existed for a long time now and has been a part of many programming languages, including Java, Python, and C#, to name a few. 
In the words of Alexander Stepanov, Generics are a way of creating an incremental source of catalogs of abstract code that can be either functions, algorithms, or data structures. As a part of the 1.18 release, Generics was introduced in Go for the first time.

Type Parameters in Go

Taking types and functions into account, Go brings Generics using a concept called Type Parameters. These types of parameters can be used with either Functions or Structs. Let’s take a deep dive into the implementation with a few examples. 

We will start by writing some boilerplate code:

Go




package main
     
func main() {
   
    var radius1 int = 8
    var radius2 float = 9.5
}

The above code declares two variables of type int and float in the main function, that contain the values of two radii of some circles. In the next step, we will declare a generic function that will take either of the inputs depending on its type and return to us the circle’s circumference. 

Go




func generic_circumference [r int | float ](radius r) {
c= 2*3.14*r
fmt.println("The circumference is: ",c)
}

So, our whole code will look something like this:

Go




package main
 
import "fmt"
 
func generic_circumference[r int | float32](radius r) {
 
    c := 2 * 3 * radius
    fmt.Println("The circumference is: ", c)
 
}
 
func main() {
    var radius1 int = 8
    var radius2 float32 = 9.5
 
    generic_circumference(radius1)
    generic_circumference(radius2)
}

Output:

The circumference is:  48
The circumference is:  57

The main differentiating factor in the above code is how we declare a variable r in a list and provide the list of types (int, flaot32) this variable can take depending on the arguments being passed during the function call. 

Parameterized Types in Go

In the above example, we saw how using generic functions allows us to indicate what types a function can accept. Through parameterized types, Go provides one other way to describe the types we want our function to accept. Based on the previous example, here’s what parameterization would look like:

Go




// Parameterized Types
type Radius interface {
    int64 | int8 | float64
}
 
func generic_circumference[R Radius](radius R){ 
    var c R
    c = 2 * 3 * radius
    fmt.Println("The circumference is: ", c)
}

In this approach, we first declare an interface named Radius, which holds the types we can pass to the function, which in the above example are: int64, int8, and float64. The next change is, how we declare our generic function. In the function declaration, using the square brackets, we pass in an instance of this Interface we declared above. 
R is used in the above example to refer to any of the types that the Radius interface supports. If we invoke generic_circumference with a float64 value, then R in the context of this function is a value with type float64, if we invoke the function with an int64, then R is int64, and so on.

These two examples using type parameters and parameterized types would hopefully give you an overview of what generics can do in Go. You can definitely experiment with different implementations based on your needs and requirements and can improve your code readability with the addition of this new feature.

Generics provide as effective tools to achieve greater levels of abstraction in your code and also improve re-usability. That being said, it is important to identify the proper use cases where these can be implemented in a manner that can increase efficiency. Generics are still an early concept in Go development and it would be interesting to see how fruitful its implementations turn out to be. 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!