Quick Guide: Go 1.21 Features

Emre Odabas
3 min readJul 28, 2023

--

Go 1.20 was released nearly six months ago, and the community has already released rc3 of Go 1.21. If you wonder what comes with earlier versions, you could check For Quick Guide: Go 1.20 Features

For Quick Guide: Go 1.22 Features

Go 1.21 will be released in August 2023. This version mainly focused on helper functions for maps, slices, and channels. Let’s look at what comes with Go 1.21.

Table of Contents

Min, Max, and Clear Functions

Finally, Go add some default functions for slices, maps, and channels. You could use these functions as shown in the specification.

var x, y int
m := min(x) // m == x
m := min(x, y) // m is the smaller of x and y
m := max(x, y, 10) // m is the larger of x and y but at least 10
c := max(1, 2.0, 10) // c == 10.0 (floating-point kind)
f := max(0, float32(x)) // type of f is float32
var s []string
_ = min(s...) // invalid: slice arguments are not permitted
t := max("", "foo", "bar") // t == "foo" (string kind)
Call        Argument Type     Result
------------------------------------------------------------------
clear(m) map[K]T deletes all entries, resulting in an
empty map (len(m) == 0)

clear(s) []T sets all elements up to the length of
s to the zero value of T

clear(t) type parameter see below

^^^ go top ^^^

Standard Log Package

Go 1.21 provides a structured log (slog). You could include a message, set your log severity and add key-value pairs. This release also includes the slogtest package for testing purposes.

logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
logger.Info("hello", "count", 3)

^^^ go top ^^^

Map Functions

With the power of generic functions, Go 1.21 provides several new functions that are very useful. (Updated) Keys and Values functions were deleted with this issue.

func Clone(m M) M
func Copy(dst M1, src M2)
func DeleteFunc(m M, del func(K, V) bool)
func Equal(m1 M1, m2 M2) bool
func EqualFunc(m1 M1, m2 M2, eq func(V1, V2) bool) bool
f̶u̶n̶c̶ ̶K̶e̶y̶s̶(̶m̶ ̶M̶)̶ ̶[̶]̶K̶
̶f̶u̶n̶c̶ ̶V̶a̶l̶u̶e̶s̶(̶m̶ ̶M̶)̶ ̶[̶]̶V̶

^^^ go top ^^^

Slice Functions

Like Map Functions, we could use Sort, Contain, Compare, Reverse, Binary Search, and Index functions on Slices. You can reach the whole function list on this page. Here are some sample usages of functions and their results as comments.

 names := []string{"Vera", "Bob", "Alice"}
slices.IsSorted(names) // false
slices.Sort(names) // [Alice Bob Vera]
slices.Compare(names, []string{"Alice", "Bob", "Vera"}) // 0
slices.BinarySearch(names, "Bob") // 1 true
slices.Reverse(names)// [Vera Bob Alice]
slices.Insert(names, 1, "Bob") // [Vera Bob Bob Alice]
slices.Compact(names)// [Vera Bob Alice]

^^^ go top ^^^

Compare & Less Functions

Compare functions for Ordered Types that are defined under the cmp package. It would work with int(8..64), uint(8..64,ptr), float(32,64) and strings.

func Compare(x, y T) int
func Less(x, y T) bool

^^^ go top ^^^

The Profile Guided Optimization (PGO)

This feature is introduced with Go 1.20 and becomes default with Go 1.21. With this feature, Go enables AutoFDO ability. It offers 2–7% performance improvements depending on application behavior. You can check this awesome article for implementation detail.

^^^ go top ^^^

--

--