Definition is as follows:
func BinarySearch[S ~[]E, E ](x S, target E) (int, bool)
Search for the target in already sorted slices (slices must be sorted in incremental order). If found, return the location and true. If not found, return the location and false where the target should be found. A simple example is as follows:
package main import ( "fmt" "slices" ) func main() { names := []string{"Alice", "Bob", "Vera"} n, found := (names, "Vera") ("Vera:", n, found) // Vera: 2 true n, found = (names, "Bill") ("Bill:", n, found) // Bill: 1 false }
Definition is as follows:
func BinarySearchFunc[S ~[]E, E, T any](x S, target T, cmp func(E, T) int) (int, bool)
The function is similar, the difference is to use a custom comparison function. The slices must be sorted in incremental order, where "increment" is defined by cmp. If the slice element matches the target, CMP should return 0, if the slice element is before the target, a negative number, and if the slice element is after the target, a positive number. CMP must implement the same sort as slices, so that if CMP (a, t) < 0 and CMP (b, t) >= 0, the point-sharp a must be before b.
package main import ( "cmp" "fmt" "slices" ) func main() { type Person struct { Name string Age int } people := []Person{ {"Alice", 55}, {"Bob", 24}, {"Gopher", 13}, } n, found := (people, Person{"Bob", 0}, func(a, b Person) int { return (, ) }) ("Bob:", n, found) // Bob: 1 true }
Definition is as follows:
func Clip[S ~[]E, E any](s S) S
Remove unused capacity from the slice and return s[:len(s):len(s)]. A simple example is as follows:
package main import ( "fmt" "slices" ) func main() { names := make([]string, 2, 5) names = (names) ("Length: %d, Capacity: %d", len(names), cap(names)) // Length: 2, capacity: 2}
Definition is as follows:
func Clone[S ~[]E, E any](s S) S
Returns a copy of the slice. Because the elements are copied using assignment, this is a shallow clone. The simple and practical methods are as follows:
package main import ( "fmt" "slices" ) func main() { names := []string{"Ludoshin's Blog", "Lu Duosin's thoughts"} namesCopy := (names) (namesCopy) }
Definition is as follows:
func Compact[S ~[]E, E comparable](s S) S
Change the consecutive element into one, similar to the uniq command on Unix. Compact will modify the content of the slice and return the modified slice, which may become smaller in length. A simple example is as follows:
package main import ( "fmt" "slices" ) func main() { seq := []int{0, 1, 1, 2, 5, 5, 5, 8} seq = (seq) (seq) // [0 1 2 5 8] }
Definition is as follows:
func CompactFunc[S ~[]E, E any](s S, eq func(E, E) bool) S
Similar to , the difference is to use custom functions to compare elements. If the element's running results are equal, CompactFunc will retain the first element. A simple example is as follows:
package main import ( "fmt" "slices" "strings" ) func main() { names := []string{"bob", "Bob", "alice", "Vera", "VERA"} names = (names, func(a, b string) bool { return (a) == (b) }) (names) // [bob alice Vera] }
Definition is as follows:
func Compare[S ~[]E, E ](s1, s2 S) int
Use the function to compare elements of s1 and s2. Compare each pair of elements in order until one element is not equal to another. Returns the result of the first mismatched element. If the two slices are equal before one of them ends, the shorter slice is considered smaller than the longer slice. If s1 == s2, the result is 0; if s1 < s2, the result is -1; if s1 > s2, the result is 1.
package main import ( "fmt" "slices" ) func main() { names := []string{"Alice", "Bob", "Vera"} ("Equal:", (names, []string{"Alice", "Bob", "Vera"})) ("V < X:", (names, []string{"Alice", "Bob", "Xena"})) ("V > C:", (names, []string{"Alice", "Bob", "Cat"})) ("3 > 2:", (names, []string{"Alice", "Bob"})) }
The operation results are as follows:
Equal: 0
V < X: -1
V > C: 1
3 > 2: 1
Definition is as follows:
func CompareFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, cmp func(E1, E2) int) int
Similar to , the difference is to use a custom comparison function for comparison. The result is the first non-zero result of cmp; if cmp always returns 0, if len(s1) == len(s2) the result is 0, if len(s1) < len(s2) the result is -1, if len(s1) > len(s2) the result is 1. A simple example is as follows:
package main import ( "cmp" "fmt" "slices" "strconv" ) func main() { numbers := []int{0, 43, 8} strings := []string{"0", "0", "8"} result := (numbers, strings, func(n int, s string) int { sn, err := (s) if err != nil { return 1 } return (n, sn) }) (result) // 1 }
Definition is as follows:
func Contains[S ~[]E, E comparable](s S, v E) bool
Used to determine whether s contains v. A simple example is as follows:
package main import ( "fmt" "slices" ) func main() { names := []string{"Alice", "Bob", "Vera"} ((names, "Bob")) // true }
Definition is as follows:
func ContainsFunc[S ~[]E, E any](s S, f func(E) bool) bool
Used to determine whether at least one element in s satisfies f(e). A simple example is as follows:
package main import ( "fmt" "slices" ) func main() { numbers := []int{0, 42, -10, 8} hasNegative := (numbers, func(n int) bool { return n < 0 }) ("Has a negative:", hasNegative)// true hasOdd := (numbers, func(n int) bool { return n%2 != 0 }) ("Has an odd number:", hasOdd) // false }
【References】
Package slices(/pkg/slices/)
The above is a detailed explanation of the usage of the new slices package in Go1.21. For more information about the Go1.21 slices package, please follow my other related articles!