Documentation
¶
Overview ¶
Package slicex provides utility functions for working with Go slices.
Key features:
- Slice conversion (ToAny, ToTyped, ToStrings)
- Element extraction (Extract, ExtractArray, ExtractSlice)
- Slice merging (Merge, Append, Prepend)
- Common utilities (Contains, Unique, Filter, Map, Join)
Usage:
anySlice := slicex.ToAny(typedSlice) unique := slicex.Unique(slice) filtered := slicex.Filter(slice, predicate)
Index ¶
- Variables
- func Append(s any, elements ...any) (any, error)
- func Contains(s any, value any) bool
- func Extract(input any) ([]any, bool)
- func ExtractArray(input any) ([]any, bool)
- func ExtractSlice(input any) ([]any, bool)
- func Filter(s any, fn func(any) bool) (any, error)
- func FromReflect(rv reflect.Value) ([]any, error)
- func IndexOf(s any, value any) int
- func IsEmpty(input any) bool
- func Join(s any, sep string) string
- func Length(input any) (int, error)
- func Map(s any, fn func(any) any) (any, error)
- func Merge(a, b any) (any, error)
- func Prepend(s any, elements ...any) (any, error)
- func Reverse(s any) (any, error)
- func StringToChars(s string) []any
- func ToAny(input any) ([]any, error)
- func ToStrings(input any) ([]string, error)
- func ToTyped[T any](input any) ([]T, error)
- func Unique(s any) (any, error)
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidReflectValue = errors.New("invalid reflect value") ErrNotCollection = errors.New("input is not a slice, array, or string") )
Sentinel errors for input validation.
var ( ErrCannotConvert = errors.New("cannot convert slice") ErrCannotConvertElement = errors.New("cannot convert element") ErrCannotConvertFirst = errors.New("cannot convert first slice") ErrCannotConvertSecond = errors.New("cannot convert second slice") )
Sentinel errors for type conversion.
Functions ¶
func Append ¶
Append appends elements to a slice, preserving the original slice's concrete type when possible.
func ExtractArray ¶
ExtractArray extracts an array (not slice) from input. Returns false for nil, slices, or non-array types.
func ExtractSlice ¶
ExtractSlice extracts a slice (not array) from input. Returns false for nil, arrays, or non-slice types.
func FromReflect ¶
FromReflect converts a reflect.Value (slice, array, or string) to []any. Returns ErrInvalidReflectValue for invalid values and ErrNotCollection for unsupported kinds.
func IndexOf ¶
IndexOf returns the index of the first occurrence of value in s, or -1 if not found. Comparison uses reflect.DeepEqual.
func Join ¶
Join formats each element with fmt.Sprintf and joins them with sep. Returns "" for empty or invalid input.
func Length ¶
Length returns the length of a slice, array, or string. Returns ErrNotCollection for other types.
func Merge ¶
Merge concatenates two slices. If both inputs share the same concrete slice type, the result preserves that type. Returns ErrCannotConvertFirst or ErrCannotConvertSecond if the respective input cannot be converted.
func Prepend ¶
Prepend inserts elements before a slice, preserving the original slice's concrete type when possible.
func StringToChars ¶
StringToChars converts a string to []any where each element is a single-character string.
func ToAny ¶
ToAny converts a slice, array, or string to []any. A nil input returns (nil, nil). A bare string returns a single-element []any. For unrecognized concrete types, reflection is used as a fallback.
func ToStrings ¶
ToStrings converts any slice to []string by formatting each element with fmt.Sprintf.
Types ¶
This section is empty.