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 ¶
- 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) 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) IsNil() 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) IsUnsigned() bool
- func (k *Kind) MapKeyKind() *Kind
- func (k *Kind) MapValueKind() *Kind
- func (k *Kind) Name() string
- func (k *Kind) String() string
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) AsComplex64 ¶
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) Is ¶
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 ¶
IsAnyComplex returns true if the Kind instance represents a complex type.
func (*Kind) IsAnyFloat ¶
IsAnyFloat returns true if the Kind instance represents a float type.
func (*Kind) IsArrayOfArrays ¶
IsArrayOfArrays returns true if the Kind instance represents an array of arrays type.
func (*Kind) IsArrayOfSlices ¶
IsArrayOfSlices returns true if the Kind instance represents an array of slices type.
func (*Kind) IsComplex ¶
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 ¶
IsComplex64 returns true if the Kind instance represents a complex64 type.
func (*Kind) IsComplex128 ¶
IsComplex128 returns true if the Kind instance represents a complex128 type.
func (*Kind) IsFunction ¶
IsFunction returns true if the Kind instance represents a function type.
func (*Kind) IsInterface ¶
IsInterface returns true if the Kind instance represents an interface type.
func (*Kind) IsSliceOfArrays ¶
IsSliceOfArrays returns true if the Kind instance represents a slice of arrays type.
func (*Kind) IsSliceOfSlices ¶
IsSliceOfSlices returns true if the Kind instance represents a slice of slices type.
func (*Kind) IsUndefined ¶
IsUndefined returns true if the Kind instance represents an undefined type.
func (*Kind) IsUnsigned ¶
IsUnsigned returns true if the Kind instance represents an unsigned type.
func (*Kind) MapKeyKind ¶
MapKeyKind returns the Kind instance of the map key.
func (*Kind) MapValueKind ¶
MapValueKind returns the Kind instance of the map key.