util

package
v1.2.4 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2025 License: Apache-2.0 Imports: 14 Imported by: 19

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrForbiddenMethod = errors.New("forbidden method")

ErrForbiddenMethod throws this error when calling a method is prohibited.

View Source
var ErrUnimplementedMethod = errors.New("unimplemented method")

ErrUnimplementedMethod throws this error when calling an unimplemented method.

Functions

func AllOfList added in v1.2.4

func AllOfList[T any](l *list.List) []T

AllOfList returns a slice of all items in the given list.

func FileLine

func FileLine(fn any) (file string, line int, fnName string)

FileLine returns the file, line number, and function name for a given function. It uses reflection and runtime information to extract these details. 'fn' is expected to be a function or method value.

func FormatError added in v1.2.4

func FormatError(err error, format string, args ...any) error

FormatError formats an error message and returns a new error. If the provided error is non-nil, it appends the formatted message before it.

func FuncName added in v1.2.4

func FuncName(fn any) string

FuncName returns the function name for a given function.

func IsBeanInjectionTarget added in v1.2.4

func IsBeanInjectionTarget(t reflect.Type) bool

IsBeanInjectionTarget reports whether the provided reflect.Type is a valid target for bean injection.

Valid targets include:

  • a bean type itself
  • collections (map, slice, array) whose element type is a bean

func IsBeanType added in v1.1.1

func IsBeanType(t reflect.Type) bool

IsBeanType reports whether the provided reflect.Type is considered a "bean" type.

A "bean" type is defined as:

  • a channel type
  • a function type
  • an interface type
  • a pointer to a struct type

func IsConstructor

func IsConstructor(t reflect.Type) bool

IsConstructor reports whether the provided function type is considered a constructor by convention.

A constructor is defined as a function that returns:

  • one non-error value, or
  • two values where the second value is an error.

Examples of valid constructor signatures:

func() *MyStruct
func(cfg Config) (*MyStruct, error)

Examples of invalid constructor signatures:

func()                     // returns nothing
func() error               // returns only an error
func() (*A, *B, error)     // returns more than two values

func IsErrorType

func IsErrorType(t reflect.Type) bool

IsErrorType reports whether the provided reflect.Type represents the built-in error type or a type that implements the error interface.

func IsFuncType

func IsFuncType(t reflect.Type) bool

IsFuncType reports whether the provided reflect.Type represents a function.

func IsPrimitiveValueType

func IsPrimitiveValueType(t reflect.Type) bool

IsPrimitiveValueType reports whether the provided reflect.Type represents a primitive value type such as an integer, unsigned integer, float, string, or boolean.

func IsPropBindingTarget added in v1.2.4

func IsPropBindingTarget(t reflect.Type) bool

IsPropBindingTarget reports whether the provided reflect.Type is a valid target for property binding.

Valid types include:

  • primitive value types
  • struct types
  • collections (map, slice, array) whose element type is a primitive value or a struct

func ListOf added in v1.2.4

func ListOf[T any](a ...T) *list.List

ListOf creates a list of the given items.

func LocalIPv4

func LocalIPv4() string

LocalIPv4 retrieves the first non-loopback IPv4 address of the local machine. The result is cached after the first call using sync.Once.

func MD5

func MD5(str string) string

MD5 computes the MD5 checksum of the given string and returns it as a lowercase hexadecimal string.

func OrderedMapKeys added in v1.2.4

func OrderedMapKeys[M ~map[K]V, K cmp.Ordered, V any](m M) []K

OrderedMapKeys returns the sorted keys of a map whose key type is ordered. This provides a deterministic order for iteration over maps.

func PatchValue

func PatchValue(v reflect.Value) reflect.Value

PatchValue modifies an unexported field to make it assignable by modifying the internal flag. It takes a reflect.Value and returns the patched value that can be written to. This is typically used to manipulate unexported fields in struct types.

func PathExists added in v1.2.4

func PathExists(file string) (bool, error)

PathExists checks whether the specified file or directory exists.

func Ptr added in v1.2.4

func Ptr[T any](i T) *T

Ptr returns a pointer to the given value.

func ReadDirNames

func ReadDirNames(dirname string) ([]string, error)

ReadDirNames reads all entry names in the given directory.

func ReturnNothing

func ReturnNothing(t reflect.Type) bool

ReturnNothing reports whether the provided function type returns no values. It is useful when analyzing function signatures using reflection.

func ReturnOnlyError

func ReturnOnlyError(t reflect.Type) bool

ReturnOnlyError reports whether the provided function type returns exactly one value and that value is an error.

func WrapError added in v1.2.4

func WrapError(err error, format string, args ...any) error

WrapError wraps an existing error with additional context, showing a hierarchical relationship between the new message and the original error.

Types

type FloatType added in v1.2.4

type FloatType interface {
	~float32 | ~float64
}

FloatType is a generic constraint that represents floating-point types: float32 and float64.

type IntType added in v1.2.4

type IntType interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

IntType is a generic constraint that represents all signed integer types: int, int8, int16, int32, and int64.

type UintType added in v1.2.4

type UintType interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

UintType is a generic constraint that represents all unsigned integer types: uint, uint8, uint16, uint32, and uint64.

Jump to

Keyboard shortcuts

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