Standard Library Slice Utilities

Learn to use the slices and sort packages from Go's standard library to search, sort, compact, and modify slices without writing manual loops.

Go gives you slices as a dynamic, flexible way to work with sequences of data. The built-in append and copy get you a long way, but many everyday tasks, sorting, deduplicating, searching, or safely cloning, show up often enough that the standard library offers two dedicated packages: slices (since Go 1.21) and sort. They stop you from rewriting the same loops over and over, and they handle edge cases that are easy to get wrong by hand.

Go version for slices package:

The slices package requires Go 1.21 or later. If you are pinned to an older version you can still use the sort package and write your own helpers; this page covers both worlds. The examples here target Go 1.27.

The slices Package

The slices package is a collection of generic functions that operate on any slice type. It covers searching (Contains, Index, BinarySearch), comparing (Equal, Compare), removing (Delete, DeleteFunc, Compact), inserting and replacing (Insert, Replace), copying and capacity (Clone, Clip, Grow), combining (Concat, Repeat), sorting (Sort, SortFunc, SortStableFunc, IsSorted), ordering copies (Sorted, SortedFunc, SortedStableFunc, Reverse), extremes (Min, Max), and iterators (All, Backward, Values, Collect, AppendSeq, Chunk).

Two details catch beginners out. Clip only caps capacity, it does not allocate a new backing array or release the old one, so copy the survivors into a fresh slice when you need to free a large array. And Clone guarantees the same length as the source, not the same capacity, which may be larger.

package main

import (
	"cmp"
	"fmt"
	"slices"
)

type Person struct {
	Name string
	Age  int
}

func main() {
	numbers := []int{42, 7, 19, 101, 3}
	slices.Sort(numbers)
	fmt.Println(numbers) // [3 7 19 42 101]

	people := []Person{
		{"Zara", 25},
		{"Alex", 30},
		{"Alex", 22},
	}
	slices.SortStableFunc(people, func(a, b Person) int {
		return cmp.Compare(a.Name, b.Name)
	})
	// people is now [{Alex 30} {Alex 22} {Zara 25}], Alex entries keep original order
	fmt.Println(people)

	original := []int{1, 2, 3}
	clone := slices.Clone(original)
	clone[0] = 99
	fmt.Println(original) // [1 2 3]
}

The sort Package for Slices

Before the slices package existed, the sort package was the main tool for ordering and searching slices. It is still fully supported and you will encounter it in many codebases. It provides type-specific helpers like sort.Ints, sort.Float64s, and sort.Strings, the flexible sort.Slice and sort.SliceStable for custom orderings, the reusable sort.Interface (with Len, Less, and Swap) for types you sort repeatedly, and binary search with sort.Search and its typed variants. All sorting happens in place. If you are starting a new project on Go 1.21 or later, slices.Sort and slices.SortFunc are the shorter generic alternatives.

Choosing Between slices.Sort and sort.Slice

import "slices"
names := []string{"zoe", "alex", "ben"}
slices.Sort(names)

Both approaches produce identical output. The slices version is shorter for ordered types and stays purely within the slices package; the sort version remains necessary when you need the older sort.Interface for compatibility or when you are writing code that must compile with older Go.

Summary

The slices and sort packages turn repetitive slice bookkeeping into a few well-tested function calls. slices is the modern, generic-first toolbox for containment checks, cloning, deduplication, and in-place modifications. sort still underpins a huge amount of existing code and remains the right choice when you need custom sorting via a comparator function without generics.

The slices Package

A complete guide to the slices package in Go - covering functions for searching, comparing, modifying, and sorting slices with practical examples

The sort Package for Slices

Learn how to sort slices in Go using the sort package, including built-in type helpers, sort.Slice, sort.SliceStable.