Documentation
¶
Overview ¶
Package all contains utilities that operate on generic types and interface{} (any) types.
Package any has general purpose utility functions for working with interfaces and generic types.
Index ¶
- func AnyMap[K cmp.Ordered, V any](m map[K]V) (o map[K]any)
- func As[T error](err error) (T, bool)
- func FieldMap(s any) map[string]any
- func If[T1 comparable, T any](cond T1, i1 T, i2 T) T
- func IsFloat(val interface{}) bool
- func IsInteger(val interface{}) bool
- func IsNil(i any) bool
- func IsSlice(value any) bool
- func Join[K any](values []K, sep string) (out string)
- func MapSlice[T, K any](i []K) (o []T)
- func MapSliceFunc[K, T any](i []K, f func(j K) T) (o []T)
- func Or[T comparable](vals ...T) T
- func SetFields(s any, values map[string]any) error
- func SortedKeys[K cmp.Ordered, V any](m map[K]V) []K
- func StringMap[K cmp.Ordered](m map[K]any) (o map[K]string)
- func Zero[T any]() T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func As ¶
As is a generic version of errors.As. It returns the first error in err’s chain that matches type T, return true if found. T should be a pointer to a custom error struct type. For example: *MyCustomError
func FieldMap ¶
FieldMap returns a map of names and values of the top-level exported fields in struct s. Anonymous fields are ignored, as well as non-exported fields. s can be a pointer or an interface to a struct as well.
func If ¶
func If[T1 comparable, T any](cond T1, i1 T, i2 T) T
If returns the first item if cond is not a zero value (like false), or the second item if it is the zero value.
Example ¶
i := 1 s := "" fmt.Println(If(i == 1, 1, 2)) fmt.Println(If(s, "yes", "no"))
Output: 1 no
func IsFloat ¶
func IsFloat(val interface{}) bool
IsFloat returns true if the given value is a float32 or float64.
func IsInteger ¶
func IsInteger(val interface{}) bool
IsInteger returns true if the given value is a variant of an integer type.
func IsNil ¶
IsNil is a safe test for nil for any kind of variable, and will not panic If i points to a nil object, IsNil will return true, as opposed to i==nil which will return false
func MapSlice ¶
func MapSlice[T, K any](i []K) (o []T)
MapSlice converts a slice of K to a slice of T. This will only work if T is an interface{} type, i contains interfaces to T, or K is convertible to T by a type cast.
func MapSliceFunc ¶
func MapSliceFunc[K, T any](i []K, f func(j K) T) (o []T)
MapSliceFunc converts a slice of K to a slice of T using the mapping function f.
func Or ¶
func Or[T comparable](vals ...T) T
Or returns the first of its arguments that is not equal to the zero value. If no argument is non-zero, it returns the zero value.
Example ¶
fmt.Println(Or("yes", "no"))
fmt.Println(Or("", "no"))
Output: yes no
func SortedKeys ¶
SortedKeys returns the keys of a map in sort order. The keys must be sortable, of course.
Types ¶
This section is empty.