generic

package
v0.3.15 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewInstance

func NewInstance[T any]() T

NewInstance create an instance of the given type T. the main purpose of this function is to create an instance of a type, can handle the type of T is a pointer or not. eg. NewInstance[int] returns 0. eg. NewInstance[*int] returns *0 (will be ptr of 0, not nil!).

func ParseTypeName

func ParseTypeName(val reflect.Value) string

ParseTypeName returns the name of the type of the given value. It takes a reflect.Value as input and processes it to determine the underlying type. If the type is a pointer, it dereferences it to get the actual type. (the optimization of this function) eg: ParseTypeName(reflect.ValueOf(&&myStruct{})) returns "myStruct" (not "**myStruct")

If the type is a function, it retrieves the function's name, handling both named and anonymous functions. examples of function paths: [package_path].[receiver_type].[func_name] named function: xxx/utils.ParseTypeName method: xxx/utils.(*MyStruct).Method anonymous function: xxx/utils.TestParseTypeName.func6.1

func PtrOf

func PtrOf[T any](v T) *T

PtrOf returns a pointer of T. useful when you want to get a pointer of a value, in some config, for example. eg. PtrOf[int] returns *int. eg. PtrOf[*int] returns **int.

func Reverse

func Reverse[S ~[]E, E any](s S) S

Reverse returns a new slice with elements in reversed order.

func TypeOf

func TypeOf[T any]() reflect.Type

TypeOf returns the type of T. eg. TypeOf[int] returns reflect.TypeOf(int). eg. TypeOf[*int] returns reflect.TypeOf(*int).

Types

type Pair

type Pair[F, S any] struct {
	First  F
	Second S
}

Jump to

Keyboard shortcuts

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