What are the new features in Go Version 1.20?

What are the new features in Go Version 1.20's picture

On 2 February 2023, we got a major release named Go Version 1.20. This version deals with multiple changes in the implementation of libraries, runtime, and toolchain.

This version arrived six months after Go 1.19 was released and promises to maintain compatibility. There are many new features which are introduced in this blog.

If that is what you are looking for, then stay with us.

What Are The New Features Made In Golang?

In this version, you will find many major changes which are made to increase the efficiency and compatibility of Go Development.

In this section, we are going to talk about them in this section. Starting with the changes in the Golang program.

Changes in Golang

Golang 1.17 added this feature called conversions from a slice to an array pointer. Three new functions SliceData, String, and StringData are called the unsafe package. Now you can construct and deconstruct string and slice values without depending on their exact representation.

It is defined in the specification that the struct values get compared one field at a time. Given the field of order of their appearance in the struct type definition as well as stopping at the first mismatch.

Previously, this specification would need to be compared before the first mismatch.

Similarly, the current specification defines that array values are compared one element at a time, in ascending index order. In both of these cases, the difference affects if certain comparisons must panic. Existing programs are unchanged as the wording of the new spec describes what has always been completed by implements.

Comparable types such as ordinary interfaces can satisfy comparable constraints, even though the type arguments are not strictly comparable, there are chances that the comparison may panic at runtime.

All this makes it possible to instantiate a type parameter constrained by the comparable with a non-sticky comparable type argument such as interface type, or maybe a composite type containing an interface type.

Ports

Windows - The latest release of Go 1.20 is the last one that will run on any release of Windows 7, Windows 8, server 2008 and Server 2012. The next version of Golang will require a system with at least Windows 10 or Server 2016.

Darwin & iOS - Go 1.20 will be the last version that will run on macOS 10.13 High Sierra or 10.14 Mojave. The next version of Go 1.21 would need macOS 10.15 Catalina or later to function.

FreeBSD/RISC-V - Go 1.20 will add experimental support for FreeBSD on RISC-V (G00S=freebsd, GOARCH=riscv64)

Tools

Go command

In the new version of Golang, we can find many changes in the tools that we use in programming. For instance, the directory $GOROOT/pkg will not store a pre-compiled package archive for the standard library: go install will not write them any more. The Go build does not check for them, as well as the Go distribution will no longer ship them.


Instead of all these directories, the required packages will be built and cached in the build cache similar to packages outside GOROOT. Due to this, the size of the Go distribution is reduced and the C toolchain skew is avoided especially for the packages which use cgo.

They have made the use of gotest -json easier. The programs where you can run gotest -json don’t require any improvement. Another change regarding its use is an event when Action is set to Start to be used at the beginning of each test program’s execution.

While running multiple tests in the go command, all these Start events are guaranteed to be written in the same order as the packages named in the command line.

Not only this the go command can define the architecture feature build tags like amd64.v2, allowing one to select the package implementation file with or without a particular architect feature.

Now before performing the command, go subcommands accept the -C<dir> to change the directory into <dir> to change.

Now, the go generate command accepts -skip<pattern> to skip //go:generate directives matching <pattern>.

The go test command accepts the-skip<pattern> in order to skip tests, subtests or all the examples matching <pattern>.

The main modulegoinstall doesn’t install libraries for non-man packages to GOPATH/pkg, when the module is located within GOPATH/src and golist doesn’t report a target field for such packages.

Built commands like gobuild, goinstall, and others. Now support a -pgo flag which allows the profile-guided optimization. Thegobuild, goinstall, and other build-related commands support a -cover flag that builds the specified target with code coverage instrumentation.

CGO

You cannot use cgo by default on systems without a C toolchain. Especially when the CGO_ENABLED environment variable is not set. If the default C compiler is not found in the path then the CGO_ENABLED defaults to 0. But, it can override the default by setting CGO_ENABLED explicitly.

Default change’s notable effect is that when Go is installed in a system without a C compiler, pure Go Builds will be used for packages in the standard library that use cgo instead of using pre-distributed package archives. This makes Go work Better in some minimal container environments and macOS on which pre-distributed package archives have not been used for this since Go 1.16.

Cover

This version supports the collecting code coverage for programs which are different from just unit tests. If you want to collect coverage data for a program you need to build it with the gobuild's -coverflag, then you need to run the resulting binary with the environment variable GOCOVERDIR. This variable is set to an output directory for coverage profiles.

Vet

There are two significant updates in this tool which are:

1. Improved detection of loop variable captured by the nested functions

Now, this tool can report the references to loop variables after following a call to T.parallel() in the subtest function bodies.

This tool can detect the mistakes in more places, in the previous version of Golang, it would only consider the last statement of the loop body.

But in the updated version it inspects the last statements within if, switch, and select statements.

2. New diagnostic for incorrect time formats

When the time format 2006-02-01 (yyyy-dd-mm) used with Time. format and time.Parse. The vet tool reports it. As this format is not commonly used and is confused with the format yyyy-mm-dd.

Runtime

In order to make the current version more CPU efficient and space-saving the internal data structure of the garbage collector was reorganized. This change helps in reducing the memory overheads and improves the overall CPU performance by 2%.

Compiler

The new version of Go 1.20 has added preview support for profile-guided Optimization(PGO). This feature helps the toolchain to perform some tasks which are application and workload-specific which is based on the run time profile information.

In this version, the compiler supports pprof CPU Profiles all of this can be collected through usual means like runtime/pprof or net/http/pprof packages.

The front end of the Go 1.20 compiler is upgraded in order to use a new way of handling the compiler’s internal data. This helps in fixing many generic issues as well as permit type declarations within generic functions & methods.

In G1.20, the compiler will reject anonymous interface cycles by default. These problems arise due to tricky uses of embedded interfaces and have faced subtle correctness issues.

Linker

On Linux, The linker can select the dynamic interpreter for glibc or musl at link time.

On Windows, the modern LLVM-based C toolchain is now supported by Go Linker.

To avoid confusion caused by the user package which starts with go. G1.20 uses go: and type: prefixes for compiler-generated symbols rather than go. This is understood by the debug/gosym package that this is a new convention for binaries built with Go1.20 and newer versions.

Bootstrap

When the Go release is built from source and GOROOT_BOOTSTRAP is not set, in the earlier versions of Go search for a Go 1.4 or later bootstrap toolchain in the directory $HOME/go1.4 (%HOMEDRIVE%%HOMEPATH%\go1.4on the windows.

Go1.18 and 1.19, firstly look for $HOME/go1.17 or $HOME/sdk/go1.17 before falling back to $HOME/go1.4.When bootstrapping Go1.20 require Go 1.17 release.

Later on, it was decided that the latest point of release should be adopted to release for bootstrapping Go 1.20. So now it needs Go1.17.13.

Go 1.20 looks for $HOME/go1.17.13 or $HOME/sdk/go1.17.13 before going back to $HOME/go1.4.

Core Library

New crypto/ ecdh Package

Go1.20 added a new crypto/ecdh package in order to provide explicit support for the elliptic curve Diffie- Hellman Key exchange over NIST curves and Curve25519.

The programs should not use lower-level functionality in crypto/elliptic for ECDH, instead, it should use crypto/ecdh.

Wrapping Multiple Errors

Go1.20 has expanded the support for error wrapping to allow an error to wrap multiple other errors. By using an unwrap method, an error e can wrap more than one error which returns a [ ]error.

Two functions, errors.Is and error.As are updated to inspect multiply wrapped errors.

A new function is introduced that returns an error wrapping many errors. We are talking about errors.Join

HTTP ResponseController

The new “net/http”.Responsecontroller type is responsible for providing access to extending per-request functionality and these are not handled by the “net/http”.Responsewriter interface.

The ResponseController type provides an easier and more efficient way to add per-handler controls. SetReadDeadline and SetWriteDeadline are two controls which allow setting per-request and write deadline.

New ReverseProxy Rewrite hook

httputil.ReverseProxy forwarding proxy includes a new Rewrite hook function, superseding the previous Director hook.

The Rewrite hook accepts a ProxyRequest parameter, this includes both inbound and outbound requests which are received and sent by proxy.

The rewrite hooks help to avoid instances where a malicious inbound request may cause headers to be removed before forwarding.

Conclusion

We have tried to include all the new features of the Go 1.20 version. As well as the things that are important to know like the versions that won’t support the latest version of Golang.

Along with that, this version is seen as a huge change in the series of updated Golang versions. The only thing that we did not include were the minor changes that were made in the library. If you are looking for them then you can look them up on the official website.


Build Your Golang Team