What are the new updates in the latest Go 1.21 version?

What are the new updates in the latest Go 1.21 version?'s picture

After the previous versions of Golang, we saw remarkable updates which helped to increase the efficiency of use. As Go promises to improve the compatibility. The new version promises this improvement.

In this blog, we will talk about the new features that make this language programmer-friendly. We will talk about the changes made in the language, tools, Go commands and many more things.

So let’s dive into this without wasting any time.

What are the major updates made in Go 1.21?

In this latest version of Go 1.21, we can see many updates that will help to make Go development experience better than before. The main changes are explained below:

  • min and max function

min and max functions are introduced in Go 1.21. These functions will calculate the largest or smallest for the min value of a fixed number of given arguments.

  • clear function

This new function is responsible for deleting all the elements from a map or it will zero all the elements from a slice.

  • Specified Package initialization

As compared to the previous version, the package initializing order needs to be specified more precisely. The new algorithm is as follows:

  1. Arrange all packages by import path.
  2. Repeat this over and over til the list of packages is empty.

a. Find the first package in the list where all the imports are initialized already.

b. Remove the initialized package from the list.

Some programs may misbehave as they rely on a specific initialization order which was not expressed by explicit imports. As specs did not define the behaviour of these programs in the past releases.

  • Other Improvements

All these other improvements are made to increase the power and precision of inference to improve the overall usability of Golang version 1.21.

  1. You can use this as a possibly particularly instantiated generic function or we can say a somewhat incomplete version more flexibly. As the compiler will try to infer it. The main takeaway is that you can use a generic function without explicit instantiation when it is assigned as a variable or returned as a result value when you can infer the type argument from the assignment.
  2. When the value is assigned to an interface type inference in the current version can also consider methods.
  3. Similarly, a type argument has to implement all the methods of its comparable constraint, the type argument’s method and constraints are matched in such a way that it leads to the inference of additional type arguments.
  4. In the earlier version of Golang, if there were different types of multiple untyped constant arguments are passed with the same parameters type, instead of an error. This update will infer the type from the type constant argument in line with the types of constant expression.
  5. In the new update, the compiler can report an inference error if the two component types are not identical. By identical it means that component types like elements of slices or the parameter types in the function signatures should have suitable type arguments to match.
  6. runtime.PanicNilError - This function is a new addition in the Go 1.21 updated version, if a goroutine is panicking then a deferred function can call to recover it directly as well and it is guaranteed not to be nil. You will get this run time panic runtime.PanicNilError, when you are calling panic with a nil interfaced value or an untyped nil.

What Are The Changes Made In The Tools Of Golang?

In this version of the Golang Update, you will find that Go Toolchain has improved support for backward and forward compatibility.

The Backward Compatibility is made by making changes to control the default behaviour of GODEBUG. For example: In the earlier versions, if the programs were dependent on buggy behaviour, then fixing those bugs would result in the program break.


But after the improvements, we can observe some behavioural changes in Go. If we take the same example of bug dependency, we will see that Go can choose between old and new behaviour based on the Goline in the go.work file or in go.mod file.

It upgrades to the new Go toolChain but leaves the go line set to the older Go version. Hence, this version maintains the benefit of the older toolchain that supports compatibility as well as security.

Now, to improve the Forward Compatibility, Golang has made a strict requirement which is that you can not use Go. 1.21.0 with Go 1.20. This restriction would result in better results in error reporting as well as prevents unresolved import errors or syntax error.

All these restrictions will help to make this version manageable as Go can call a toolchain which is grouped with its own results and requirements. As a go.mod or go.work go line declares a minimum requirement on a newer version then this will run automatically.

Changes in Go command

The new changes revolve mainly around the -pgo build flag, -C dir flag. In Go 1.21, we will come across various updates.

-pgo build flag defaults into -pgo=auto, and now you can use this without specifying a single main package on the command line. If it finds a file named default.pgo in the main package’s directory. Then the go command will be used to enable the profile-guided optimization to build the corresponding program.

And some changes directly affect how we write the programs like:

  1. If you are using the -C dir flag in your command line then make sure that it is the first flag there.
  2. Now the new go test option will print full names instead of just base names.
  3. Go test -c flag can support writing test binaries for multiple packages.
  4. The go test -o flag accepts the directory argument.

What are the improvements made with runtime?

  1. To facilitate the process of debugging while printing very deep stacks. Instead of printing the first 100 frames, now it prints the first 50 innermost frames and after that 50 outermost frames.
  2. For better utilization of memory for the Linux platforms especially the one that supports the transparent huge pages. The go runtime can manage which part of the heap could be backed by the huge pages more specifically. Small heaps use up less memory and large oneworld experience fewer broken pages for dense parts of the heap.
  3. Due to improvements in the runtime internal garbage collecting, the applications will experience a 40% reduction in tail latency and a small reduction in memory usage.

Compiler

In the previous updates, we have talked about PGO which was added as a preview. Now, in this latest version, it is ready for general use. It will help in adding additional optimizations on the codes which are identified as hot by the profile of the production workload.

The most remarkable feature of this version is the speed improvement. This is achieved by using the PGO at default for the binaries that improve the behaviour of the application by 2 to 7%.

These things have led to an increase in build speed up to 6%.

Assembler

There are some changes in the use of frameless nosplit assembly functions when used on the amd64. Now this function will not mark NOFRAME automatically but it needs to be specified according to requirement.

Linker

Go 1.21 has tried to improve the interrogation of Go application with the Windows debuggers and other tools with the help of a linker which emits SEH unwinding data by default on Windows and amd64.

In this version, the compiler helps the linker to delete the dead global map variables, only if the number of entries in the variable intializer is big enough and when the initializer expression will not cause any side effect.

What Are The Changes Made In The Core Library?

In this section, we will talk about the main changes made in the core library. We are going to talk about them in this section:

  1. New log/slog package - These new log/slog pancakes will help with providing structured logging with the levels. This structured logging emits the key-value pairs to make it faster, and more accurate at processing the large amount of log data.
  2. New testing/slogtest packages will help with validating the slog.Handler implementation.
  3. New slices packages - This will help with providing several common operations on the maps, using generic functions which work with the maps of any key or element type.
  4. New maps packages will provide many common operations on the maps.
  5. New cmp package defines the type constraint Ordered with the other two generic functions which are Less and Compare.

Conclusion

In this blog, we discussed the all teh major features which are made in the current version of Golang to help with increasing the compatibility of this language.

That’s all for now.

Hope you find this informative.



Build Your Golang Team