Exploring the power of Go.1.21 — Slices Package

PRATHEESH PC
5 min readJul 25, 2023

--

Golang 1.21 is packed with new features and improvements. One feature that I am excitedly looking is slices,package for common operations on slices of any element type. This includes sorting functions that are generally faster and more ergonomic than the sort package.

We will explore functions such as sorting, searching, comparing, and manipulating slices. These features make the slices package a valuable addition to the Go language, providing developers with efficient ways to handle slice data.

Go 1.21 is not yet released. These are work-in-progress release notes. Go 1.21 is expected to be released in August 2023.

Let’s explore

The Import Statement

You can import the slices package as follows

import "slices"

List of Methods Slices package include

  • BinarySearch : Searches for target in a sorted slice and returns the position where target is found.
  • Clone : Returns a copy of the slice.
  • Compact : Replaces consecutive runs of equal elements
  • Compare : Compare two slices
  • Contains : Check some value exists in the slice
  • Delete : Remove an element from n’th position
  • Equal : Check two slices are equal
  • Grow : Increase the capacity of slice
  • Index : Returns the index of the first occurrence of search value
  • Insert : Insert new values
  • IsSorted : Check the slice is sorted
  • Min and Max : Return minimum or maximum value from the slice
  • Replace : Replace elements
  • Reverse : Reverses the elements of the slice in place.
  • Sort : Sorts a slice of any ordered type in ascending order
  • … having more

Let’s see some samples of some of these functions

BinarySearch

Note : The slice should be sorted before searching an element

Clone

Clone returns a copy of the slice. The elements are copied using assignment, so this is a shallow clone.

Compact

Compact replaces consecutive runs of equal elements with a single copy. Compact modifies the contents of the slice s and returns the modified slice, which may have a smaller length.

And the output

Before Compact :  [0 1 1 1 1 1 1 1 2 3 5 8]
After Compact : [0 1 2 3 5 8]

Compare

This compares each element of the s1 and s2 slice. The elements are compared sequentially, starting at index 0, until one element is not equal to the other

The result is

  • 0 ,when s1 == s2
  • -1, when s1 < s2
  • +1, when s1 > s2

result

Equal :  0
S1 < S2 : -1
S1 > S2 : 1

Contains

This will check whether an element exists in the slice. It will return a boolean value based on the existence

Output

15 exists :  false
6 exists : true

Equal

Equal reports whether two slices are equal: the same length and all elements equal. If the lengths are different, Equal returns false. Otherwise, the elements are compared in increasing index order, and the comparison stops at the first unequal pair. Floating point NaNs are not considered equal.

Output

Equal :  true
Not Equal : false

Index

This function can be used to get the index of an element in the array

Output

banana at position :  1
apple at position : 0

IsSorted

This function will reports whether the given slice is sorted in ascending order

Output

Slice is not sorted
Slice is sorted

Min and Max

This function will return Minimum and Maximum value from the slice

Output

Min value :  1
Max value : 12

Replace

As this function is used to replace the elements from i to j with set of replace elements.

The syntax

newslice := slices.Replace(replaceSlice, from, to, value1, value2, ...)

Output

before Replace :  [zero one two three five seven eight]
after Replace : [zero one two three four five six seven eight]

Reverse

Reverses the elements of the slice in place

Output

before Reverse :  [zero one two three four five]
after Reverse : [five four three two one zero]

Sort

Sorts a slice of any ordered type in ascending order. When sorting floating-point numbers, NaNs are ordered before other values.

Output

before Sort :  [90 10 40 20 50]
after Sort : [10 20 40 50 90]

Func’s of functions

Some of the functions in slices package also have another function with Func suffix , which works similar to the function, but it uses a custom comparison function to determine the functionality.

  • BinarySearchFunc : Similar to BinarySearch , uses a custom comparison function to determine the sort order.
  • CompactFunc : uses a custom comparison function to determine the sort order and remove duplicates.
  • CompareFunc : used to compare two slices using a custom comparison function and returns an integer value indicating the sort order of the slices.
  • ContainsFunc : used to search for an element in a slice using a custom function and returns a boolean value indicating whether the element is found in the slice
  • DeleteFunc : Custom delete function
  • EqualFunc : used to compare two slices using a custom comparison function and returns a boolean value indicating whether the two slices are equal
  • IndexFunc : used to find the index of the first element in a slice that satisfies a given function.
  • IsSortedFunc : used to check whether a slice is sorted in increasing order according to a custom comparison function
  • MinFunc and MaxFunc : Used to find Minimum and Maximum value from the slice with custom comparison

Conclusion

In conclusion, the new slices package in Go provides a set of useful functions for working with slices of any type. The package includes functions for sorting, searching, comparing, and manipulating slices, making it easier to work with slice data in Go. The package is still experimental and subject to change, but it shows promise for becoming a standard part of the Go library in the future.

With the slices package, you can easily check if a slice is sorted, copy one slice into another, and search for elements in a slice using custom functions. You can also remove elements from a slice, find the minimum and maximum values in a slice, and more. The package provides a convenient and efficient way to work with slice data in Go, and it is worth exploring for any Go developer.

Overall, the slices package is a welcome addition to the Go language, and it is exciting to see the development of this awesome language in action. As the package continues to evolve and improve, it will undoubtedly become an even more valuable tool for Go developers.

Thank you, Nahuel Costamagna, for inspiring me to write this article. Your blog post titled “Exploring the Power of Go 1.21: slices Package” on https://www.beeblogit.com/post/2 provided me with valuable insights and ideas on the topic. I appreciate your contribution and hope to continue learning from your work in the future.

by me a coffe

If you enjoy my articles or find value in what I write, consider supporting my work by buying me a coffee! ☕️

--

--

PRATHEESH PC
PRATHEESH PC

Written by PRATHEESH PC

Hey there! I'm a software developer and I'm absolutely in love with building things with technology.

Responses (1)