Skip to content
/ k1 Public

A Simple Toolkit for Casting, Reflection, and Everyday Go Utilities.

License

Notifications You must be signed in to change notification settings

amberpixels/k1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

k1t (k1)

k1t — A Simple Toolkit for Casting, Reflection, and Everyday Go Utilities.

Go Reference Go Report Card

Overview

k1 is a Go utility library that provides helper functions for type casting, reflection, and other common programming tasks. It's designed to make your code more concise and readable, especially in testing scenarios.

Installation

go get github.com/amberpixels/k1

Features

Type Casting

The cast package provides functions for converting between different types:

  • AsString: Convert a value to a string
  • AsBytes: Convert a value to a byte slice
  • AsBool: Convert a value to a boolean
  • AsInt: Convert a value to an integer
  • AsFloat: Convert a value to a float
  • AsKind: Convert a value to a reflect.Kind
  • AsSliceOfAny: Convert a value to a slice of any
  • AsStrings: Convert a value to a slice of strings
  • AsTime: Convert a value to a time.Time

Type Checking

The cast package also provides functions for checking if a value is of a certain type:

  • IsString: Check if a value is a string (with configurable options)
  • IsStringish: Check if a value is string-like
  • IsNil: Check if a value is nil
  • IsInt: Check if a value is an integer
  • IsStrings: Check if a value is a slice of strings
  • IsTime: Check if a value is a time.Time

Reflection Helpers

The reflectish package provides helper functions for working with reflection:

  • IndirectDeep: Recursively dereference pointers
  • LengthOf: Get the length of various types (maps, arrays, strings, channels, slices)

Usage Examples

Type Casting

import "github.com/amberpixels/k1/cast"

// Convert a byte slice to a string
str = cast.AsString([]byte("byte_data")) // "byte_data"

// Convert a custom string type
type CustomString string
str = cast.AsString(CustomString("example")) // "example"

// Convert an integer
num := cast.AsInt(42) // "42"

// Convert a float
f := cast.AsFloat(3.14) // "3.14"

Type Checking

import "github.com/amberpixels/k1/cast"

// Check if a value is a string
if cast.IsString("example") {
    // It's a string
}

// Check if a value is string-like (string, []byte, etc.)
if cast.IsStringish([]byte("example")) {
    // It's string-like
}

// Check if a value is nil
if cast.IsNil(someValue) {
    // It's nil
}

Reflection Helpers

import (
    "reflect"
    "github.com/amberpixels/k1/reflect"
)

// Recursively dereference pointers
v := reflect.ValueOf(&someValue)
v = reflectish.IndirectDeep(v)

// Get the length of a value
length, ok := reflectish.LengthOf(someValue)
if ok {
    // Length is available
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A Simple Toolkit for Casting, Reflection, and Everyday Go Utilities.

Resources

License

Stars

Watchers

Forks

Packages

No packages published