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 ¶
- func BytesToString[STRING ~string](b []byte) STRING
- func Deref[T any](t *T) T
- func DerefImplType(v reflect.Value) reflect.Type
- func DerefInterfaceValue(v reflect.Value) reflect.Value
- func DerefMap[K comparable, V any](a map[K]*V) map[K]V
- func DerefPtrValue(v reflect.Value) reflect.Value
- func DerefSlice[T any](a []*T) []T
- func DerefSliceValue(v reflect.Value) reflect.Value
- func DerefType(t reflect.Type) reflect.Type
- func DerefValue(v reflect.Value) reflect.Value
- func Ref[T any](t T) *T
- func RefMap[K comparable, V any](a map[K]V) map[K]*V
- func RefSlice[T any](a []T) []*T
- func RefSliceValue(v reflect.Value, ptrDepth int) reflect.Value
- func RefType(t reflect.Type, ptrDepth int) reflect.Type
- func RefValue(v reflect.Value, ptrDepth int) reflect.Value
- func SafeAssert[T any](v any) T
- func SafeAssertMap[K comparable, V any](a map[K]any) result.Result[map[K]V]
- func SafeAssertSlice[T any](a []any) result.Result[[]T]
- func ToAnyMap[K comparable, V any](a map[K]V) map[K]any
- func ToAnySlice[T any](a []T) []any
- func UnsafeAssertMap[K comparable, V any](a map[K]any) map[K]V
- func UnsafeAssertSlice[T any](a []any) []T
- func UnsafeConvert[T any, U any](t T) U
- func Zero[T any]() T
- type ReadonlyBytes
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BytesToString ¶
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 ¶
DerefImplType returns the underlying type of the value that implements the interface v.
func DerefInterfaceValue ¶
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 ¶
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 ¶
DerefSliceValue convert []*T to []T.
func DerefValue ¶
DerefValue dereference and unpack interface, get the underlying non-pointer and non-interface 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 RefSliceValue ¶
RefSliceValue convert []T to []*T, the ptrDepth is the count of '*'.
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 ¶
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 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 ¶
UnsafeAssertSlice convert []any to []T.
func UnsafeConvert ¶
UnsafeConvert convert a value to another 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.