SoFunction
Updated on 2025-03-04

Detailed explanation of the usage of functions in the new slices package in Go1.21

Definition is as follows:

func Delete[S ~[]E, E any](s S, i, j int) S

Remove the element s[i:j] from s and return the modified slice. If s[i:j] is not a valid slice of s, then panic. Delete is O(len(s)-j), so if many items have to be deleted, it is better to call delete all at once, rather than delete them one by one. Delete cannot modify the element s[len(s)-(j-i):len(s)]. If these elements contain pointers, consider zeroing these elements so that the objects they refer to can be garbage collected. A simple example is as follows:

package main
import (
    "fmt"
    "slices"
)
func main() {
    letters := []string{"a", "b", "c", "d", "e"}
    letters = (letters, 1, 4)
    (letters) // [a e]
}

Definition is as follows:

func DeleteFunc[S ~[]E, E any](s S, del func(E) bool) S

Remove the element that returns true from s and returns the modified slice. When DeleteFunc deletes m elements, it may not modify the element s[len(s)-m:len(s)]. If these elements contain pointers, consider zeroing these elements so that the objects they refer to can be garbage collected. A simple example is as follows:

package main
import (
    "fmt"
    "slices"
)
func main() {
    seq := []int{0, 1, 1, 2, 3, 5, 8}
    seq = (seq, func(n int) bool {
        return n%2 != 0 // Delete odd numbers    })
    (seq) // [0 2 8]
}

Definition is as follows:

func Equal[S ~[]E, E comparable](s1, s2 S) bool

Determine whether the two slices are equal (the length is the same and all elements are equal). If the length is different, return false. If the lengths are the same, the elements are compared in the order in which the index is incremented, and the comparison is stopped when the first unequal occurs. A simple example is as follows:

package main
import (
    "fmt"
    "slices"
)
func main() {
    numbers := []int{0, 42, 8}
    ((numbers, []int{0, 42, 8})) // true
    ((numbers, []int{10})) // false
}

Definition is as follows:

func EqualFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, eq func(E1, E2) bool) bool

Use a custom function for each pair of elements to determine whether the two slices are equal. If the length is different, return false. If the length is the same, the elements are compared in order of increments of the index and the comparison is stopped at the first index where eq returns false. A simple example is as follows:

package main
import (
    "fmt"
    "slices"
    "strconv"
)
func main() {
    numbers := []int{0, 42, 8}
    strings := []string{"000", "42", "0o10"}
    equal := (numbers, strings, func(n int, s string) bool {
        sn, err := (s, 0, 64)
        if err != nil {
            return false
        }
        return n == int(sn)
    })
    (equal) // true
}

Definition is as follows:

func Grow[S ~[]E, E any](s S, n int) S

Increase the capacity of the slice to provide space for the other n elements. After Grow(n), at least n elements can be added to the slice without further allocation. If n is negative or too large to allocate memory, panic.

Definition is as follows:

func Index[S ~[]E, E comparable](s S, v E) int

Returns the index that v appears for the first time in s, and -1 if it does not exist. A simple example is as follows:

package main
import (
    "fmt"
    "slices"
)
func main() {
    numbers := []int{0, 42, 8}
    ((numbers, 8)) // 2
    ((numbers, 7)) // -1
}

Definition is as follows:

func IndexFunc[S ~[]E, E any](s S, f func(E) bool) int

Returns the first index i that satisfies f(s[i]), and -1 if it is not. A simple example is as follows:

package main
import (
    "fmt"
    "slices"
)
func main() {
    numbers := []int{0, 42, -10, 8}
    i := (numbers, func(n int) bool {
        return n < 0
    })
    ("First negative at index", i) // 2
}

Definition is as follows:

func Insert[S ~[]E, E any](s S, i int, v ...E) S

Insert the value v… into s at index i, returning the modified slice. The elements in s[i:] are moved upward to make room. In the returned slice r, r[i] == v[0], r[i+len(v)] == The value originally at r[i]. If out of range, panic. The complexity of this function is O(len(s) + len(v)). A simple example is as follows:

package main
import (
    "fmt"
    "slices"
)
func main() {
    names := []string{"Alice", "Bob", "Vera"}
    names = (names, 1, "Bill", "Billie")
    names = (names, len(names), "Zac")
    (names) // [Alice Bill Billie Bob Vera Zac]
}

Definition is as follows:

func IsSorted[S ~[]E, E ](x S) bool

Determine whether x is sorted in ascending order. A simple example is as follows:

package main
import (
    "fmt"
    "slices"
)
func main() {
    (([]string{"Alice", "Bob", "Vera"})) // true
    (([]int{0, 2, 1})) // false
}

【References】

Package slices(/pkg/slices/

This is the end of this article about the detailed explanation of the usage of functions in the new slices package in Go1.21. For more related contents of Go1.21 slices package, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!