anyutil

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2025 License: MIT Imports: 5 Imported by: 9

README

anyutil

Package anyutil contains utility functions for the goradd framework that operate on generic types and interface{} (any) types.

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnyMap

func AnyMap[K cmp.Ordered, V any](m map[K]V) (o map[K]any)

AnyMap converts a map[K]V to a map[K]any.

func As

func As[T error](err error) (T, bool)

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

func FieldMap(s any) map[string]any

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

func IsNil(i any) bool

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 IsSlice

func IsSlice(value any) bool

IsSlice returns true if value is a slice

func Join

func Join[K any](values []K, sep string) (out string)

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 SetFields

func SetFields(s any, values map[string]any) error

SetFields will restore to s values that were extracted using FieldMap

func SortedKeys

func SortedKeys[K cmp.Ordered, V any](m map[K]V) []K

SortedKeys returns the keys of a map in sort order. The keys must be sortable, of course.

func StringMap

func StringMap[K cmp.Ordered](m map[K]any) (o map[K]string)

StringMap converts a map[K]any to a map[K]string. It will attempt to cast to a string, and if that fails, will use fmt.Print to do the conversion to string.

func Zero

func Zero[T any]() T

Zero returns the zero value of a type.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL