3 min read

Update: On 31st July, Ian Lance Taylor posted in detail explaining the benefits and costs of including generics in Go. He also briefly discussed the draft design to convey how generics is going to be added to the Go language. Taylor says “Our goal is to arrive at a design that makes it possible to write the kinds of generic code, without making the language too complex to use or making it not feel like Go anymore.”

Check out the Goland blog for more details.

On 26th July at GopherCon 2019, Ian Lance Taylor introduced generics codes in Go. He briefly explained the need, implementation and benefits from generics for the Go language. Next, Taylor reviewed the Go contract design draft which included addition of optional type parameters to types and functions.

Taylor also proposed guidelines for implementing generic design in Go.

In all the three years of Go surveys, lack of generics has been listed as one of the three highest priorities for fixing the Go language.

Taylor defines generic as “Generic programming which enables the representation of functions and data structures in a generic form, with types factored out.”

Generic code is written using types, which are specified later. An unspecified type is called as type parameter. A type parameter offers support only when permitted by contracts.

A generic code imparts strong basis for sharing codes and building programs. It can be compiled using an interface-based approach which optimizes time as the package is compiled only once. If a generic code is compiled multiple times, it can carry compile time cost.

Some of the many functions that can be written generically in Go include –

Image Source: Source graph

Go already supports two generic data structures which are built using Slice and Map languages. Go requires data structures to be written only once and then reused after putting it in a package.

The contract draft design states that since Go is designed to support programming, a clear contract should be maintained between a generic code and a calling code. With the new changes, users may find the language more complex. However, the Go team expects users to not write generic code themselves, instead use packages that are written by others using generic code.

Developers are very happy that the Go generics proposal is simple to understand and enables users to depend on the already written generic packages. This will save them time as users need not rewrite type specific functions in Go.

Users have also admired the new contract design draft by the Go team.

Head over to the Google proposal page for more details on the new contract draft design.

Read More

Ian Lance Taylor, Golang team member, adds another perspective to Go being Google’s language

Is Golang truly community driven and does it really matter?

Go User Survey 2018 results: Golang goes from strength to strength, as more engineers than ever are using it at work