conv

package
v1.20.3 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2025 License: MIT Imports: 6 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.

Examples

```go
var iface interface{} = 42
val := reflect.ValueOf(iface)
typ := conv.DerefImplType(val) // returns int type
```

func DerefInterfaceValue

func DerefInterfaceValue(v reflect.Value) reflect.Value

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

Examples

```go
var iface interface{} = 42
val := reflect.ValueOf(iface)
result := conv.DerefInterfaceValue(val) // returns value of 42
```

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.

Examples

```go
x := 42
val := reflect.ValueOf(&x)
result := conv.DerefPtrValue(val) // returns value of 42
```

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.

Examples

```go
x, y := 1, 2
ptrSlice := []*int{&x, &y}
val := reflect.ValueOf(ptrSlice)
result := conv.DerefSliceValue(val) // returns []int{1, 2}
```

func DerefType

func DerefType(t reflect.Type) reflect.Type

DerefType dereference, get the underlying non-pointer type.

Examples

```go
typ := reflect.TypeOf((*int)(nil))
baseType := conv.DerefType(typ) // returns int type
```

func DerefValue

func DerefValue(v reflect.Value) reflect.Value

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

Examples

```go
x := 42
val := reflect.ValueOf(&x)
result := conv.DerefValue(val) // returns value of 42
```

func IsCompositionMethod added in v1.20.3

func IsCompositionMethod(method reflect.Method) bool

IsCompositionMethod determines whether the method is inherited from an anonymous field (composition). It checks if the method is auto-generated by Go's composition mechanism.

NOTE: This function relies on the "<autogenerated>" file marker used by Go's compiler for composition methods. This may not be reliable in all cases.

Examples

```go
type Base struct{}
func (Base) Method() {}

type Derived struct {
	Base // anonymous field
}

method, _ := reflect.TypeOf(Derived{}).MethodByName("Method")
if conv.IsCompositionMethod(method) {
	// Method is inherited from Base
}
```

func IsExportedName added in v1.20.3

func IsExportedName(name string) bool

IsExportedName checks if the name is exported (starts with uppercase letter).

Examples

```go
if conv.IsExportedName("MyFunction") {
	// Name is exported
}
```

func IsExportedOrBuiltinType added in v1.20.3

func IsExportedOrBuiltinType(t reflect.Type) bool

IsExportedOrBuiltinType checks if the type is exported or a builtin type. It dereferences pointer types before checking.

Examples

```go
typ := reflect.TypeOf(MyStruct{})
if conv.IsExportedOrBuiltinType(typ) {
	// Type is exported or builtin
}
```

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 '*'.

Examples

```go
slice := []int{1, 2}
val := reflect.ValueOf(slice)
result := conv.RefSliceValue(val, 1) // returns []*int{&1, &2}
```

func RefType

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

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

Examples

```go
typ := reflect.TypeOf(42)
ptrType := conv.RefType(typ, 1) // returns *int type
```

func RefValue

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

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

Examples

```go
val := reflect.ValueOf(42)
ptrVal := conv.RefValue(val, 1) // returns *int pointing to 42
```

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 TypeName added in v1.20.3

func TypeName(obj any) string

TypeName gets the type name of the object. For functions, it returns the function name from runtime. For other types, it returns the type string representation.

Examples

```go
name := conv.TypeName(42)           // "int"
name := conv.TypeName(&MyStruct{})  // "*conv.MyStruct"
```

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