Socket
Socket
Sign inDemoInstall

github.com/goloop/kind

Package Overview
Dependencies
0
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    github.com/goloop/kind

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: 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.


Version published

Readme

Source

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.

FAQs

Last updated on 31 Jul 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc