k1

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2025 License: MIT Imports: 2 Imported by: 0

README

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JoinStringers

func JoinStringers[T fmt.Stringer](vals []T, sep string) string

JoinStringers joins given stringers into a single string.

Types

This section is empty.

Directories

Path Synopsis
Package cast provides utility functions for type casting and type checking.
Package cast provides utility functions for type casting and type checking.
Package maybe provides a generic, type-safe container for values that may or may not be present.
Package maybe provides a generic, type-safe container for values that may or may not be present.
Package reflectish provides common reflection utilities that extend Go's standard "reflect" library.
Package reflectish provides common reflection utilities that extend Go's standard "reflect" library.

Jump to

Keyboard shortcuts

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