reflect_utils
reflect_utils is a Go library providing a collection of utility functions to simplify working with the reflect package. It offers helpers for instantiation, type checking, pointer manipulation, and struct tag handling.
Installation
go get github.com/zauberhaus/reflect_utils
Features
Instantiation
NewT any (T, error): Creates a new instance of type T.
NewOf(t reflect.Type) (any, error): Creates a new instance of the given reflect.Type. Handles pointers, slices, maps, structs, and interfaces (specifically context.Context).
NewWithDefaultsOf(t reflect.Type) (any, error): Creates a new instance of the given type and initializes fields based on default struct tags using github.com/creasty/defaults.
Type Checks & Inspection
IsPointer(value any) bool: Checks if a value, reflect.Value, or reflect.Type is a pointer.
IsStruct(value any) bool: Checks if a value is a struct (automatically dereferences pointers).
IsEnum(value any) bool: Checks if a type follows a specific enum convention (looks for an IsA<TypeName> method generated by github.com/dmarkham/enumer).
IsNil(value any) bool: Safely checks if a value is nil, handling typed nils for pointers, maps, channels, and slices.
IsEmpty(object any) bool: Checks if a value is "empty" (nil, zero value, or empty collection).
IsComparable(object any) bool: Checks if a value is comparable.
HasMethod(value any, method string) bool: Checks if a value has a method with the given name.
HasField(value any, field string) bool: Checks if a struct (or pointer to struct) has a field with the given name.
HasDefault(t reflect.Type) bool: Checks if a struct type contains fields with default tags.
Pointer & Memory Utilities
PtrT any *T: Returns a pointer to the given value.
CopyToHeap(val any) any: Copies a value to the heap. If the value is already a pointer, it returns it as is.
FromPointer(val any) any: Dereferences a value if it is a pointer; otherwise returns the value as is.
License
Copyright 2026 Zauberhaus.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0