-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslice_conv.go
50 lines (44 loc) · 1.09 KB
/
slice_conv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package gofn
// ToIfaceSlice convert a slice to a slice of interface
func ToIfaceSlice[T any, S ~[]T](s S) []any {
result := make([]any, len(s))
for i := range s {
result[i] = s[i]
}
return result
}
// Deprecated: use ToIfaceSlice instead
func ToIntfSlice[T any, S ~[]T](s S) []any {
return ToIfaceSlice(s)
}
// ToStringSlice converts str-approximate slice to string slice
func ToStringSlice[U, T ~string, S ~[]T](slice S) []U {
result := make([]U, len(slice))
for i := range slice {
result[i] = U(slice[i])
}
return result
}
// ToNumberSlice converts int-approximate slice to int slice
func ToNumberSlice[U, T NumberExt, S ~[]T](slice S) []U {
result := make([]U, len(slice))
for i := range slice {
result[i] = U(slice[i])
}
return result
}
// ToSlice returns a slice for individual input arguments
func ToSlice[T any](s ...T) []T {
if s == nil {
return []T{}
}
return s
}
// ToPtrSlice returns a slice of pointers point to the input slice's elements
func ToPtrSlice[T any, S ~[]T](s S) []*T {
result := make([]*T, len(s))
for i := range s {
result[i] = &s[i]
}
return result
}