Documentation ¶
Overview ¶
Package slicex provides some extra slice generic functions.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Convert ¶
func Convert[S1 ~[]E1, E1, E2 any](vs S1, convert func(E1) E2) []E2
Convert converts the slice from []E1 to []E2.
Example ¶
type Ints []int ints1 := []int{1, 2, 3} ints2 := Ints{4, 5, 6} int64s1 := Convert(ints1, func(v int) int64 { return int64(v) }) int64s2 := Convert(ints2, func(v int) int64 { return int64(v) }) fmt.Println(int64s1) fmt.Println(int64s2)
Output: [1 2 3] [4 5 6]
func Interfaces ¶
func Interfaces[S ~[]E, E any](vs S) []interface{}
Interfaces converts []any to []interface{}.
Example ¶
ss := []string{"a", "b", "c"} vs1 := Interfaces(ss) fmt.Printf("%T: %v\n", vs1, vs1) ints := []int{1, 2, 3} vs2 := Interfaces(ints) fmt.Printf("%T: %v\n", vs2, vs2)
Output: []interface {}: [a b c] []interface {}: [1 2 3]
func Make ¶
Make returns a new slice.
If both cap and defaultCap are equal to 0, it is equal to make(S, len). If cap is equal to 0, use defaultCap as cap instead, which is equal to make(S, len, defaultCap).
func Merge ¶
func Merge[S ~[]E, E any](ss ...S) S
Merge merges a set of slices in turn to one slice.
If no arguments, return nil. If all the arguments are empty, return a empty slice with cap==0.
func SetEqual ¶
func SetEqual[S ~[]E, E comparable](vs1, vs2 S) bool
SetEqual reports whether the element set of the two slices are equal.
Example ¶
s1 := []string{"a", "b", "c"} s2 := []string{"b", "c", "a"} s3 := []string{"a", "b", "b"} if SetEqual(s1, s2) { fmt.Printf("%v is equal to %v\n", s1, s2) } else { fmt.Printf("%v is not equal to %v\n", s1, s2) } if SetEqual(s1, s3) { fmt.Printf("%v is equal to %v\n", s1, s3) } else { fmt.Printf("%v is not equal to %v\n", s1, s3) }
Output: [a b c] is equal to [b c a] [a b c] is not equal to [a b b]
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.