Generics In Golang

 

Golang is an emerging technology that took the development world by storm. Introducing rapid software development that is consistent, strongly typed, insanely fast performance, and simple concurrency model; that is cheap, easy to learn and debug. 

As promised by google on twitter, finally generics are introduced in Golang.



This post will introduce the upcoming feature in Golang 1.18 called Generics. I'm going to assume basic familiarity with Golang. The rest of content is organized as follows:

  1. What are generics
  2. Why Golang?
  3. Golang before generics
  4. Generics in Golang
  5. Install Golang 1.18
  6. Simple stack using generics
  7. Implementation in Golang.

What are generics?

Generics are programming concepts that applies to functions, classes, and methods. That allows the usage and declaration of strongly typed software artifacts with complete independence on the type/s of their parameters. This can be achieved in a two step process.


First the function, method or class will provide a mechanism to accept the types as parameters.
Then the caller must provide those types when using the method. For example, if we need to provide a static function that receives two objects and returns the addition result.


Instead of writing a function for each type, we can make use of generics. As an illustration of how generics works in other languages, here are some examples in c++ and c#:




The above code snippets simply calls the add method with different data types and returns the addition result in the same data type as the arguments.

Why Golang?

If you haver ever used Golang (which I assume that you are) you may have came across some of its powerful syntax and programming paradigm. Golang natively included all the tools required to develop feature rich cross-platform applications. Including an impressive concurrency model, fault tolerance, amazingly realtime performance, unified database interface,.....

This completed with an outstanding community and google ownership leaves no stone unturned. Golang has unprecedented code reuse ability and package distribution. You can check the complete list of features here.


Golang before generics

Before the formal introduction to generics in Golang, creating a generic code was not impossible, but it involved the usage of type reflections, interfaces, and it was a bit frustrating to review and develop. This is what the  aforementioned add function would look like without the use of generics:

    





  • The idea here is use an empty interface to accept any kind of argument at line 9.
  • lines 10 throw 13, we check if both of the arguments are of the same type.
  • Lines 15 throw 31, checks the type of the arguments, casts the interface to that type and  perform the addition.
  • Lines 32 throw 34, returns error if the supplied type is not supported by the function.

Install Golang 1.18

In order to start using Golang generics, you must install Golang 1.18 or newer. Assuming that you have already an order version of Golang installed.
Just use this script.



If this works fine, you should see Golang 1.18 printed on the terminal window, If not please make a comment to get help.


Simple stack using generics

A very famous and useful application of generics is using stacks. If you don't know about stacks, click here.

Generics can be a lot of help when it comes to the implementation of different data structures such as stacks, heaps, graphs, etc.. because it allows the development of a generic data structure rather than a typed locked ones.  We are going to implement the following methods:

  • pop: removes the first item of the stack
  • push: appends an item to the stack
  • peak: gets a peak at the next item in the stack

Implementation in Golang

The implementation of the stack itself would take place in an upcoming post. Here I would like to implement the add function (we have reviewed in c++ and c#) in Golang. And here it goes:



As we may have notice, there is some slight differences in Golang generics than other languages. For example: Golang used square brackets rather than the symbol "<>".
Golang went further, it provided a mechanism (an optional mechanism i.e. not required) to restrict the type of data that goes into the function.

In the last code snippet the function "add" accepts two parameters a,b. The type of these parameters is denoted by the letter "T" where "T" can be any of these types. The function add also returns a variable of type "T".

A detailed analysis and benchmarks of generics in Golang is to be expected in this blog. Stay tuned.

Comments

Popular posts from this blog

Stack Using Golang Generics

Golang Generics Performance Evaluation and Implications