Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FunctionName ¶
FunctionName returns the name of the given function. If the input is not a function, it returns an empty string. For named types, it returns the type name. For methods, it returns the method name. For anonymous functions, it returns the full signature.
Parameters:
- fn: any type, expected to be a function.
Returns:
- string: the name of the function or an empty string if the input is not a function.
func IsFunction ¶
IsFunction checks if the provided value is a function. It returns true if the value is a function, otherwise it returns false.
Parameters:
- fn: The value to be checked.
Returns:
- bool: true if the value is a function, false otherwise.
func IsRefinedType ¶
IsRefinedType checks if the provided reflect.Type matches the type of the generic parameter R.
Parameters: - value: The reflect.Type to be checked.
Returns: - bool: True if the type of the generic parameter R matches the provided reflect.Type, otherwise false.
func IsStructMethod ¶
IsStructMethod checks if the provided value is a method expression (e.g., (*Type).Method or Type.Method). It distinguishes between method expressions and regular functions that take struct parameters.
A method expression is created by qualifying a method with a type name, like (*T).Method or T.Method. This is different from: - Regular functions that take struct parameters - Method values (like t.Method where t is an instance) - Anonymous functions
The function works by: 1. Verifying the input is a function with at least one parameter 2. Checking that the first parameter is a struct type (or pointer to struct) 3. Using regex to identify method expressions and extract the type name 4. Comparing the extracted type name with the actual struct type
func IsZero ¶ added in v0.0.10
IsZero checks if a value is the zero value for its type. It handles nil values, pointers, interfaces, and all other types. Returns true if the value is zero or nil, false otherwise.
func ResultImplements ¶ added in v0.1.1
ResultImplements checks if any of the result arguments of a function implements the given interface type T. This is particularly useful for runtime type checking and reflection-based functionality where you need to verify function return types against interfaces.
Design choices:
- Generic implementation allows compile-time type safety for the interface type
- Accepts both function values and reflect.Type for flexibility
- Returns false for nil or non-function inputs rather than panicking
- Uses reflect.TypeOf(&zero).Elem() pattern to get interface type to handle both concrete and interface types
Example usage:
type Stringer interface { String() string }
func returnsStringer() fmt.Stringer { return nil }
func returnsMultiple() (int, fmt.Stringer, error) { return 0, nil, nil }
ResultImplements[fmt.Stringer](returnsStringer) // returns true
ResultImplements[fmt.Stringer](returnsMultiple) // returns true
ResultImplements[error](returnsMultiple) // returns true
ResultImplements[fmt.Stringer](func() int { }) // returns false
Parameters:
- function: Either a function value or reflect.Type of a function. If nil or not a function, returns false.
Returns:
- bool: true if any result argument implements interface T, false otherwise
Types ¶
This section is empty.