reflect

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cast

func Cast[T any](v any) (T, error)

Cast casts a value to the provided type. Handles resolving pointers and interfaces. Returns the casted value and an error if the value cannot be casted T: The type to cast to v: The value to cast

func CastType

func CastType(t reflect.Type, v any) (reflect.Value, error)

CastType casts a value to the provided type. Handles resolving pointers and interfaces. Returns the casted value and an error if the value cannot be casted t: The type to cast to v: The value to cast

func GetIntefaceName

func GetIntefaceName[T any]() string

GetIntefaceName returns the name of the interface in the format `modulePath.interfaceName` T: The type to get the name of

func GetMethodByName

func GetMethodByName(t reflect.Type, name string) (reflect.Method, bool)

GetMethodByName gets a method by name. If the method is not found, it will check for a pointer to the type and return the method if found. Returns the method and a bool indicating if the method was found t: The type to get the method from name: The name of the method

func GetPointerOfValue

func GetPointerOfValue(val reflect.Value) any

GetPointerOfValue gets the pointer of a value. If the value is already a pointer, it will return the value. If the value is not addressable, it will return the value. Otherwise, it will return the address of the value val: The value to get the pointer of

func GetReflectTypeName

func GetReflectTypeName(t reflect.Type) string

GetReflectTypeName returns the name of the type in the format `modulePath.typeName` t: The type to get the name of

func NewStructInstance

func NewStructInstance(t reflect.Type) (reflect.Value, error)

NewStructInstance creates a new instance of a struct. Throws an error if t is not a struct t: The type of the struct

func SameType

func SameType[A any, B any]() bool

SameType checks if the provided types are the same. Handles resolving interfaces. Returns true if the types are the same, false otherwise A: The first type to compare B: The second type to compare

func SetField

func SetField(target reflect.Value, field reflect.StructField, value reflect.Value) error

SetField sets the value of a field. Resolves differences with interfaces and pointers. Handles unsafe setting of fields. Returns an error if the field cannot be set target: The target struct to set the field on field: The field to set value: The value to set the field to

Types

This section is empty.

Jump to

Keyboard shortcuts

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