Go language adds much-anticipated generics
Go 1.18, featuring highly anticipated generics capabilities, has moved to a second beta release. However, Go’s developers have postponed the production release of Go 1.18 until March, instead of February, as originally planned. A Go 1.18 release candidate now is expected in February.
Published January 31, the second Go 1.18 beta fixes some obscure bugs in generics, with the goal of ensuring a more stable final release. Generics has been called the most-significant change to the programming language since the release of Go 1.0 in 2012. The initial beta was released December 14, 2021.
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.
Go 1.18 Beta 1 was the first preview release of the Google-developed open source language containing support for generic code using parameterized types. 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.
The first Go 1.18 beta was the most downloaded Go beta ever, with twice as many downloads as any prior beta. Google is running the first beta product. To install the second beta alongside an existing toolchain, users can run the following:
go install golang.org/dl/go1.18beta2@latest
go1.18beta2 download
Afterward, developers can run go1.18beta2
as a drop-in replacement for Go. For more download options, see https://go.dev/dl/#go1.18beta2.
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.
- 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.