Documentation
¶
Overview ¶
Package kind provides a small, cached reflection layer for classifying Go values and types.
The main entry point is Of, which returns a Kind describing the dynamic type and value-specific state of a value:
k := kind.Of([]int{1, 2, 3})
k.IsSlice() // true
k.IsInt() // true, because the slice element is int
k.Name() // "[]int"
Type descriptors are cached, so repeated calls for the same reflect.Type do not repeat the recursive classification work. Value-specific checks such as IsNil and IsZero are still evaluated per value.
Index ¶
- type Kind
- func (k *Kind) AsBool() (bool, bool)
- func (k *Kind) AsComplex64() (complex64, bool)
- func (k *Kind) AsComplex128() (complex128, bool)
- func (k *Kind) AsFloat32() (float32, bool)
- func (k *Kind) AsFloat64() (float64, bool)
- func (k *Kind) AsInt() (int, bool)
- func (k *Kind) AsInt8() (int8, bool)
- func (k *Kind) AsInt16() (int16, bool)
- func (k *Kind) AsInt32() (int32, bool)
- func (k *Kind) AsInt64() (int64, bool)
- func (k *Kind) AsString() (string, bool)
- func (k *Kind) AsUint() (uint, bool)
- func (k *Kind) AsUint8() (uint8, bool)
- func (k *Kind) AsUint16() (uint16, bool)
- func (k *Kind) AsUint32() (uint32, bool)
- func (k *Kind) AsUint64() (uint64, bool)
- func (k *Kind) AsUintptr() (uintptr, bool)
- func (k *Kind) AsUnsafePointer() (unsafe.Pointer, bool)
- func (k *Kind) Elem() *Kind
- func (k *Kind) Is(name string) bool
- func (k *Kind) IsAnyComplex() bool
- func (k *Kind) IsAnyFloat() bool
- func (k *Kind) IsAnyInt() bool
- func (k *Kind) IsArray() bool
- func (k *Kind) IsArrayOfArrays() bool
- func (k *Kind) IsArrayOfSlices() bool
- func (k *Kind) IsBool() bool
- func (k *Kind) IsChannel() bool
- func (k *Kind) IsComplex() bool
- func (k *Kind) IsComplex64() bool
- func (k *Kind) IsComplex128() bool
- func (k *Kind) IsFloat32() bool
- func (k *Kind) IsFloat64() bool
- func (k *Kind) IsFunction() bool
- func (k *Kind) IsInt() bool
- func (k *Kind) IsInt8() bool
- func (k *Kind) IsInt16() bool
- func (k *Kind) IsInt32() bool
- func (k *Kind) IsInt64() bool
- func (k *Kind) IsInterface() bool
- func (k *Kind) IsMap() bool
- func (k *Kind) IsNamed() bool
- func (k *Kind) IsNil() bool
- func (k *Kind) IsNilable() bool
- func (k *Kind) IsNumber() bool
- func (k *Kind) IsPointer() bool
- func (k *Kind) IsSigned() bool
- func (k *Kind) IsSlice() bool
- func (k *Kind) IsSliceOfArrays() bool
- func (k *Kind) IsSliceOfSlices() bool
- func (k *Kind) IsString() bool
- func (k *Kind) IsStruct() bool
- func (k *Kind) IsUint() bool
- func (k *Kind) IsUint8() bool
- func (k *Kind) IsUint16() bool
- func (k *Kind) IsUint32() bool
- func (k *Kind) IsUint64() bool
- func (k *Kind) IsUintptr() bool
- func (k *Kind) IsUndefined() bool
- func (k *Kind) IsUnsafePointer() bool
- func (k *Kind) IsUnsigned() bool
- func (k *Kind) IsZero() bool
- func (k *Kind) Key() *Kind
- func (k *Kind) Kind() reflect.Kind
- func (k *Kind) MapKeyKind() *Kind
- func (k *Kind) MapValueKind() *Kind
- func (k *Kind) Name() string
- func (k *Kind) String() string
- func (k *Kind) Type() reflect.Type
- func (k *Kind) Value() any
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Kind ¶
type Kind struct {
// contains filtered or unexported fields
}
Kind describes a Go value's dynamic type and value-specific state. It is immutable after construction and safe to read concurrently.
func Of ¶
Of returns a Kind that describes the dynamic type of v.
Example ¶
package main
import (
"fmt"
"github.com/goloop/kind"
)
func main() {
k := kind.Of([]int{1, 2, 3})
fmt.Println(k.IsSlice())
fmt.Println(k.IsInt())
fmt.Println(k.Name())
}
Output: true true []int
func TypeOf ¶
TypeOf is a generic helper that returns a Kind for T's static type.
Example ¶
package main
import (
"fmt"
"github.com/goloop/kind"
)
func main() {
type Reader interface {
Read([]byte) (int, error)
}
k := kind.TypeOf[Reader]()
fmt.Println(k.IsInterface())
fmt.Println(k.Kind())
}
Output: true interface
func (*Kind) AsComplex64 ¶
AsComplex64 returns the value as complex64 when the dynamic type's kind is complex64.
func (*Kind) AsComplex128 ¶
func (k *Kind) AsComplex128() (complex128, bool)
AsComplex128 returns the value as complex128 when the dynamic type's kind is complex128.
func (*Kind) AsFloat32 ¶
AsFloat32 returns the value as float32 when the dynamic type's kind is float32.
func (*Kind) AsFloat64 ¶
AsFloat64 returns the value as float64 when the dynamic type's kind is float64.
func (*Kind) AsString ¶
AsString returns the value as string when the dynamic type's kind is string.
func (*Kind) AsUint16 ¶
AsUint16 returns the value as uint16 when the dynamic type's kind is uint16.
func (*Kind) AsUint32 ¶
AsUint32 returns the value as uint32 when the dynamic type's kind is uint32.
func (*Kind) AsUint64 ¶
AsUint64 returns the value as uint64 when the dynamic type's kind is uint64.
func (*Kind) AsUintptr ¶
AsUintptr returns the value as uintptr when the dynamic type's kind is uintptr.
func (*Kind) AsUnsafePointer ¶
AsUnsafePointer returns the value as unsafe.Pointer when the dynamic type's kind is unsafe.Pointer.
func (*Kind) Is ¶
Is reports whether name matches the canonical Go type name. Spaces are ignored, and built-in names are also compared case-insensitively for compatibility with older releases.
func (*Kind) IsAnyComplex ¶
IsAnyComplex reports whether the type or leaf is complex64 or complex128.
func (*Kind) IsAnyFloat ¶
IsAnyFloat reports whether the type or leaf is float32 or float64.
func (*Kind) IsAnyInt ¶
IsAnyInt reports whether the type or leaf is any integer type, including uintptr.
func (*Kind) IsArray ¶
IsArray reports whether the type is an array that is not classified as an array of arrays or array of slices.
func (*Kind) IsArrayOfArrays ¶
IsArrayOfArrays reports whether the type is an array whose element chain contains another array before the leaf type.
func (*Kind) IsArrayOfSlices ¶
IsArrayOfSlices reports whether the type is an array whose element chain contains a slice before the leaf type.
func (*Kind) IsComplex ¶
IsComplex reports whether the type is composite, reference-like, function, interface or unsafe pointer. Numeric complex64/complex128 are covered by IsAnyComplex.
func (*Kind) IsComplex64 ¶
IsComplex64 reports whether the type or its element leaf is complex64.
func (*Kind) IsComplex128 ¶
IsComplex128 reports whether the type or its element leaf is complex128.
func (*Kind) IsFunction ¶
IsFunction reports whether the type is a function.
func (*Kind) IsInterface ¶
IsInterface reports whether the type is an interface. Use OfType or TypeOf when you need the static interface type; Of sees the dynamic concrete type.
func (*Kind) IsNil ¶
IsNil reports whether the value is nil. It is value-specific and is true for nil interfaces and typed nil pointers, slices, maps, channels and funcs.
func (*Kind) IsSlice ¶
IsSlice reports whether the type is a slice that is not classified as a slice of arrays or slice of slices.
func (*Kind) IsSliceOfArrays ¶
IsSliceOfArrays reports whether the type is a slice whose element chain contains an array before the leaf type.
func (*Kind) IsSliceOfSlices ¶
IsSliceOfSlices reports whether the type is a slice whose element chain contains another slice before the leaf type.
func (*Kind) IsUndefined ¶
IsUndefined reports whether the type could not be classified.
func (*Kind) IsUnsafePointer ¶
IsUnsafePointer reports whether the type or its element leaf is unsafe.Pointer.
func (*Kind) IsUnsigned ¶
IsUnsigned reports whether the type or leaf is an unsigned integer, including uintptr.
func (*Kind) MapKeyKind ¶
MapKeyKind returns the key Kind for maps.
func (*Kind) MapValueKind ¶
MapValueKind returns the value Kind for maps.