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.
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.
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.
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.
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.
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.
Contains
func Contains[S ~[]E, E comparable](s S, v E) bool
Reports whether v is present in s.
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).
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).
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.
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.
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.
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.
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.
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.
IsSorted
func IsSorted[S ~[]E, E cmp.Ordered](x S) bool
Reports whether x is sorted in ascending order.
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.
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.
The sorted slice to search
The position where target is found or should be inserted
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.
The sorted slice to search
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.
The slice to search (must not be empty)
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.
The slice to search (must not be empty)
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.
The slice to search (must not be empty)
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.
The slice to search (must not be empty)
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.
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.
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.
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.
Number of additional elements to make room for
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)].
Reverse
func Reverse[S ~[]E, E any](s S)
Reverses the elements of the slice in place.
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.
The slices to concatenate
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).
Number of times to repeat (must be non-negative)
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]
}
Search
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
}