conv

package
v1.20.0 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package conv provides generic functions for type conversion and value transformation.

This package offers safe and unsafe conversion utilities between compatible types, including byte/string conversions, reference conversions, and reflection-based operations.

Examples

// Convert bytes to string (zero-copy)
bytes := []byte{'h', 'e', 'l', 'l', 'o'}
str := conv.BytesToString[string](bytes)
fmt.Println(str) // Output: hello

// Convert string to readonly bytes
readonlyBytes := conv.StringToReadonlyBytes("hello")
// Note: modifying readonlyBytes will panic

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BytesToString

func BytesToString[STRING ~string](b []byte) STRING

BytesToString convert []byte type to ~string type.

func Deref

func Deref[T any](t *T) T

Deref returns the value of the pointer type. NOTE:

If the value is nil, it will return the zero T.

func DerefImplType

func DerefImplType(v reflect.Value) reflect.Type

DerefImplType returns the underlying type of the value that implements the interface v.

func DerefInterfaceValue

func DerefInterfaceValue(v reflect.Value) reflect.Value

DerefInterfaceValue returns the value of the underlying type that implements the interface v.

func DerefMap

func DerefMap[K comparable, V any](a map[K]*V) map[K]V

DerefMap convert map[K]*V to map[K]V. NOTE:

If a value is nil, it will be set to the zero V.

func DerefPtrValue

func DerefPtrValue(v reflect.Value) reflect.Value

DerefPtrValue returns the underlying non-pointer type value.

func DerefSlice

func DerefSlice[T any](a []*T) []T

DerefSlice convert []*T to []T. NOTE:

If an element is nil, it will be set to the zero T.

func DerefSliceValue

func DerefSliceValue(v reflect.Value) reflect.Value

DerefSliceValue convert []*T to []T.

func DerefType

func DerefType(t reflect.Type) reflect.Type

DerefType dereference, get the underlying non-pointer type.

func DerefValue

func DerefValue(v reflect.Value) reflect.Value

DerefValue dereference and unpack interface, get the underlying non-pointer and non-interface value.

func Ref

func Ref[T any](t T) *T

Ref returns the address of the value.

func RefMap

func RefMap[K comparable, V any](a map[K]V) map[K]*V

RefMap convert map[K]V to map[K]*V.

func RefSlice

func RefSlice[T any](a []T) []*T

RefSlice convert []T to []*T.

func RefSliceValue

func RefSliceValue(v reflect.Value, ptrDepth int) reflect.Value

RefSliceValue convert []T to []*T, the ptrDepth is the count of '*'.

func RefType

func RefType(t reflect.Type, ptrDepth int) reflect.Type

RefType convert T to *T, the ptrDepth is the count of '*'.

func RefValue

func RefValue(v reflect.Value, ptrDepth int) reflect.Value

RefValue convert T to *T, the ptrDepth is the count of '*'.

func SafeAssert

func SafeAssert[T any](v any) T

SafeAssert asserts any value up to (zero)T.

func SafeAssertMap

func SafeAssertMap[K comparable, V any](a map[K]any) result.Result[map[K]V]

SafeAssertMap convert map[K]any to map[K]V.

func SafeAssertSlice

func SafeAssertSlice[T any](a []any) result.Result[[]T]

SafeAssertSlice convert []any to []T.

func ToAnyMap

func ToAnyMap[K comparable, V any](a map[K]V) map[K]any

ToAnyMap convert map[K]V to map[K]any.

func ToAnySlice

func ToAnySlice[T any](a []T) []any

ToAnySlice convert []T to []any.

func UnsafeAssertMap

func UnsafeAssertMap[K comparable, V any](a map[K]any) map[K]V

UnsafeAssertMap convert map[K]any to map[K]V.

func UnsafeAssertSlice

func UnsafeAssertSlice[T any](a []any) []T

UnsafeAssertSlice convert []any to []T.

func UnsafeConvert

func UnsafeConvert[T any, U any](t T) U

UnsafeConvert convert a value to another type.

func Zero

func Zero[T any]() T

Zero returns the zero value of the type.

Types

type ReadonlyBytes

type ReadonlyBytes = []byte

func StringToReadonlyBytes

func StringToReadonlyBytes[STRING ~string](s STRING) ReadonlyBytes

StringToReadonlyBytes convert ~string to unsafe read-only []byte. NOTE:

panic if modify the member value of the ReadonlyBytes.

Jump to

Keyboard shortcuts

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