Go 1.18 arrives with much-anticipated generics

Now available in a production release, Go 1.18 introduces ‘the most significant change’ to Go since the programming language debuted in 2012.

Go 1.18 arrives with much-anticipated generics
Paweł Czerwiński (CC0)

Go 1.18, featuring highly anticipated generics capabilities, is now generally available as a production release.

Downloadable at go.dev as of March 15, Go 1.18 introduces support for generic code using parameterized types. Generics has been called the most significant change to the Go programming language since the release of Go 1.0 in 2012. It was also the most-requested feature by Go programmers.

Subsequent releases will provide additional support for more complicated generic use cases. Users can learn about the feature via a generics tutorial and release notes.

The Go language server gopls and the Visual Studio Code Go extension now support generics. To install gopls with generics, see this documentation. To configure the VS Code Go extension, follow these instructions.

Production availability of Go 1.18 follows beta and release candidate stages, with the production release slipping slightly to mid-March. The release was previously expected February 2022.

Go 1.18 Beta 1, which arrived December 14, was the first preview release of the Google-developed open source language containing support for generic code using parameterized types. The first Go 1.18 beta was the most downloaded Go beta ever, with twice as many downloads as any prior beta.

Generic programming is an enhancement enabling representation of functions and data structures in a generic form, with types being factored out. Generics can provide building blocks to share code and make it easier to build programs.

In a blog post marking the initial beta release, the Go team cautioned that with any significant new feature, it was common for users to discover new bugs; generics is not expected to be an exception to this rule. Developers are advised to approach new generics with appropriate caution. Additionally, support for certain subtle cases such as specific kinds of recursive generic types has been postponed to future releases.

Nevertheless, some early adopters have been “quite happy,” the team said. A brief tutorial has been published on getting started with generics. While Go has had interface types, a form of generic programming, it has lacked what Go developers have wanted from generics, according a July 2019 blog post. For example, advocates for Go generics have sought abilities such as being able write functions such as Reverse without caring about the element type of a slice.

Release notes are posted at tip.golang.org. Also in Go 1.18:

  • Built-in support is offered for writing fuzzing-based tests, to find inputs that cause a program to crash or return invalid answers. This feature is experimental in Go 1.18.
  • A Go workspace mode lets developers work with multiple Go modules simultaneously, which is important for larger projects.
  • An expanded go version -m command now records build details such as compiler flags.
  • A GOAMD64 environment variable allows users to select any of four versions of the AMD64 architecture.
  • CPU performance on Apple M1, ARM64, and PowerPC64 has been improved by as much as 20% due to the expansion of the Go 1.17 register ABI calling convention to these architectures.
  • The Windows/Arm and Windows/Arm64 ports now support non-cooperative pre-emption, bringing this capability to all four Windows ports and hopefully addressing subtle bugs encountered when calling into Win32 functions that block for extended time periods.
  • The garbage collector now includes non-heap sources of GC work when determining how frequently to run. This makes GC overhead more predictable when these sources are significant.

Go 1.17 was released in August. Probably best known for being used in development of the Docker container system, Go was designed to be a simple, easy-to-learn programming language with strong support for concurrency.

Copyright © 2022 IDG Communications, Inc.