Documentation
¶
Overview ¶
Package anys provides utilities for working with slices of any type.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Append ¶
Append takes a slice of any and appends the provided values to it.
This function is particularly useful when you need to append elements from a slice of a specific type (e.g., []int) to a slice of type []any, which cannot be done directly using Go's built-in append function.
Example usage:
a := []any{1, "two"}
ints := []int{3, 4, 5}
// The following line would cause a compile-time error because
// we cannot directly append a slice of int to a slice of any.
// a = append(a, ints...)
// Instead, use the custom Append function from the 'anys' package:
a = anys.Append(a, ints...)
fmt.Println(a)
// Output: [1 two 3 4 5]
Example ¶
package main
import (
"fmt"
"github.com/goaux/anys"
)
func main() {
a := []any{1, "two"}
ints := []int{3, 4, 5}
// The following line would cause a compile-time error because we cannot
// directly append a slice of int to a slice of any.
// a = append(a, ints...)
// Instead, use the custom Append function from the 'anys' package to achieve this:
a = anys.Append(a, ints...)
fmt.Println(a)
}
Output: [1 two 3 4 5]
func AppendBackTo ¶
func AppendBackTo[T, S any](t []T, s ...S) []T
AppendBackTo returns a slice of type T by appending values that have been type asserted to T from the provided arguments. If a value cannot be asserted to type T, the corresponding element is set to T's zero value.
func AppendMap ¶
func AppendMap[T, S any](t []T, conv func(S) T, s ...S) []T
AppendMap returns a slice of type T by applying the conversion function to each provided value and appending the result to the given slice.
func BackTo ¶
func BackTo[T, S any](s ...S) []T
BackTo returns a slice of type T by attempting to assert each element in the provided slice `s` to type T. If an element cannot be asserted to type T, it is set to the zero value of T.
This function is useful for converting a slice of any (interface{}) values back to their original type.
Example:
ii := []int{1, 2, 3}
aa := anys.From(ii...)
i2 := anys.BackTo[int](aa...) // Convert the slice of any back to a slice of int
Example ¶
package main
import (
"fmt"
"reflect"
"github.com/goaux/anys"
)
func main() {
ii := []int{1, 2, 3}
aa := anys.From(ii...)
i2 := anys.BackTo[int](aa...)
fmt.Println(reflect.DeepEqual(ii, i2))
}
Output: true
func From ¶
From returns a slice of type `any` containing the provided values.
The From function is a utility that converts a slice of any type into a slice of `any`. This is particularly useful when you need to work with a homogeneous collection of elements of different types or when interfacing with APIs that require a slice of `any`.
Example:
ii := []int{1, 2, 3}
aa := anys.From(ii...) // Converts the slice of integers into a slice of any
Example ¶
package main
import (
"fmt"
"reflect"
"github.com/goaux/anys"
)
func main() {
ints := []int{3, 4, 5}
x := anys.From(ints...) // From is equivalent to the special case of Map
y := anys.Map(func(i int) any { return any(i) }, ints...)
fmt.Println(reflect.DeepEqual(x, y))
}
Output: true
func Map ¶
func Map[T, S any](conv func(S) T, s ...S) []T
Map returns a slice of type T obtained by applying the conversion function to each element of the input values.
Example ¶
package main
import (
"fmt"
"strconv"
"strings"
"github.com/goaux/anys"
)
func main() {
ii := []int{1, 2, 3}
s := strings.Join(
anys.Map(func(i int) string { return strconv.Itoa(i) }, ii...),
",",
)
fmt.Println(s)
}
Output: 1,2,3
Types ¶
This section is empty.