Go Reference ยท Collections: sort, slices, maps
Go slices package
Reference for the Go slices package (Go 1.21+): sorting, searching, comparing, cloning, inserting, deleting, compacting and iterator functions for any slice type.
The package slices collects generic functions that work on slices of any element type. It arrived in Go 1.21 and replaces many hand-written loops and most uses of the older sort package: sorting, searching, comparing, cloning, removing, and, since Go 1.23, converting between slices and iterators.
The functions are generic over the slice type S ~[]E, so they accept named slice types and return the same named type where that makes sense. Functions that compare values need E to be comparable or cmp.Ordered; the ...Func variants take your own function instead.
s := []int{3, 1, 2} slices.Sort(s) fmt.Println(s, slices.Contains(s, 2)) // [1 2 3] true
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
Main groups of functions (type parameters omitted):
| Group | Functions |
|---|---|
| Sorting | Sort, SortFunc, SortStableFunc, IsSorted, IsSortedFunc |
| Searching | Contains, ContainsFunc, Index, IndexFunc, BinarySearch, BinarySearchFunc |
| Min and max | Max, Min, MaxFunc, MinFunc |
| Comparing | Equal, EqualFunc, Compare, CompareFunc |
| Editing | Insert, Delete, DeleteFunc, Replace, Compact, CompactFunc, Reverse |
| Copying and memory | Clone, Clip, Grow, Concat, Repeat |
| Iterators (Go 1.23) | All, Values, Backward, Collect, AppendSeq, Sorted, SortedFunc, Chunk |
func Sort[S ~[]E, E cmp.Ordered](x S) func SortFunc[S ~[]E, E any](x S, cmp func(a, b E) int) func Contains[S ~[]E, E comparable](s S, v E) bool func Index[S ~[]E, E comparable](s S, v E) int
| Parameter | Description |
|---|---|
s, x |
The slice. A nil slice behaves like an empty one. |
cmp |
A three-way comparison: negative if a < b, zero if equal, positive if a > b. |
v |
The value to look for. |
Edge cases: Max and Min panic on an empty slice. Insert and Delete panic when an index is out of range. For floats, NaN is treated as smaller than any other value in sorting and Max/Min return NaN if the slice contains one.
Usage notes
- Editing functions may modify the original array.
Insert,Delete,Compact,ReplaceandDeleteFuncreuse the underlying array, so always assign the result (s = slices.Delete(s, 1, 3)) and do not keep using the old slice. Since Go 1.22 the elements freed at the end are zeroed so they do not keep garbage alive. Clonefor independence: it returns a copy that does not share memory, and keeps nil as nil.Compactremoves only consecutive duplicates, so sort first to remove all duplicates.BinarySearchrequires sorted input, and returns the position where the value is or would be inserted, plus a found flag.- Compare functions:
cmp.Compareandcmp.Orfrom packagecmpbuild multi-key comparisons.strings.Compareworks for strings. - Faster than
sort: the generic functions avoid reflection and interface method calls. - Maps have their own package,
maps; combine them, for exampleslices.Sorted(maps.Keys(m))for a sorted key list. - Requires Go 1.21, and the iterator functions need Go 1.23.