Help us shape the future of Go! Take the 2026 Go Developer Survey

kind

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: MIT Imports: 4 Imported by: 0

README

deps.dev Go Reference License Stay with Ukraine

kind

kind is a small cached reflection helper for Go. It classifies values and types with readable predicates such as IsSlice, IsMap, IsInt, IsNilable, IsNamed, IsZero, and exposes map key / element information without repeating reflection code at every call site.

The package is intentionally narrow: it does not replace reflect, but wraps the common type-inspection questions that appear in parsers, validators, configuration loaders and diagnostics.

Features

  • Cached descriptors per reflect.Type.
  • Value-aware IsNil and IsZero, including typed nil pointers, slices, maps, channels and funcs.
  • Static type inspection through TypeOf[T]() and OfType(reflect.Type).
  • Container helpers: Elem, Key, MapKeyKind, MapValueKind.
  • Numeric and scalar predicates, including named types and uintptr.
  • Safe As* helpers for scalar values; named scalar types convert without panicking.
  • Zero third-party dependencies.

Installation

go get github.com/goloop/kind

Requires Go 1.24 or newer.

Quick Start

package main

import (
    "fmt"

    "github.com/goloop/kind"
)

func main() {
    k := kind.Of(map[string][]int{"one": {1, 2, 3}})

    fmt.Println(k.IsMap())                   // true
    fmt.Println(k.MapKeyKind().IsString())   // true
    fmt.Println(k.MapValueKind().IsSlice())  // true
    fmt.Println(k.MapValueKind().IsInt())    // true
}

Static interface types should be inspected with TypeOf:

type Reader interface {
    Read([]byte) (int, error)
}

k := kind.TypeOf[Reader]()
fmt.Println(k.IsInterface()) // true

Notes

Of(value) sees the dynamic type stored in an interface, exactly like reflect.TypeOf. If you need a static interface type, use TypeOf[T]() or OfType.

Contributing

Before submitting changes, run:

go test ./...
go vet ./...
gofmt -l .

License

kind is released under the MIT License. See LICENSE.

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

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

func Of(v any) *Kind

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 OfType

func OfType(t reflect.Type) *Kind

OfType returns a Kind that describes t without binding it to a value.

func TypeOf

func TypeOf[T any]() *Kind

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) AsBool

func (k *Kind) AsBool() (bool, bool)

AsBool returns the value as bool when the dynamic type's kind is bool.

func (*Kind) AsComplex64

func (k *Kind) AsComplex64() (complex64, bool)

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

func (k *Kind) AsFloat32() (float32, bool)

AsFloat32 returns the value as float32 when the dynamic type's kind is float32.

func (*Kind) AsFloat64

func (k *Kind) AsFloat64() (float64, bool)

AsFloat64 returns the value as float64 when the dynamic type's kind is float64.

func (*Kind) AsInt

func (k *Kind) AsInt() (int, bool)

AsInt returns the value as int when the dynamic type's kind is int.

func (*Kind) AsInt8

func (k *Kind) AsInt8() (int8, bool)

AsInt8 returns the value as int8 when the dynamic type's kind is int8.

func (*Kind) AsInt16

func (k *Kind) AsInt16() (int16, bool)

AsInt16 returns the value as int16 when the dynamic type's kind is int16.

func (*Kind) AsInt32

func (k *Kind) AsInt32() (int32, bool)

AsInt32 returns the value as int32 when the dynamic type's kind is int32.

func (*Kind) AsInt64

func (k *Kind) AsInt64() (int64, bool)

AsInt64 returns the value as int64 when the dynamic type's kind is int64.

func (*Kind) AsString

func (k *Kind) AsString() (string, bool)

AsString returns the value as string when the dynamic type's kind is string.

func (*Kind) AsUint

func (k *Kind) AsUint() (uint, bool)

AsUint returns the value as uint when the dynamic type's kind is uint.

func (*Kind) AsUint8

func (k *Kind) AsUint8() (uint8, bool)

AsUint8 returns the value as uint8 when the dynamic type's kind is uint8.

func (*Kind) AsUint16

func (k *Kind) AsUint16() (uint16, bool)

AsUint16 returns the value as uint16 when the dynamic type's kind is uint16.

func (*Kind) AsUint32

func (k *Kind) AsUint32() (uint32, bool)

AsUint32 returns the value as uint32 when the dynamic type's kind is uint32.

func (*Kind) AsUint64

func (k *Kind) AsUint64() (uint64, bool)

AsUint64 returns the value as uint64 when the dynamic type's kind is uint64.

func (*Kind) AsUintptr

func (k *Kind) AsUintptr() (uintptr, bool)

AsUintptr returns the value as uintptr when the dynamic type's kind is uintptr.

func (*Kind) AsUnsafePointer

func (k *Kind) AsUnsafePointer() (unsafe.Pointer, bool)

AsUnsafePointer returns the value as unsafe.Pointer when the dynamic type's kind is unsafe.Pointer.

func (*Kind) Elem

func (k *Kind) Elem() *Kind

Elem returns the element Kind for pointers, arrays, slices, maps and channels.

func (*Kind) Is

func (k *Kind) Is(name string) bool

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

func (k *Kind) IsAnyComplex() bool

IsAnyComplex reports whether the type or leaf is complex64 or complex128.

func (*Kind) IsAnyFloat

func (k *Kind) IsAnyFloat() bool

IsAnyFloat reports whether the type or leaf is float32 or float64.

func (*Kind) IsAnyInt

func (k *Kind) IsAnyInt() bool

IsAnyInt reports whether the type or leaf is any integer type, including uintptr.

func (*Kind) IsArray

func (k *Kind) IsArray() bool

IsArray reports whether the type is an array that is not classified as an array of arrays or array of slices.

func (*Kind) IsArrayOfArrays

func (k *Kind) IsArrayOfArrays() bool

IsArrayOfArrays reports whether the type is an array whose element chain contains another array before the leaf type.

func (*Kind) IsArrayOfSlices

func (k *Kind) IsArrayOfSlices() bool

IsArrayOfSlices reports whether the type is an array whose element chain contains a slice before the leaf type.

func (*Kind) IsBool

func (k *Kind) IsBool() bool

IsBool reports whether the type or its element leaf is bool.

func (*Kind) IsChannel

func (k *Kind) IsChannel() bool

IsChannel reports whether the type is a channel.

func (*Kind) IsComplex

func (k *Kind) IsComplex() bool

IsComplex reports whether the type is composite, reference-like, function, interface or unsafe pointer. Numeric complex64/complex128 are covered by IsAnyComplex.

func (*Kind) IsComplex64

func (k *Kind) IsComplex64() bool

IsComplex64 reports whether the type or its element leaf is complex64.

func (*Kind) IsComplex128

func (k *Kind) IsComplex128() bool

IsComplex128 reports whether the type or its element leaf is complex128.

func (*Kind) IsFloat32

func (k *Kind) IsFloat32() bool

IsFloat32 reports whether the type or its element leaf is float32.

func (*Kind) IsFloat64

func (k *Kind) IsFloat64() bool

IsFloat64 reports whether the type or its element leaf is float64.

func (*Kind) IsFunction

func (k *Kind) IsFunction() bool

IsFunction reports whether the type is a function.

func (*Kind) IsInt

func (k *Kind) IsInt() bool

IsInt reports whether the type or its element leaf is int.

func (*Kind) IsInt8

func (k *Kind) IsInt8() bool

IsInt8 reports whether the type or its element leaf is int8.

func (*Kind) IsInt16

func (k *Kind) IsInt16() bool

IsInt16 reports whether the type or its element leaf is int16.

func (*Kind) IsInt32

func (k *Kind) IsInt32() bool

IsInt32 reports whether the type or its element leaf is int32.

func (*Kind) IsInt64

func (k *Kind) IsInt64() bool

IsInt64 reports whether the type or its element leaf is int64.

func (*Kind) IsInterface

func (k *Kind) IsInterface() bool

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) IsMap

func (k *Kind) IsMap() bool

IsMap reports whether the type is a map.

func (*Kind) IsNamed

func (k *Kind) IsNamed() bool

IsNamed reports whether the type has a defined name.

func (*Kind) IsNil

func (k *Kind) IsNil() bool

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) IsNilable

func (k *Kind) IsNilable() bool

IsNilable reports whether values of this type may be nil.

func (*Kind) IsNumber

func (k *Kind) IsNumber() bool

IsNumber reports whether the type or leaf is any numeric type.

func (*Kind) IsPointer

func (k *Kind) IsPointer() bool

IsPointer reports whether the type is a pointer.

func (*Kind) IsSigned

func (k *Kind) IsSigned() bool

IsSigned reports whether the type or leaf is signed numeric.

func (*Kind) IsSlice

func (k *Kind) IsSlice() bool

IsSlice reports whether the type is a slice that is not classified as a slice of arrays or slice of slices.

func (*Kind) IsSliceOfArrays

func (k *Kind) IsSliceOfArrays() bool

IsSliceOfArrays reports whether the type is a slice whose element chain contains an array before the leaf type.

func (*Kind) IsSliceOfSlices

func (k *Kind) IsSliceOfSlices() bool

IsSliceOfSlices reports whether the type is a slice whose element chain contains another slice before the leaf type.

func (*Kind) IsString

func (k *Kind) IsString() bool

IsString reports whether the type or its element leaf is string.

func (*Kind) IsStruct

func (k *Kind) IsStruct() bool

IsStruct reports whether the type is a struct.

func (*Kind) IsUint

func (k *Kind) IsUint() bool

IsUint reports whether the type or its element leaf is uint.

func (*Kind) IsUint8

func (k *Kind) IsUint8() bool

IsUint8 reports whether the type or its element leaf is uint8.

func (*Kind) IsUint16

func (k *Kind) IsUint16() bool

IsUint16 reports whether the type or its element leaf is uint16.

func (*Kind) IsUint32

func (k *Kind) IsUint32() bool

IsUint32 reports whether the type or its element leaf is uint32.

func (*Kind) IsUint64

func (k *Kind) IsUint64() bool

IsUint64 reports whether the type or its element leaf is uint64.

func (*Kind) IsUintptr

func (k *Kind) IsUintptr() bool

IsUintptr reports whether the type or its element leaf is uintptr.

func (*Kind) IsUndefined

func (k *Kind) IsUndefined() bool

IsUndefined reports whether the type could not be classified.

func (*Kind) IsUnsafePointer

func (k *Kind) IsUnsafePointer() bool

IsUnsafePointer reports whether the type or its element leaf is unsafe.Pointer.

func (*Kind) IsUnsigned

func (k *Kind) IsUnsigned() bool

IsUnsigned reports whether the type or leaf is an unsigned integer, including uintptr.

func (*Kind) IsZero

func (k *Kind) IsZero() bool

IsZero reports whether the value is Go's zero value for its type.

func (*Kind) Key

func (k *Kind) Key() *Kind

Key returns the key Kind for maps.

func (*Kind) Kind

func (k *Kind) Kind() reflect.Kind

Kind returns the underlying reflect.Kind. It returns reflect.Invalid for nil.

func (*Kind) MapKeyKind

func (k *Kind) MapKeyKind() *Kind

MapKeyKind returns the key Kind for maps.

func (*Kind) MapValueKind

func (k *Kind) MapValueKind() *Kind

MapValueKind returns the value Kind for maps.

func (*Kind) Name

func (k *Kind) Name() string

Name returns the canonical Go type name.

func (*Kind) String

func (k *Kind) String() string

String returns the canonical Go type name.

func (*Kind) Type

func (k *Kind) Type() reflect.Type

Type returns the reflected type. It returns nil for the nil Kind.

func (*Kind) Value

func (k *Kind) Value() any

Value returns the original value passed to Of. For OfType and nil it returns nil.

Jump to

Keyboard shortcuts

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