Detailed explanation of the new slices package in Go 1.21 (2)

The new slices package in Go 1.21 provides many slice-related functions that can be used for any type of slice.

slices.Delete

The definition is as follows:

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

Remove element s[i:j] from s and return the modified slice. If s[i:j] is not a valid slice of s, panic will occur. Delete is O(len(s)-j), so if many items must be deleted, it is better to call delete them all at once rather than delete them one by one. Delete cannot modify element s[len(s)-(ji):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 = slices.Delete(letters, 1, 4)
	fmt.Println(letters) // [a e]
	
}

slices.DeleteFunc

The definition is as follows:

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

Removes elements from s for which the del function returns true 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 = slices.DeleteFunc(seq, func(n int) bool {
		return n%2 != 0 // 删除奇数
	})
	fmt.Println(seq) // [0 2 8]
}

slices.Equal

The definition is as follows:

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

Determines whether two slices are equal (same length and all elements equal). If the lengths are different, return false. If the lengths are the same, the elements are compared in increasing index order, stopping at the first inequality. A simple example is as follows:

package main

import (
	"fmt"
	"slices"
)

func main() {
	numbers := []int{0, 42, 8}
	fmt.Println(slices.Equal(numbers, []int{0, 42, 8})) // true
	fmt.Println(slices.Equal(numbers, []int{10})) // false
}

slices.EqualFunc

The 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 lengths are different, return false. If the lengths are the same, the elements are compared in increasing index order, stopping 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 := slices.EqualFunc(numbers, strings, func(n int, s string) bool {
		sn, err := strconv.ParseInt(s, 0, 64)
		if err != nil {
			return false
		}
		return n == int(sn)
	})
	fmt.Println(equal) // true
}

slices.Grow

The definition is as follows:

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

Increase the capacity of the slice to provide room for n additional 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, it will panic.

slices.Index

The definition is as follows:

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 it does not exist. A simple example is as follows:

package main

import (
	"fmt"
	"slices"
)

func main() {
	numbers := []int{0, 42, 8}
	fmt.Println(slices.Index(numbers, 8)) // 2
	fmt.Println(slices.Index(numbers, 7)) // -1
}

slices.IndexFunc

The 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]), or -1 if it does not. A simple example is as follows:

package main

import (
	"fmt"
	"slices"
)

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

slices.Insert

The 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 up to make room. In the returned slice r, r[i] == v[0], r[i+len(v)] == the original value 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 = slices.Insert(names, 1, "Bill", "Billie")
	names = slices.Insert(names, len(names), "Zac")
	fmt.Println(names) // [Alice Bill Billie Bob Vera Zac]
}

slices.IsSorted

The definition is as follows:

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

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

package main

import (
	"fmt"
	"slices"
)

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

【Reference】

Package slices(https://golang.google.cn/pkg/slices/)

Guess you like

Origin blog.csdn.net/luduoyuan/article/details/132385463