Documentation
¶
Overview ¶
Package sliceutil provides methods for working with slices
Index ¶
- func Contains[K comparable](slice []K, value K) bool
- func Copy[K comparable](slice []K) []K
- func Deduplicate[K comparable](slice []K) []K
- func ErrorToString(data []error) []string
- func Exclude[K comparable](slice []K, items ...K) []K
- func Index[K comparable](slice []K, item K) int
- func IntToInterface(data []int) []any
- func IsEqual[K comparable](s1, s2 []K) bool
- func StringToError(data []string) []error
- func StringToInterface(data []string) []any
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
func Contains[K comparable](slice []K, value K) bool
Contains checks if string slice contains some value
func Copy ¶
func Copy[K comparable](slice []K) []K
Copy creates copy of given slice
Example ¶
s1 := []string{"A", "B", "C"} s2 := Copy(s1) fmt.Printf("%v\n", s2)
Output: [A B C]
func Deduplicate ¶
func Deduplicate[K comparable](slice []K) []K
Deduplicate removes duplicates from slice. Slice must be sorted before deduplication.
Example ¶
s := []string{"A", "A", "A", "B", "C", "C", "D"} fmt.Println(Deduplicate(s))
Output: [A B C D]
func ErrorToString ¶
ErrorToString converts slice with errors to slice with strings
Example ¶
s := []error{fmt.Errorf("error1")} fmt.Printf("%v\n", ErrorToString(s))
Output: [error1]
func Exclude ¶
func Exclude[K comparable](slice []K, items ...K) []K
Exclude removes items from slice
Example ¶
s := []string{"A", "B", "C", "D"} fmt.Println(Exclude(s, "B", "D"))
Output: [A C]
func Index ¶
func Index[K comparable](slice []K, item K) int
Index returns index of given item in a slice or -1 otherwise
Example ¶
s := []string{"A", "B", "C"} fmt.Println(Index(s, "C")) fmt.Println(Index(s, "D"))
Output: 2 -1
func IntToInterface ¶
IntToInterface converts slice with ints to slice with any
Example ¶
s := []int{1, 2} fmt.Printf("%v\n", IntToInterface(s))
Output: [1 2]
func IsEqual ¶ added in v12.120.0
func IsEqual[K comparable](s1, s2 []K) bool
IsEqual compares two slices and returns true if the slices are equal
Example ¶
s1 := []int{1, 2, 3, 4} s2 := []int{1, 2, 3, 4} s3 := []int{1, 3, 2, 4} fmt.Printf("%v == %v → %t\n", s1, s2, IsEqual(s1, s2)) fmt.Printf("%v == %v → %t\n", s2, s3, IsEqual(s2, s3))
Output: [1 2 3 4] == [1 2 3 4] → true [1 2 3 4] == [1 3 2 4] → false
func StringToError ¶
StringToError converts slice with strings to slice with errors
func StringToInterface ¶
StringToInterface converts slice with strings to slice with any
Example ¶
s := []string{"A", "B"} fmt.Printf("%v\n", StringToInterface(s))
Output: [A B]
Types ¶
This section is empty.