Generics in Golang

In this blog, we will introduce the concept of Generics, which was recently implemented in Golang version 1.18. Read more to find out the purpose of Go Generics and its basic features!

Introduction to the Creation of Go Generics

Go generics was designed to make writing generic and reusable Go code easier.

One of the primary issues addressed in Golang is its lack of generics. Even though almost every programming language like Python and Java supports generics, Go’s creators only decided to add it to its features in its recently updated version 1.18. Implementing and designing generics in Go was a challenge due to the nature of the programming language to be simple to learn and free of complexities.

Constructing a generic function with no standard method in the Go programming language may be unexpected. The only popular approach is to utilize the empty interface type or code generators, which have their own set of challenges.

After various research and proposals over the past decade, Go successfully adopted the right approach for implementing generic programming by introducing a new type of constraint parallel to interface types.

What is the purpose of Go Generics?

Generics are a means of constructing code that is unaffected by the types that are utilized. Any of a set of types can now be used in functions and types.

Generics added three (3) major changes to Golang. We'll go over the new language features in this blog. We won't go into great detail, but we will cover all significant aspects.

  1. Type Parameters and Lists (Types and Functions)

Type parameters are added to function declarations and types. Developers can encode functions and data structures with type parameters and type lists inside interfaces as constraints. Constraints are a technique of expressing requirements for type parameter lists enclosed inside square brackets.

Types are factored out of a function or data structure specification, then represented in a generic form using generics.

Example of Golang type parameter: a type parameter list is added with a single type parameter T, and replaces the uses of float64 with T

2. Type Sets

Interface types are defined as sets of types, including types without methods. Type parameters can be instantiated using type arguments. There is a type for each type parameter in a type parameter list. When used as a type constraint, the type set defined by an interface specifies the types that are authorized as type arguments for the respective type parameter.

Example of using an interface type in a union element which adds that type set to the union

3. Type Inference

Type Inference is the most difficult modification to Golang, but it is critical because it allows developers to write code that calls generic functions. Its overall mechanics are quite complex, but it is easy to apply. Go’s creators aimed to achieve a balance between inference power and complexity. It’s not perfect but continuous updates will be made to improve it in the upcoming release.

 Example of type inference: the call to MyFunc on line 11 explicitly declares the type to be used but in the call on line 14 the type of the parameter is inferred from the type of the variable declared on line 8 (int)

Conclusion

Generics would benefit the language with increased flexibility and code reusability, resulting in a more efficient and strong programming language. It will enable developers to construct a wide range of general solutions using types or functions with type parameters. Go generics would allow the development of larger applications to be easier.

Since the very nature of Golang has a simple and minimal standard library, developers must find best practices for the use of generics.

Let us know your thoughts about Generics on Golang! Reach out to us at www.pro5.ai.