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 ¶
var ( ErrUnsupported = errors.New("gozod: conversion not supported") ErrNilPointer = errors.New("gozod: nil pointer") ErrInvalidFmt = errors.New("gozod: invalid format") ErrEmptyInput = errors.New("gozod: empty input") ErrOverflow = errors.New("gozod: value overflow") ErrNegative = errors.New("gozod: negative to unsigned") ErrNotWhole = errors.New("gozod: not whole number") )
Functions ¶
func NewEmptyInputError ¶
NewEmptyInputError creates an error for empty input during conversion.
func NewFormatError ¶
NewFormatError creates an error for invalid format during parsing.
func NewNegativeError ¶ added in v0.6.0
NewNegativeError creates an error for negative to unsigned conversion.
func NewNilPointerError ¶ added in v0.6.0
NewNilPointerError creates an error for nil pointer conversion.
func NewNotWholeError ¶
NewNotWholeError creates an error for non-whole number conversion.
func NewOverflowError ¶
NewOverflowError creates an error for value overflow during conversion.
func NewUnsupportedError ¶
NewUnsupportedError creates an error for unsupported type conversions.
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.