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.
- Cached descriptors per
reflect.Type. - Value-aware
IsNilandIsZero, including typed nil pointers, slices, maps, channels and funcs. - Static type inspection through
TypeOf[T]()andOfType(reflect.Type). - Container helpers:
Elem,Key,Leaf,Base,Depth,Len,Cap. - Category predicates:
IsScalar,IsContainer,IsComposite,IsReference,IsComparable,IsOrdered,IsNumeric. - Struct field/tag helpers for parser and config packages.
- Assignability and interface helpers, including generic top-level functions
such as
kind.Implements[encoding.TextMarshaler](k). - Capability helpers answer what a type can do: text/binary/JSON/env marshaling, validation, parsing, flag values, SQL scanner/valuer, IO and slog value support.
- Numeric and scalar predicates, including named types and
uintptr. - For maps, scalar leaf predicates describe the value type; inspect keys with
KeyorMapKeyKind. - Safe
As*helpers for scalar values; named scalar types convert without panicking. - Zero third-party dependencies.
go get github.com/goloop/kindRequires Go 1.24 or newer.
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
fmt.Println(k.IsString()) // false; map key does not leak into leaf flags
fmt.Println(k.Depth()) // 2
fmt.Println(k.Leaf().Name()) // int
}Static interface types should be inspected with TypeOf:
type Reader interface {
Read([]byte) (int, error)
}
k := kind.TypeOf[Reader]()
fmt.Println(k.IsInterface()) // trueCapability checks are based on interfaces and method sets, not package names:
type Request struct{}
func (*Request) Validate() error { return nil }
k := kind.TypeOf[Request]()
fmt.Println(k.IsValidator()) // trueStruct tags are cached with the type descriptor:
type Config struct {
Port int `env:"PORT" json:"port"`
}
k := kind.TypeOf[Config]()
field, _ := k.Field("Port")
fmt.Println(k.HasTag("env")) // true
fmt.Println(field.Type.IsInt()) // true
fmt.Println(field.Tag.Get("json")) // portOf(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.
Full reference: DOC.md (Ukrainian: DOC.UK.md).
Before submitting changes, run:
go test ./...
go vet ./...
gofmt -l .kind is released under the MIT License. See LICENSE.
Make shape inspection safe on nil receivers and publish the stable API. The v1 release keeps the existing import path and public API. Future incompatible changes require a new major version.