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

kind

package module
v0.0.1-beta Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2023 License: MIT Imports: 2 Imported by: 0

README

Go Report Card License License Stay with Ukraine

kind

The "kind" package in GoLang is designed to facilitate the inspection and categorization of different data types at runtime, using reflection. It provides a unified way of querying the characteristics of a given value, determining whether it's a simple or complex type, and even obtaining its string representation. The package revolves around a central Kind struct, which encapsulates the nature of a particular data type and provides a myriad of methods to query its specifics.

Key features and components include:

  1. The Kind struct: This structure serves as a container for an exhaustive list of possible data types a given value can have in Go. This includes scalars (like bools, ints, uints, floats, and strings), complex types (like arrays, slices, maps, channels, and structs), and some language-specific types (like pointers, interfaces, and functions). It also has a field to hold the value itself and a name that stores a string representation of the type.

  2. Query methods: The Kind struct offers numerous methods that let you determine if the encapsulated value is of a particular type. Each method corresponds to a particular type (e.g., IsInt(), IsSlice(), IsMap()) and returns true if the value is of that type.

  3. Conversion methods: The struct also provides methods to attempt conversion of the encapsulated value to a specific type. If successful, these methods return the value in the requested type along with a success indicator (e.g., AsBool(), AsFloat32()).

  4. Helper functions: There are additional methods to assist with common tasks, such as determining whether a value is of a complex type (IsComplex()), or whether it is a signed or unsigned type (IsSigned(), IsUnsigned()). There's also an Is function that compares the name of the kind with a provided string, offering another way to determine the type.

  5. Map Key-Value Kind information: If the type in question is a Map, then MapKeyKind() and MapValueKind() provide Kind instances representing the type of key and value respectively.

  6. Of function: This package provides a standalone Of function that takes in a value and returns an instance of Kind that describes the type of the given value. It performs an initial check to see if the value is nil, and then proceeds to dissect the type of the value using reflection, populating the relevant fields in the Kind struct accordingly.

In summary, the "kind" package in GoLang is a robust utility for dealing with dynamic types, making it much easier to inspect, categorize, and convert different types of data at runtime.

Documentation

Overview

Package kind provides utilities to analyze and represent the type details of Go values.

The Kind struct represents the type details of an instance, including simple types like int, uint, string, bool, etc., as well as complex types like slices, maps, structs, arrays, interfaces, and pointers.

The package exposes the Of function that takes a value as input and returns a Kind instance representing the type of that value. The Kind struct contains various boolean flags and methods to query the type details.

It also provides a function, deepEqualKind, to compare two Kind instances for equality, allowing users to check if two values have the same type and value representation.

Example usage:

kind := kind.Of(42)
fmt.Println(kind.IsInt(), kind.Name()) // true "int"

kind := kind.Of([]int{1, 2, 3})
fmt.Println(kind.IsSlice(), kind.IsInt(), kind.Name()) // true true "[]int"

The Kind struct has methods like IsComplex, IsSigned, IsUnsigned, IsNil, IsPointer, etc., to query various attributes of the type. It also provides As<Type>() methods to extract the underlying value as a specific Go type (e.g., AsBool, AsString, AsInt, AsFloat32, etc.).

Additionally, Kind provides MapKeyKind and MapValueKind methods to get the Kind instances of the key and value types of a map, respectively.

The package is useful for situations where type introspection or reflection is needed to understand the nature of Go values, especially in cases where nested or complex types are involved.

Note: The package assumes that the value passed to the Of function is a valid Go value, and it does not handle all possible types. For instance, certain edge cases like unsafe pointers, function pointers, and unexported fields in structs might not be fully supported. Users should exercise caution and test thoroughly in their specific use cases.

This package is written in pure Go and does not have any external dependencies.

Index

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 is a struct that represents detailed information about the type of an instance.

func Of

func Of(v interface{}) *Kind

Of returns a Kind instance that represents the type of the given value.

Example usage:

kind := kind.Of(42)
fmt.Println(kind.IsInt(), kind.Name()) // true "int"

kind := kind.Of([]int{1, 2, 3})
fmt.Println(kind.IsSlice(), kind.IsInt(), kind.Name()) // true true "[]int"

func (*Kind) AsBool

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

AsBool returns the value of the Kind as bool.

func (*Kind) AsComplex64

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

AsComplex64 returns the value of the Kind as complex64.

func (*Kind) AsComplex128

func (k *Kind) AsComplex128() (complex128, bool)

AsComplex128 returns the value of the Kind as complex128.

func (*Kind) AsFloat32

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

AsFloat32 returns the value of the Kind as float32.

func (*Kind) AsFloat64

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

AsFloat64 returns the value of the Kind as float64.

func (*Kind) AsInt

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

AsInt returns the value of the Kind as int.

func (*Kind) AsInt8

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

AsInt8 returns the value of the Kind as int8.

func (*Kind) AsInt16

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

AsInt16 returns the value of the Kind as int16.

func (*Kind) AsInt32

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

AsInt32 returns the value of the Kind as int32.

func (*Kind) AsInt64

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

AsInt64 returns the value of the Kind as int64.

func (*Kind) AsString

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

AsString returns the value of the Kind as string.

func (*Kind) AsUint

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

AsUint returns the value of the Kind as uint.

func (*Kind) AsUint8

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

AsUint8 returns the value of the Kind as uint8.

func (*Kind) AsUint16

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

AsUint16 returns the value of the Kind as uint16.

func (*Kind) AsUint32

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

AsUint32 returns the value of the Kind as uint32.

func (*Kind) AsUint64

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

AsUint64 returns the value of the Kind as uint64.

func (*Kind) Is

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

Is returns true if the name of the Kind instance is equal to the given name.

Example usage:

kind := kind.Of(42)
fmt.Println(kind.Is("int")) // true

kind := kind.Of([]int{1, 2, 3})
fmt.Println(kind.Is("[]int")) // true

func (*Kind) IsAnyComplex

func (k *Kind) IsAnyComplex() bool

IsAnyComplex returns true if the Kind instance represents a complex type.

func (*Kind) IsAnyFloat

func (k *Kind) IsAnyFloat() bool

IsAnyFloat returns true if the Kind instance represents a float type.

func (*Kind) IsAnyInt

func (k *Kind) IsAnyInt() bool

IsAnyInt returns true if the Kind instance represents an integer type.

func (*Kind) IsArray

func (k *Kind) IsArray() bool

IsArray returns true if the Kind instance represents an array type.

func (*Kind) IsArrayOfArrays

func (k *Kind) IsArrayOfArrays() bool

IsArrayOfArrays returns true if the Kind instance represents an array of arrays type.

func (*Kind) IsArrayOfSlices

func (k *Kind) IsArrayOfSlices() bool

IsArrayOfSlices returns true if the Kind instance represents an array of slices type.

func (*Kind) IsBool

func (k *Kind) IsBool() bool

IsBool returns true if the Kind instance represents a bool type.

func (*Kind) IsChannel

func (k *Kind) IsChannel() bool

IsChannel returns true if the Kind instance represents a channel type.

func (*Kind) IsComplex

func (k *Kind) IsComplex() bool

IsComplex returns true if the Kind instance represents a complex type. Types like int, uint, string, etc are simple types. That is, they can be determined by one indicator, for example, IsInt(), IsUint(), IsString, etc.. Types like struct, map, slice are complex types, for example: map[string]int == IsMap() and IsString() and IsInt() - have several attr.

func (*Kind) IsComplex64

func (k *Kind) IsComplex64() bool

IsComplex64 returns true if the Kind instance represents a complex64 type.

func (*Kind) IsComplex128

func (k *Kind) IsComplex128() bool

IsComplex128 returns true if the Kind instance represents a complex128 type.

func (*Kind) IsFloat32

func (k *Kind) IsFloat32() bool

IsFloat32 returns true if the Kind instance represents a float32 type.

func (*Kind) IsFloat64

func (k *Kind) IsFloat64() bool

IsFloat64 returns true if the Kind instance represents a float64 type.

func (*Kind) IsFunction

func (k *Kind) IsFunction() bool

IsFunction returns true if the Kind instance represents a function type.

func (*Kind) IsInt

func (k *Kind) IsInt() bool

IsInt returns true if the Kind instance represents an int type.

func (*Kind) IsInt8

func (k *Kind) IsInt8() bool

IsInt8 returns true if the Kind instance represents an int8 type.

func (*Kind) IsInt16

func (k *Kind) IsInt16() bool

IsInt16 returns true if the Kind instance represents an int16 type.

func (*Kind) IsInt32

func (k *Kind) IsInt32() bool

IsInt32 returns true if the Kind instance represents an int32 type.

func (*Kind) IsInt64

func (k *Kind) IsInt64() bool

IsInt64 returns true if the Kind instance represents an int64 type.

func (*Kind) IsInterface

func (k *Kind) IsInterface() bool

IsInterface returns true if the Kind instance represents an interface type.

func (*Kind) IsMap

func (k *Kind) IsMap() bool

IsMap returns true if the Kind instance represents a map type.

func (*Kind) IsNil

func (k *Kind) IsNil() bool

IsNil returns true if the Kind instance represents a nil type.

func (*Kind) IsNumber

func (k *Kind) IsNumber() bool

IsNumber returns true if the Kind instance represents a number type.

func (*Kind) IsPointer

func (k *Kind) IsPointer() bool

IsPointer returns true if the Kind instance represents a pointer type.

func (*Kind) IsSigned

func (k *Kind) IsSigned() bool

IsSigned returns true if the Kind instance represents a signed type.

func (*Kind) IsSlice

func (k *Kind) IsSlice() bool

IsSlice returns true if the Kind instance represents a slice type.

func (*Kind) IsSliceOfArrays

func (k *Kind) IsSliceOfArrays() bool

IsSliceOfArrays returns true if the Kind instance represents a slice of arrays type.

func (*Kind) IsSliceOfSlices

func (k *Kind) IsSliceOfSlices() bool

IsSliceOfSlices returns true if the Kind instance represents a slice of slices type.

func (*Kind) IsString

func (k *Kind) IsString() bool

IsString returns true if the Kind instance represents a string type.

func (*Kind) IsStruct

func (k *Kind) IsStruct() bool

IsStruct returns true if the Kind instance represents a struct type.

func (*Kind) IsUint

func (k *Kind) IsUint() bool

IsUint returns true if the Kind instance represents a uint type.

func (*Kind) IsUint8

func (k *Kind) IsUint8() bool

IsUint8 returns true if the Kind instance represents a uint8 type.

func (*Kind) IsUint16

func (k *Kind) IsUint16() bool

IsUint16 returns true if the Kind instance represents a uint16 type.

func (*Kind) IsUint32

func (k *Kind) IsUint32() bool

IsUint32 returns true if the Kind instance represents a uint32 type.

func (*Kind) IsUint64

func (k *Kind) IsUint64() bool

IsUint64 returns true if the Kind instance represents a uint64 type.

func (*Kind) IsUintptr

func (k *Kind) IsUintptr() bool

IsUintptr returns true if the Kind instance represents a uintptr type.

func (*Kind) IsUndefined

func (k *Kind) IsUndefined() bool

IsUndefined returns true if the Kind instance represents an undefined type.

func (*Kind) IsUnsigned

func (k *Kind) IsUnsigned() bool

IsUnsigned returns true if the Kind instance represents an unsigned type.

func (*Kind) MapKeyKind

func (k *Kind) MapKeyKind() *Kind

MapKeyKind returns the Kind instance of the map key.

func (*Kind) MapValueKind

func (k *Kind) MapValueKind() *Kind

MapValueKind returns the Kind instance of the map key.

func (*Kind) Name

func (k *Kind) Name() string

Name returns the name of the Kind instance.

func (*Kind) String

func (k *Kind) String() string

String returns the name of the Kind instance.

Jump to

Keyboard shortcuts

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