Go slices Package

Generic slice helpers added in Go 1.21 โ€” search, sort, compare, modify, and deduplicate without writing the boilerplate yourself.

Contains / Index Sort / SortFunc BinarySearch Reverse Compact Max / Min Equal / Compare Insert / Delete
๐Ÿ”

Searching

โ„น๏ธ
All slices functions are generic โ€” they work on any slice type without type assertions or interface{} casting. Requires Go 1.21+.
Contains and Index Linear search
nums := []int{1, 2, 3, 4, 5}

slices.Contains(nums, 3)   // true
slices.Contains(nums, 9)   // false

slices.Index(nums, 3)      // 2  โ€” first index of 3
slices.Index(nums, 9)      // -1 โ€” not found

words := []string{"go", "rust", "zig"}
slices.Contains(words, "rust")  // true
slices.Index(words, "zig")     // 2
IndexFunc and ContainsFunc โ€” predicate-based search Func variants
type Person struct{ Name string; Age int }
people := []Person{
    {"Alice", 30},
    {"Bob", 17},
    {"Carol", 25},
}

// IndexFunc โ€” index of first element matching predicate
i := slices.IndexFunc(people, func(p Person) bool {
    return p.Age < 18
})
// i = 1 (Bob)

// ContainsFunc โ€” true if any element matches
hasMinor := slices.ContainsFunc(people, func(p Person) bool {
    return p.Age < 18
})
// hasMinor = true
๐Ÿ“Š

Sorting

๐Ÿ’ก
slices.Sort works on any slice of an ordered type. slices.SortFunc takes a comparison function that returns int (negative/zero/positive), not a bool like sort.Slice. Use cmp.Compare to build comparators.
slices.Sort โ€” ordered types Sort
// Works on any slice of an ordered type (int, string, float, ...)
nums := []int{5, 2, 8, 1, 9}
slices.Sort(nums)
// [1 2 5 8 9]

words := []string{"banana", "apple", "cherry"}
slices.Sort(words)
// [apple banana cherry]

slices.IsSorted(nums)   // true
slices.IsSorted(words)  // true
slices.SortFunc โ€” custom comparator SortFunc
import "cmp"

people := []Person{{"Alice", 30}, {"Bob", 25}, {"Carol", 30}}

// Sort by Age, then Name
slices.SortFunc(people, func(a, b Person) int {
    if n := cmp.Compare(a.Age, b.Age); n != 0 {
        return n
    }
    return cmp.Compare(a.Name, b.Name)
})
// [{Bob 25} {Alice 30} {Carol 30}]

// Descending โ€” negate the comparison
slices.SortFunc(nums, func(a, b int) int {
    return cmp.Compare(b, a)  // note: b, a (reversed)
})
SortStableFunc โ€” preserve equal element order Stable
// SortStableFunc guarantees equal elements keep their original order
slices.SortStableFunc(people, func(a, b Person) int {
    return cmp.Compare(a.Age, b.Age)
})

// IsSortedFunc โ€” check with a custom comparator
sorted := slices.IsSortedFunc(people, func(a, b Person) int {
    return cmp.Compare(a.Age, b.Age)
})
// sorted = true
โš–๏ธ

Comparing Slices

Equal and EqualFunc Equal
// Equal โ€” same length, same elements in order
slices.Equal([]int{1, 2, 3}, []int{1, 2, 3})  // true
slices.Equal([]int{1, 2}, []int{1, 2, 3})     // false

// EqualFunc โ€” custom element equality
slices.EqualFunc(
    []string{"Go", "Rust"},
    []string{"go", "rust"},
    strings.EqualFold,  // case-insensitive
)
// true
Compare and CompareFunc Compare
// Compare โ€” lexicographic: -1, 0, or 1
slices.Compare([]int{1, 2}, []int{1, 3})  // -1
slices.Compare([]int{1, 2}, []int{1, 2})  // 0
slices.Compare([]int{1, 3}, []int{1, 2})  // 1
// Shorter slice is less if it's a prefix
slices.Compare([]int{1}, []int{1, 2})    // -1
โœ๏ธ

Modifying Slices

โš ๏ธ
Functions like Insert, Delete, and Replace may modify and return the original slice or a new one. Always use the returned value โ€” don't assume the original is unchanged.
Reverse โ€” in-place reversal Reverse
nums := []int{1, 2, 3, 4, 5}
slices.Reverse(nums)
// [5 4 3 2 1]

// Sort descending: sort then reverse
slices.Sort(nums)
slices.Reverse(nums)
// [5 4 3 2 1]
Insert and Delete Insert / Delete
s := []int{1, 2, 4, 5}

// Insert(s, i, values...) โ€” insert at index i
s = slices.Insert(s, 2, 3)
// [1 2 3 4 5]

// Insert multiple values
s = slices.Insert(s, 0, -1, 0)
// [-1 0 1 2 3 4 5]

// Delete(s, i, j) โ€” remove elements [i, j)
s = slices.Delete(s, 0, 2)
// [1 2 3 4 5]
Replace and Clip Replace / Clip
s := []int{1, 2, 3, 4, 5}

// Replace(s, i, j, values...) โ€” replace [i,j) with values
s = slices.Replace(s, 1, 3, 20, 30, 40)
// [1 20 30 40 4 5]

// Clip โ€” remove unused capacity
// Useful before storing in a long-lived struct
s = s[:3]           // len=3, cap still large
s = slices.Clip(s)  // len=3, cap=3
Grow and Clone Memory
// Grow โ€” pre-allocate capacity for n more elements
s := make([]int, 0)
s = slices.Grow(s, 100)  // cap >= 100, avoids reallocations
for i := range 100 {
    s = append(s, i)
}

// Clone โ€” shallow copy
original := []int{1, 2, 3}
copy := slices.Clone(original)
copy[0] = 99
// original is unchanged: [1 2 3]
๐Ÿงน

Dedup & Filter

Compact โ€” remove consecutive duplicates Compact
// Compact removes consecutive duplicate elements (like Unix uniq)
// Sort first if you want to deduplicate the whole slice
s := []int{1, 1, 2, 3, 3, 3, 4, 1}
s = slices.Compact(s)
// [1 2 3 4 1]  โ€” only consecutive dups removed

// Full dedup: sort then compact
slices.Sort(s)
s = slices.Compact(s)
// [1 2 3 4]

// CompactFunc โ€” custom equality
words := []string{"Go", "go", "GO", "Rust"}
words = slices.CompactFunc(words, strings.EqualFold)
// ["Go" "Rust"]  โ€” consecutive case-insensitive dups removed
DeleteFunc โ€” filter out elements matching a predicate DeleteFunc
// DeleteFunc removes all elements where f returns true
nums := []int{1, 2, 3, 4, 5, 6}
nums = slices.DeleteFunc(nums, func(n int) bool {
    return n%2 == 0  // remove even numbers
})
// [1 3 5]

// Filter out empty strings
parts := []string{"a", "", "b", "", "c"}
parts = slices.DeleteFunc(parts, func(s string) bool {
    return s == ""
})
// ["a" "b" "c"]
๐Ÿ†

Min, Max & Collect

Min and Max Min / Max
nums := []int{5, 2, 8, 1, 9, 3}

slices.Min(nums)  // 1
slices.Max(nums)  // 9

words := []string{"banana", "apple", "cherry"}
slices.Min(words)  // "apple"
slices.Max(words)  // "cherry"

// panics on empty slice โ€” check length first
if len(nums) > 0 {
    m := slices.Min(nums)
    _ = m
}
MinFunc and MaxFunc Func variants
people := []Person{
    {"Alice", 30},
    {"Bob", 25},
    {"Carol", 35},
}

youngest := slices.MinFunc(people, func(a, b Person) int {
    return cmp.Compare(a.Age, b.Age)
})
// {Bob 25}

oldest := slices.MaxFunc(people, func(a, b Person) int {
    return cmp.Compare(a.Age, b.Age)
})
// {Carol 35}
slices.Collect โ€” gather an iterator into a slice (Go 1.23+) Go 1.23+
import (
    "maps"
    "slices"
)

// Collect turns any iter.Seq into a slice
m := map[string]int{"a": 1, "b": 2, "c": 3}
keys := slices.Collect(maps.Keys(m))
// ["a" "b" "c"] (order not guaranteed โ€” sort if needed)
slices.Sort(keys)

vals := slices.Collect(maps.Values(m))
// [1 2 3] (order not guaranteed)
๐Ÿ“‹

Quick Reference

FunctionReturnsDescription
slices.Contains(s, v)boolTrue if v is in s
slices.ContainsFunc(s, f)boolTrue if any element satisfies f
slices.Index(s, v)intFirst index of v, or -1
slices.IndexFunc(s, f)intFirst index where f returns true, or -1
slices.Sort(s)โ€”Sort ordered slice in place
slices.SortFunc(s, cmp)โ€”Sort with custom comparison function
slices.SortStableFunc(s, cmp)โ€”Stable sort with custom comparison
slices.IsSorted(s)boolTrue if s is sorted ascending
slices.IsSortedFunc(s, cmp)boolTrue if s is sorted by cmp
slices.BinarySearch(s, v)int, boolIndex and found; s must be sorted
slices.BinarySearchFunc(s, t, cmp)int, boolBinary search with custom comparator
slices.Equal(a, b)boolTrue if same length and elements
slices.EqualFunc(a, b, eq)boolTrue if equal by custom equality
slices.Compare(a, b)intLexicographic comparison: -1, 0, 1
slices.Reverse(s)โ€”Reverse in place
slices.Insert(s, i, vs...)[]EInsert elements at index i
slices.Delete(s, i, j)[]ERemove elements [i, j)
slices.DeleteFunc(s, f)[]ERemove all elements where f returns true
slices.Replace(s, i, j, vs...)[]EReplace [i, j) with vs
slices.Compact(s)[]ERemove consecutive duplicate elements
slices.CompactFunc(s, eq)[]ERemove consecutive elements equal by eq
slices.Clone(s)[]EShallow copy
slices.Grow(s, n)[]EEnsure capacity for n more elements
slices.Clip(s)[]ERemove unused capacity (len == cap)
slices.Min(s)ESmallest element; panics if empty
slices.Max(s)ELargest element; panics if empty
slices.MinFunc(s, cmp)EMinimum by custom comparator
slices.MaxFunc(s, cmp)EMaximum by custom comparator
slices.Collect(seq)[]EGather iter.Seq into a slice (Go 1.23+)