Skip to main content
Package slices defines various functions useful with slices of any type.

Comparison Functions

Equal

func Equal[S ~[]E, E comparable](s1, s2 S) bool
Reports whether two slices are equal: the same length and all elements equal. Empty and nil slices are considered equal. Floating point NaNs are not considered equal.
s1
S
required
First slice to compare
s2
S
required
Second slice to compare
return
bool
true if slices are equal, false otherwise

EqualFunc

func EqualFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, eq func(E1, E2) bool) bool
Reports whether two slices are equal using an equality function on each pair of elements.
s1
S1
required
First slice to compare
s2
S2
required
Second slice to compare
eq
func(E1, E2) bool
required
Equality function

Compare

func Compare[S ~[]E, E cmp.Ordered](s1, s2 S) int
Compares the elements of s1 and s2 using cmp.Compare on each pair. The result is 0 if s1 == s2, -1 if s1 < s2, and +1 if s1 > s2.
s1
S
required
First slice to compare
s2
S
required
Second slice to compare
return
int
0 if equal, -1 if s1 < s2, +1 if s1 > s2

CompareFunc

func CompareFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, cmp func(E1, E2) int) int
Like Compare but uses a custom comparison function on each pair of elements.
s1
S1
required
First slice to compare
s2
S2
required
Second slice to compare
cmp
func(E1, E2) int
required
Comparison function returning negative, zero, or positive

Search Functions

Index

func Index[S ~[]E, E comparable](s S, v E) int
Returns the index of the first occurrence of v in s, or -1 if not present.
s
S
required
The slice to search
v
E
required
The value to find
return
int
Index of first occurrence, or -1 if not found

IndexFunc

func IndexFunc[S ~[]E, E any](s S, f func(E) bool) int
Returns the first index i satisfying f(s[i]), or -1 if none do.
s
S
required
The slice to search
f
func(E) bool
required
Predicate function

Contains

func Contains[S ~[]E, E comparable](s S, v E) bool
Reports whether v is present in s.
s
S
required
The slice to search
v
E
required
The value to find
return
bool
true if v is present, false otherwise

ContainsFunc

func ContainsFunc[S ~[]E, E any](s S, f func(E) bool) bool
Reports whether at least one element e of s satisfies f(e).
s
S
required
The slice to search
f
func(E) bool
required
Predicate function

Modification Functions

Insert

func Insert[S ~[]E, E any](s S, i int, v ...E) S
Inserts the values v… into s at index i, returning the modified slice. The elements at s[i:] are shifted up to make room. Insert panics if i > len(s).
s
S
required
The slice to insert into
i
int
required
The index to insert at
v
...E
required
The values to insert
return
S
The modified slice
complexity
string
O(len(s) + len(v))

Delete

func Delete[S ~[]E, E any](s S, i, j int) S
Removes the elements s[i:j] from s, returning the modified slice. Delete panics if j > len(s) or s[i:j] is not a valid slice.
s
S
required
The slice to delete from
i
int
required
Start index (inclusive)
j
int
required
End index (exclusive)
return
S
The modified slice
complexity
string
O(len(s)-i)

DeleteFunc

func DeleteFunc[S ~[]E, E any](s S, del func(E) bool) S
Removes any elements from s for which del returns true, returning the modified slice.
s
S
required
The slice to delete from
del
func(E) bool
required
Predicate function to determine which elements to delete

Replace

func Replace[S ~[]E, E any](s S, i, j int, v ...E) S
Replaces the elements s[i:j] by the given v, and returns the modified slice. Replace panics if j > len(s) or s[i:j] is not a valid slice.
s
S
required
The slice to modify
i
int
required
Start index (inclusive)
j
int
required
End index (exclusive)
v
...E
required
The replacement values

Sorting Functions

Sort

func Sort[S ~[]E, E cmp.Ordered](x S)
Sorts a slice of any ordered type in ascending order. When sorting floating-point numbers, NaNs are ordered before other values.
x
S
required
The slice to sort

SortFunc

func SortFunc[S ~[]E, E any](x S, cmp func(a, b E) int)
Sorts the slice x in ascending order as determined by the cmp function. This sort is not guaranteed to be stable.
x
S
required
The slice to sort
cmp
func(a, b E) int
required
Comparison function returning negative when a < b, positive when a > b, zero when equal

SortStableFunc

func SortStableFunc[S ~[]E, E any](x S, cmp func(a, b E) int)
Sorts the slice x while keeping the original order of equal elements, using cmp to compare elements.
x
S
required
The slice to sort
cmp
func(a, b E) int
required
Comparison function

IsSorted

func IsSorted[S ~[]E, E cmp.Ordered](x S) bool
Reports whether x is sorted in ascending order.
x
S
required
The slice to check
return
bool
true if sorted, false otherwise

IsSortedFunc

func IsSortedFunc[S ~[]E, E any](x S, cmp func(a, b E) int) bool
Reports whether x is sorted in ascending order, with cmp as the comparison function.
x
S
required
The slice to check
cmp
func(a, b E) int
required
Comparison function

BinarySearch

func BinarySearch[S ~[]E, E cmp.Ordered](x S, target E) (int, bool)
Searches for target in a sorted slice and returns the position where target is found, or the position where target would appear in the sort order. The slice must be sorted in increasing order.
x
S
required
The sorted slice to search
target
E
required
The value to search for
index
int
The position where target is found or should be inserted
found
bool
true if target was found, false otherwise

BinarySearchFunc

func BinarySearchFunc[S ~[]E, E, T any](x S, target T, cmp func(E, T) int) (int, bool)
Works like BinarySearch, but uses a custom comparison function. The slice must be sorted in increasing order.
x
S
required
The sorted slice to search
target
T
required
The value to search for
cmp
func(E, T) int
required
Comparison function returning 0 if match, negative if element precedes target, positive if follows

Min/Max Functions

Min

func Min[S ~[]E, E cmp.Ordered](x S) E
Returns the minimal value in x. It panics if x is empty. For floating-point numbers, Min propagates NaNs.
x
S
required
The slice to search (must not be empty)
return
E
The minimum value

MinFunc

func MinFunc[S ~[]E, E any](x S, cmp func(a, b E) int) E
Returns the minimal value in x, using cmp to compare elements. It panics if x is empty.
x
S
required
The slice to search (must not be empty)
cmp
func(a, b E) int
required
Comparison function

Max

func Max[S ~[]E, E cmp.Ordered](x S) E
Returns the maximal value in x. It panics if x is empty. For floating-point numbers, Max propagates NaNs.
x
S
required
The slice to search (must not be empty)
return
E
The maximum value

MaxFunc

func MaxFunc[S ~[]E, E any](x S, cmp func(a, b E) int) E
Returns the maximal value in x, using cmp to compare elements. It panics if x is empty.
x
S
required
The slice to search (must not be empty)
cmp
func(a, b E) int
required
Comparison function

Utility Functions

Clone

func Clone[S ~[]E, E any](s S) S
Returns a copy of the slice. The elements are copied using assignment, so this is a shallow clone. The result may have additional unused capacity.
s
S
required
The slice to clone
return
S
A copy of the slice

Compact

func Compact[S ~[]E, E comparable](s S) S
Replaces consecutive runs of equal elements with a single copy. This is like the uniq command found on Unix.
s
S
required
The slice to compact
return
S
The compacted slice

CompactFunc

func CompactFunc[S ~[]E, E any](s S, eq func(E, E) bool) S
Like Compact but uses an equality function to compare elements. For runs of elements that compare equal, CompactFunc keeps the first one.
s
S
required
The slice to compact
eq
func(E, E) bool
required
Equality function

Grow

func Grow[S ~[]E, E any](s S, n int) S
Increases the slice’s capacity, if necessary, to guarantee space for another n elements. After Grow(n), at least n elements can be appended to the slice without another allocation.
s
S
required
The slice to grow
n
int
required
Number of additional elements to make room for
return
S
The slice with increased capacity

Clip

func Clip[S ~[]E, E any](s S) S
Removes unused capacity from the slice, returning s[:len(s):len(s)].
s
S
required
The slice to clip
return
S
The clipped slice

Reverse

func Reverse[S ~[]E, E any](s S)
Reverses the elements of the slice in place.
s
S
required
The slice to reverse

Concat

func Concat[S ~[]E, E any](slices ...S) S
Returns a new slice concatenating the passed in slices. If the concatenation is empty, the result is nil.
slices
...S
required
The slices to concatenate
return
S
The concatenated slice

Repeat

func Repeat[S ~[]E, E any](x S, count int) S
Returns a new slice that repeats the provided slice the given number of times. The result has length and capacity (len(x) * count).
x
S
required
The slice to repeat
count
int
required
Number of times to repeat (must be non-negative)
return
S
The repeated slice

Examples

Basic Operations

package main

import (
    "fmt"
    "slices"
)

func main() {
    // Equal
    s1 := []int{1, 2, 3}
    s2 := []int{1, 2, 3}
    fmt.Println(slices.Equal(s1, s2)) // true

    // Contains
    fmt.Println(slices.Contains(s1, 2)) // true

    // Index
    fmt.Println(slices.Index(s1, 3)) // 2

    // Clone
    s3 := slices.Clone(s1)
    fmt.Println(s3) // [1 2 3]
}

Sorting

package main

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

type Person struct {
    Name string
    Age  int
}

func main() {
    people := []Person{
        {"Alice", 55},
        {"Bob", 24},
        {"Gopher", 13},
    }

    // Sort by age
    slices.SortFunc(people, func(a, b Person) int {
        return cmp.Compare(a.Age, b.Age)
    })

    fmt.Println(people)
    // [{Gopher 13} {Bob 24} {Alice 55}]
}

Modification

package main

import (
    "fmt"
    "slices"
)

func main() {
    s := []int{1, 2, 3, 4, 5}

    // Insert
    s = slices.Insert(s, 2, 99)
    fmt.Println(s) // [1 2 99 3 4 5]

    // Delete
    s = slices.Delete(s, 2, 3)
    fmt.Println(s) // [1 2 3 4 5]

    // DeleteFunc
    s = slices.DeleteFunc(s, func(n int) bool {
        return n%2 == 0 // delete even numbers
    })
    fmt.Println(s) // [1 3 5]
}
package main

import (
    "fmt"
    "slices"
)

func main() {
    numbers := []int{1, 3, 5, 7, 9}

    // BinarySearch
    i, found := slices.BinarySearch(numbers, 5)
    fmt.Printf("Index: %d, Found: %v\n", i, found)
    // Index: 2, Found: true

    // Min and Max
    fmt.Println("Min:", slices.Min(numbers)) // 1
    fmt.Println("Max:", slices.Max(numbers)) // 9
}

Build docs developers (and LLMs) love