Documentation
¶
Overview ¶
Package coerce provides type coercion utilities for converting values between different Go types with proper error handling and overflow detection.
Key features:
- Safe type conversions with detailed error messages
- Support for primitive types, big.Int, complex numbers, and time.Time
- Generic To[T] function for unified coercion API
- Overflow detection for numeric conversions
Usage:
val, err := coerce.To[int64]("123")
if err != nil {
// handle error
}
result, err := coerce.ToBool("true") // true, nil
result, err := coerce.ToString(123) // "123", nil
Index ¶
- Variables
- func NewEmptyInputError(target string) error
- func NewFormatError(value, target string) error
- func NewNegativeError(value any, target string) error
- func NewNilPointerError(target string) error
- func NewNotWholeError(value any) error
- func NewOverflowError(value any, target string) error
- func NewUnsupportedError(from, to string) error
- func To[T any](v any) (T, error)
- func ToBigInt(v any) (*big.Int, error)
- func ToBool(v any) (bool, error)
- func ToComplex64(v any) (complex64, error)
- func ToComplex128(v any) (complex128, error)
- func ToComplexFromString(s string) (complex128, error)
- func ToFloat[T ~float32 | ~float64](v any) (T, error)
- func ToFloat64(v any) (float64, error)
- func ToInt64(v any) (int64, error)
- func ToInteger[...](v any) (T, error)
- func ToLiteral(v any) (any, error)
- func ToString(v any) (string, error)
- func ToTime(v any) (time.Time, error)
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrUnsupported = errors.New("conversion not supported") ErrNilPointer = errors.New("nil pointer") ErrInvalidFmt = errors.New("invalid format") ErrEmptyInput = errors.New("empty input") ErrOverflow = errors.New("value overflow") ErrNegative = errors.New("negative to unsigned") ErrNotWhole = errors.New("not whole number") )
Functions ¶
func NewEmptyInputError ¶
func NewFormatError ¶
func NewNegativeError ¶ added in v0.6.0
func NewNilPointerError ¶ added in v0.6.0
func NewNotWholeError ¶
func NewOverflowError ¶
func NewUnsupportedError ¶
func ToComplex64 ¶
ToComplex64 converts any value to complex64.
func ToComplex128 ¶
func ToComplex128(v any) (complex128, error)
ToComplex128 converts any value to complex128.
func ToComplexFromString ¶
func ToComplexFromString(s string) (complex128, error)
ToComplexFromString parses a complex number from its string representation.
func ToInteger ¶
func ToInteger[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64](v any) (T, error)
ToInteger converts any value to the specified integer type T with bounds checking.
func ToLiteral ¶
ToLiteral converts a value to its most natural Go literal representation. The error return is reserved for future use and is currently always nil.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.