Documentation
¶
Overview ¶
Package typeconv provides safe type conversion utilities for Go.
It handles interface{} values commonly encountered when working with JSON, configuration files, and external APIs, converting them to concrete Go types with clear error messages for unsupported conversions.
Index ¶
- func Deref[T any](p *T, fallback T) T
- func DerefOrZero[T any](p *T) T
- func MustBool(v any) bool
- func MustDuration(v any) time.Duration
- func MustFloat64(v any) float64
- func MustInt(v any) int
- func MustInt64(v any) int64
- func MustIntSlice(v any) []int
- func MustString(v any) string
- func MustTime(v any) time.Time
- func Ptr[T any](v T) *T
- func ToBool(v any) (bool, error)
- func ToDuration(v any) (time.Duration, error)
- func ToFloat64(v any) (float64, error)
- func ToFloat64Slice(v any) ([]float64, error)
- func ToInt(v any) (int, error)
- func ToInt64(v any) (int64, error)
- func ToIntSlice(v any) ([]int, error)
- func ToMap(v any) (map[string]any, error)
- func ToString(v any) (string, error)
- func ToStringSlice(v any) ([]string, error)
- func ToTime(v any) (time.Time, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Deref ¶
func Deref[T any](p *T, fallback T) T
Deref dereferences a pointer, returning the pointed-to value. If the pointer is nil, it returns the provided fallback value.
func DerefOrZero ¶
func DerefOrZero[T any](p *T) T
DerefOrZero dereferences a pointer, returning the pointed-to value. If the pointer is nil, it returns the zero value for the type.
func MustDuration ¶
MustDuration converts a value to time.Duration, panicking if the conversion fails.
func MustFloat64 ¶
MustFloat64 converts a value to float64, panicking if the conversion fails.
func MustIntSlice ¶ added in v0.2.0
MustIntSlice converts a value to []int, panicking if the conversion fails.
func MustString ¶
MustString converts a value to string, panicking if the conversion fails.
func MustTime ¶ added in v0.2.0
MustTime converts a value to time.Time, panicking if the conversion fails.
func Ptr ¶
func Ptr[T any](v T) *T
Ptr returns a pointer to the given value. This is useful for creating pointers to literals or inline values.
func ToBool ¶
ToBool converts a value to bool. Supported source types:
- bool (pass through)
- string ("true", "false", "1", "0", "yes", "no" — case insensitive)
- int, int8, int16, int32, int64 (0=false, nonzero=true)
- uint, uint8, uint16, uint32, uint64 (0=false, nonzero=true)
func ToDuration ¶
ToDuration converts a value to time.Duration. Supported source types:
- time.Duration (pass through)
- string (parsed via time.ParseDuration, e.g. "30s", "5m", "1h30m")
- int64 (interpreted as nanoseconds)
func ToFloat64 ¶
ToFloat64 converts a value to float64. Supported source types:
- int, int8, int16, int32, int64
- uint, uint8, uint16, uint32, uint64
- float32, float64
- string (parsed via strconv.ParseFloat)
func ToFloat64Slice ¶ added in v0.2.0
ToFloat64Slice converts a value to []float64. Supported source types:
- []float64 (pass through)
- []any (each element is converted via ToFloat64)
- []string (each element is parsed via ToFloat64)
- []int (each element is cast to float64)
- []int64 (each element is cast to float64)
func ToInt ¶
ToInt converts a value to int. Supported source types:
- int, int8, int16, int32, int64
- uint, uint8, uint16, uint32, uint64
- float32, float64 (only if the value has no fractional part)
- string (parsed via strconv.Atoi)
- bool (true=1, false=0)
func ToInt64 ¶
ToInt64 converts a value to int64. Supported source types:
- int, int8, int16, int32, int64
- uint, uint8, uint16, uint32, uint64
- float32, float64 (only if the value has no fractional part)
- string (parsed via strconv.ParseInt)
- bool (true=1, false=0)
func ToIntSlice ¶ added in v0.2.0
ToIntSlice converts a value to []int. Supported source types:
- []int (pass through)
- []any (each element is converted via ToInt)
- []string (each element is parsed via ToInt)
- []float64 (each element is converted via ToInt)
- []int64 (each element is cast to int)
func ToMap ¶ added in v0.2.0
ToMap converts a value to map[string]any. Supported source types:
- map[string]any (pass through)
- struct (exported fields become keys, using field names)
func ToString ¶
ToString converts a value to string. Supported source types:
- string (pass through)
- []byte
- fmt.Stringer (calls String())
- numeric types and bool (via fmt.Sprint)
func ToStringSlice ¶
ToStringSlice converts a value to []string. Supported source types:
- []string (pass through)
- []any (each element is converted via ToString)
Types ¶
This section is empty.