Generic was a very big issue throughout the Golang Developer's Community before the release of version 1.18 Go Programming Language. It was an issue in the community because developers were unable to use Generics in Go.
I know, it might sound unusual to you because maybe you don’t know what exactly Generics is. In this blog, I will be delivering the knowledge of Generics, How to Use it, and most importantly when to use it.
So let’s understand Generics first but before that, I have mentioned some prerequisites, so that you can be prepared with it and get a better understanding of the concept through this explanation blog.
Before proceeding, I would like to request you go through the concepts that I will be mentioning below to get proper clarity on the concepts explained in this blog. To understand generic first you need to be prepared with the basic concepts of Golang like Variables, Constants, Datatypes, I/O Format, Slices, Struct, Functions, Maps, Parameters, and Arguments. Then on the other hand you should have Golang installed on your system. Now, let us proceed toward understanding what exactly Generics is.
In simple words, Generic enables the creation of code with types that can be defined later and implemented as required. In other words, Generics in Go relates to the ability to create types or functions that are not type-specific and may be used with a variety of kinds.
As a result, you can create code that is more general and reusable and can be used for many other types. As an illustration, you could create a function that sorts an integer slice in ascending order.
For each type that you wished to sort, you would have to create a separate version of this function without generics (e.g. int, float64, string, etc.). As long as the type has a predetermined ordering, you can use generics to create a single version of the function that can sort slices of any type.
The above is an example of a generic function that sorts a slice of any type that implements the
sort.interface interface. Slices of any type that supports the sorting operation can be sorted using this function. Slices of texts or numbers are examples of interfaces.
Although they can be a potent tool for creating reusable, adaptable code in Go, generics can also be tricky to work with. Start with some simpler examples if you're unfamiliar with Go and generics, then progress to more sophisticated usage of generics. Let me provide a basic example to make it easier to grab the concept.
This function swaps the values of two pointers to integers that it is given as an argument by utilizing a temporary variable. You would call this function as follows to swap the values of the two variables x and y:
Because it may be used to swap the values of any two int variables, regardless of their names or values, therefore you can refer to this function as generic.
In this section, I will be explaining the limitations of Generics in Go. There are a few limitations that in my perspective should not be avoided when you are preparing yourself for Golang Programming. I have mentioned them below,
When writing code in Go that may be used with numerous kinds rather than just one type, generics can be a helpful tool. This can make it simpler for you to create more flexible, broad, and reusable code.
Here are a few scenarios in Go where you might think about using generics:
Generics may not always be the best option, of course. For instance, you might not need to utilize generics if you are working with a limited, fixed collection of types and you do not need to build general, reusable code. In general, before utilizing generics in your codebase, it's a good idea to think about if they will help you produce cleaner, more manageable code. Start with some simpler examples if you're unfamiliar with Go and generics, then progress to more sophisticated usage of generics. I hope this blog was helpful for your learning. I tried to cover most of the things related to generics in this blog, but keep in mind to be prepared with the essentials before beginning with this explanation blog on Golang. This blog will also contribute to the extension of your knowledge that you can optimize Go Programing for both Golang Web Development and Application Development. Happy Coding!