xreflect

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: MIT Imports: 1 Imported by: 0

README

xreflect

Reflection helpers to simplify common runtime type operations in Go.

 

This package gathers lightweight helpers that make common reflect-based operations simpler to use. Its intent is to provide small, reusable utilities for runtime type inspection and instance allocation.

At the moment the package exposes a single convenience function but is intended to grow with additional reflection helpers.


Purpose

xreflect centralizes small utilities that reduce boilerplate when working with Go's reflect package. It is especially useful for CLI tools, tests, and factories where dynamic instance allocation and generic convenience functions improve ergonomics.


Installation

Add the package to your module:

  go get github.com/AeonDigital/Go-Core/xreflect@latest

Import it in your code:

import "github.com/AeonDigital/Go-Core/xreflect"

Basic usage

Allocate an initialized instance of a type T at runtime. For value types (e.g., int, struct), NewInstanceOf[T]() returns a non-nil initialized value. For pointer types, it returns a non-nil pointer to the underlying struct type, preventing nil pointer panics.

// For a struct value type
type Config struct { Port int }
cfg := xreflect.NewInstanceOf[Config]() // returns Config with zero-values

// For a pointer type
ptr := xreflect.NewInstanceOf[*Config]() // returns *Config (non-nil)

This helper is intended for dynamic factories, test fixtures, and code that needs to allocate types generically without writing reflection boilerplate.


Supported APIs

Currently the package exposes:

  • xreflect.NewInstanceOf[T any]() T

This function uses reflect under the hood to allocate either a value or a pointer instance depending on the generic type parameter supplied.


External dependencies

xreflect depends only on the Go standard library:

  • reflect

No third-party libraries are required.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewInstanceOf

func NewInstanceOf[T any]() T

NewInstanceOf utilizes generics and reflection to allocate and return a fully initialized instance of type T. If T is a value type, it returns the initialized zero value allocated in memory. If T is a pointer type, it automatically resolves the underlying struct type, allocates the necessary memory for it, and returns the valid pointer, preventing nil pointer panics.

Types

This section is empty.

Jump to

Keyboard shortcuts

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