Documentation
¶
Overview ¶
Package safeconv provides safe type conversion functions. Must* variants panic on overflow, Safe* variants clamp, To* variants extract typed values.
Index ¶
- Constants
- func Extract[T any](v any) (T, bool)
- func MustConvert[From, To Integer](v From) To
- func MustIntToUint(v int) uint
- func MustIntToUint32(v int) uint32
- func MustUintToInt(v uint) int
- func SafeConvert[From, To Integer](v From) To
- func SafeInt(v uint64) int
- func SafeInt64(v uint64) int64
- func ToFloat64(value any) (float64, bool)
- func ToInt(value any) (int, bool)
- type Integer
Constants ¶
const MaxInt = int(^uint(0) >> 1)
MaxInt is the maximum value for int type (platform-dependent).
const MaxInt64 = int64(math.MaxInt64)
MaxInt64 is the maximum value for int64 type.
const MaxUint32 = uint32(math.MaxUint32)
MaxUint32 is the maximum value for uint32 type.
Variables ¶
This section is empty.
Functions ¶
func Extract ¶
Extract type-asserts v (type any) to T. If the direct assertion fails, it attempts numeric coercion via reflect for numeric source and target types. Returns (zero, false) for nil, non-numeric coercion, or type mismatch.
func MustConvert ¶
func MustConvert[From, To Integer](v From) To
MustConvert converts v from From to To, panicking on overflow or sign loss.
func MustIntToUint ¶
MustIntToUint converts int to uint, panics if negative. Prefer MustConvert[int, uint] for new code.
func MustIntToUint32 ¶
MustIntToUint32 converts int to uint32, panics on bounds violation. Prefer MustConvert[int, uint32] for new code.
func MustUintToInt ¶
MustUintToInt converts uint to int, panics on overflow. Prefer MustConvert[uint, int] for new code.
func SafeConvert ¶
func SafeConvert[From, To Integer](v From) To
SafeConvert converts v from From to To, clamping to the target type's range on overflow.
func SafeInt ¶
SafeInt converts uint64 to int, clamping on overflow. Prefer SafeConvert[uint64, int] for new code.
func SafeInt64 ¶
SafeInt64 converts uint64 to int64, clamping on overflow. Prefer SafeConvert[uint64, int64] for new code.