Searching
Contains ยท Index ยท IndexFuncAll
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
Sort ยท SortFunc ยท SortStableFunc ยท IsSortedslices.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
Binary Search
BinarySearch ยท BinarySearchFunc
BinarySearch and BinarySearchFunc
Binary Search
nums := []int{1, 3, 5, 7, 9, 11} // BinarySearch(s, target) โ index, found i, found := slices.BinarySearch(nums, 7) // i=3, found=true i, found = slices.BinarySearch(nums, 6) // i=3, found=false (6 would be inserted at index 3) // BinarySearchFunc โ for custom types or orderings people := []Person{{"Alice", 25}, {"Bob", 30}, {"Carol", 35}} // must be sorted by the same key first i, found = slices.BinarySearchFunc(people, 30, func(p Person, age int) int { return cmp.Compare(p.Age, age) }) // i=1, found=true (Bob, age 30)
Comparing Slices
Equal ยท EqualFunc ยท Compare
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
Reverse ยท Insert ยท Delete ยท Replace ยท ClipFunctions 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 ยท CompactFunc ยท DeleteFunc
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 ยท Max ยท MinFunc ยท MaxFunc ยท 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
All key functions ยท Go 1.21+| Function | Returns | Description |
|---|---|---|
| slices.Contains(s, v) | bool | True if v is in s |
| slices.ContainsFunc(s, f) | bool | True if any element satisfies f |
| slices.Index(s, v) | int | First index of v, or -1 |
| slices.IndexFunc(s, f) | int | First 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) | bool | True if s is sorted ascending |
| slices.IsSortedFunc(s, cmp) | bool | True if s is sorted by cmp |
| slices.BinarySearch(s, v) | int, bool | Index and found; s must be sorted |
| slices.BinarySearchFunc(s, t, cmp) | int, bool | Binary search with custom comparator |
| slices.Equal(a, b) | bool | True if same length and elements |
| slices.EqualFunc(a, b, eq) | bool | True if equal by custom equality |
| slices.Compare(a, b) | int | Lexicographic comparison: -1, 0, 1 |
| slices.Reverse(s) | โ | Reverse in place |
| slices.Insert(s, i, vs...) | []E | Insert elements at index i |
| slices.Delete(s, i, j) | []E | Remove elements [i, j) |
| slices.DeleteFunc(s, f) | []E | Remove all elements where f returns true |
| slices.Replace(s, i, j, vs...) | []E | Replace [i, j) with vs |
| slices.Compact(s) | []E | Remove consecutive duplicate elements |
| slices.CompactFunc(s, eq) | []E | Remove consecutive elements equal by eq |
| slices.Clone(s) | []E | Shallow copy |
| slices.Grow(s, n) | []E | Ensure capacity for n more elements |
| slices.Clip(s) | []E | Remove unused capacity (len == cap) |
| slices.Min(s) | E | Smallest element; panics if empty |
| slices.Max(s) | E | Largest element; panics if empty |
| slices.MinFunc(s, cmp) | E | Minimum by custom comparator |
| slices.MaxFunc(s, cmp) | E | Maximum by custom comparator |
| slices.Collect(seq) | []E | Gather iter.Seq into a slice (Go 1.23+) |