Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Equal ¶
func Equal[S ~[]E, E comparable](s1, s2 S) bool
Equal reports whether two slices are equal, meaning they have the same length and all corresponding elements are equal. If the slices have different lengths, Equal returns false immediately. The comparison proceeds in increasing index order, stopping at the first unequal pair. Empty and nil slices are considered equal, while floating point NaNs are not considered equal.
Example:
Equal([]int{1, 2, 3}, []int{1, 2, 3}) // returns true Equal([]int{1, 2}, []int{1, 2, 3}) // returns false
(Introduced in Go 1.21)
Example ¶
package main import ( "fmt" "github.com/exonlabs/go-utils/pkg/abc/slicex" ) func main() { // Using Equal to compare slices slice1 := []int{1, 2, 3} slice2 := []int{1, 2, 3} slice3 := []int{1, 2, 4} fmt.Println(slicex.Equal(slice1, slice2)) fmt.Println(slicex.Equal(slice1, slice3)) }
Output: true false
func Index ¶
func Index[S ~[]E, E comparable](s S, v E) int
Index returns the index of the first occurrence of the value v in the slice s, or -1 if the value is not present in the slice.
Example:
Index([]string{"apple", "banana", "cherry"}, "banana") // returns 1 Index([]string{"apple", "banana", "cherry"}, "grape") // returns -1
(Introduced in Go 1.21)
Example ¶
package main import ( "fmt" "github.com/exonlabs/go-utils/pkg/abc/slicex" ) func main() { // Finding the index of an element in a slice slice := []string{"apple", "banana", "cherry"} index := slicex.Index(slice, "banana") fmt.Println(index) index = slicex.Index(slice, "grape") fmt.Println(index) }
Output: 1 -1
func Reverse ¶
func Reverse[S ~[]E, E any](s S)
Reverse reverses the elements of the slice s in place. This operation modifies the original slice, reversing the order of its elements without allocating additional space.
Example:
s := []int{1, 2, 3, 4} Reverse(s) // s is now []int{4, 3, 2, 1}
(Introduced in Go 1.21)
Example ¶
package main import ( "fmt" "github.com/exonlabs/go-utils/pkg/abc/slicex" ) func main() { // Reversing a slice in place slice := []int{1, 2, 3, 4} slicex.Reverse(slice) fmt.Println(slice) }
Output: [4 3 2 1]
func ReverseCopy ¶
func ReverseCopy[S ~[]T, T any](s S) S
ReverseCopy creates a new slice that is a reversed copy of the original slice s. It does not modify the original slice, returning a new slice with elements in reverse order.
Example:
s := []int{1, 2, 3} reversed := ReverseCopy(s) // reversed is now []int{3, 2, 1}
(The original slice s remains unchanged)
Example ¶
package main import ( "fmt" "github.com/exonlabs/go-utils/pkg/abc/slicex" ) func main() { // Creating a reversed copy of a slice slice := []int{1, 2, 3} reversed := slicex.ReverseCopy(slice) fmt.Println(reversed) fmt.Println(slice) }
Output: [3 2 1] [1 2 3]
func SplitN ¶
SplitN splits the iterable slice s into slices of fixed length n. If the length of s is not a multiple of n, the last slice will contain the remaining elements. Returns a slice of slices.
Example:
s := []int{1, 2, 3, 4, 5} result := SplitN(s, 2) // result is now [][]int{{1, 2}, {3, 4}, {5}}
If n is less than or equal to 0, it returns an empty slice.
Example ¶
package main import ( "fmt" "github.com/exonlabs/go-utils/pkg/abc/slicex" ) func main() { // Splitting a slice into fixed-length sub-slices slice := []int{1, 2, 3, 4, 5} result := slicex.SplitN(slice, 2) fmt.Println(result) }
Output: [[1 2] [3 4] [5]]
Types ¶
This section is empty.