Menu
Google’s Go language could add generics later this year

Google’s Go language could add generics later this year

A proposal to add generic programming to Go using type parameters is the latest attempt to add a long-sought capability that would make the language easier to use

Credit: Dreamstime

Google's Go finally could be adding generics, long sought by many Go users as a mechanism to simplify the language.

A Go language change proposal filed January 12 in GitHub calls for adding support for type parameters for types and functions, thus enabling a form of generic programming. Efforts to add generics to Go have been going on for years, with support for generics being one of the most-commonly requested features since Go was first released in 2009.

Now, Go developers may see an implementation by the end of this year, perhaps included as part of Go 1.18 beta releases. The implementation would be complete but perhaps not fully optimised.

Generics can provide powerful building blocks to share code and more easily build programs. With generic programming, writing functions and data structures can be done in a manner where some types are specified afterward.

For example, a developer could write a function that operates on a slice of an arbitrary data type, where the actual data type is specified when the function is called. A developer also could define a data structure that stores values of any type, in which the actual type to be stored is specified when an instance of the data structure is created.

High-level changes in the generic programming proposal for Go include:

  • Functions can have an additional type parameter list that uses square brackets but otherwise looks like an ordinary parameter list: func F[T any](p T) { ... }
  • These type parameters can be used by the regular parameters and in the function body
  • Types can also have a type parameter list: type MySlice[T any] []T
  • Each type parameter has a type constraint, just as each ordinary parameter has a type: func F[T Constraint](p T) { ... }
  • Type constraints are interface types
  • The new predeclared name any is a type constraint that permits any type
  • Interface types used as type constraints can have a list of predeclared types; only type arguments that match one of those types satisfy the constraint
  • Generic functions may only use operations permitted by their type constraints
  • Using a generic function or type requires passing type arguments
  • Type inference allows omitting the type arguments of a function call in common cases

Fitting generics into a language such as Go is a difficult task, as failed attempts dating back to 2010 indicate. In the past couple of years, the developers of Go have worked on a series of design drafts that culminated in a design based on type parameters. The draft has had input from the Go programming community, and there has been some experimentation with it via the generics playground.

The changes to the language anticipated for generics support are backward-compatible, so existing Go programs would keep working. The current version of Go is version 1.15, with Go 1.16 now in a beta stage. A production release of Go 1.16 is eyed for next month.


Follow Us

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.

Tags Google

Featured

Slideshows

The Kiwi channel gathers for the 2020 Reseller News Women in ICT Awards

The Kiwi channel gathers for the 2020 Reseller News Women in ICT Awards

Hundreds of leaders from the New Zealand IT industry gathered at the Hilton in Auckland on 17 November to celebrate the finest female talent in the Kiwi channel and recognise the winners of the Reseller News Women in ICT Awards (WIICTA) 2020.

The Kiwi channel gathers for the 2020 Reseller News Women in ICT Awards
Leading female front runners honoured at the 2020 Reseller News Women in ICT Awards

Leading female front runners honoured at the 2020 Reseller News Women in ICT Awards

The leading female front runners of the New Zealand ICT industry joined together for the annual Reseller News Women in ICT Awards event at the Hilton in Auckland, during which hundreds of guests celebrated 13 outstanding individuals who won awards, chosen from more than 50 finalists representing over 30 organisations.

Leading female front runners honoured at the 2020 Reseller News Women in ICT Awards
Channel gathers to celebrate the Reseller News Innovation Awards 2020 winners

Channel gathers to celebrate the Reseller News Innovation Awards 2020 winners

More than 500 channel leaders gathered in Auckland on 21 October at the ​Reseller News Innovation Awards ​2020 to celebrate the achievements of the New Zealand technology industry's top partners, start-ups, vendors, distributors and individuals.

Channel gathers to celebrate the Reseller News Innovation Awards 2020 winners
Show Comments