Go 1.21: slices package

In Go version 1.21a new package has been added called slices. This package is a set of functions that make it easier to perform the most common slice operations. The package appeared thanks to the addition of generics earlier in 1.18. All functions in the package are generics (or “generics”).

What are generics?

Generics are functions or types that can operate on any data type. Go generics were introduced in version 1.21 and they mean that you can write a function that will work on any type of data, not just a specific one.

For example, the function Delete from the package slices looks like that:

func Delete[S ~[]E, E any](s S, i, j int) S

Here S And E are type parameters. S represents the slice type, and E – type of elements in this slice. Tilda (~) means that S can be any type that can be cast to this slice.

For example, if you have the following types:

type MyType int
type MySlice []MyType

You can use functions from the package slices with objects of type MySlice.

Slices package functions

Most of the package’s functions have versions with the prefix Func, which allow you to use their functions. This gives you more flexibility when working with slices.

For example, functions Sort, IsSorted, Min, Max And BinarySearch can be applied to slices whose elements implement the interface Ordered. This interface includes all the basic numeric and string types, and the types that can be cast to them.

type Ordered interface {
	~int | ~int8 | ~int16 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
		~float32 | ~float64 |
		~string
}

For other types of slices, you can use variants of these functions with the suffix Func and provide your own comparison function that takes two elements and returns a bool.

Equal And EqualFunc Test the equality of two slices by comparing elements directly or using a custom equality function.

Compare And CompareFunc compare two slices sequentially and return <0, 0, or >0 depending on whether the first unequal element in s1 is less than, equal to, or greater than the corresponding element in s2.

Index And IndexFunc return the index of the first element that satisfies the predicate, or -1 if no element satisfies.

Contains And ContainsFunc check whether a value or predicate matches any element.

Insert effectively O(len(s) + len(v)) inserts elements into a slice at a given index, expanding the slice as necessary.

Delete And DeleteFunc remove elements from a slice by index or predicate.

Replace replaces a slice of elements with new values.

Peculiarities

Delete function Deletein the package, is quite simple:

func Delete[S ~[]E, E any](s S, i, j int) S {
	_ = s[i:j] // bounds check
return append(s[:i], s[j:]...)

}

This approach has a drawback. After removal, objects can remain in the original array, and the garbage collector will not remove them from memory. Also, the new slice will have a capacity equal to the original slice.

slices.Delete([]int{1, 2, 3, 4, 5, 6}, 2, 3)

slices.Delete([]int{1, 2, 3, 4, 5, 6}, 2, 3)

Function Clipreduces the capacity of the slice to its current length.

func Clip[S ~[]E, E any](s S) S {
	return s[:len(s):len(s)]
}

This functionality for slices appeared in Go starting with version 1.2 when Full slice expression was introduced.

Full slice expression a[low : high : max]

Simple slice expression a[low : high]

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *