Documentation
¶
Overview ¶
Package as provides a easy way to convert numeric types with overflow check.
Example ¶
package main import ( "fmt" "github.com/lunemec/as" ) func main() { for _, n := range []int{127, 128} { num, err := as.Int8(n) if err != nil { fmt.Printf("Input invalid: %d, err: %s\n", num, err) } else { fmt.Printf("Input valid: %d\n", num) } } }
Output: Input valid: 127 Input invalid: -128, err: 128 (int) overflows int8
Index ¶
- func Int(v interface{}) (int, error)
- func Int16(v interface{}) (int16, error)
- func Int32(v interface{}) (int32, error)
- func Int64(v interface{}) (int64, error)
- func Int8(v interface{}) (int8, error)
- func SliceT[From Number, To Number](src []From) ([]To, error)
- func SliceTUnchecked[From SliceNumber, To SliceNumber](src []From) []To
- func T[To Number](v any) (To, error)
- func Uint(v interface{}) (uint, error)
- func Uint16(v interface{}) (uint16, error)
- func Uint32(v interface{}) (uint32, error)
- func Uint64(v interface{}) (uint64, error)
- func Uint8(v interface{}) (uint8, error)
- type InvalidTypeError
- type Number
- type OverflowError
- type SliceNumber
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SliceT ¶ added in v1.1.0
SliceT is generic function to convert between slices of numeric types while checking for overflow on each item. NOTE: this function is quite slow, and especially when high % of numbers overflow, the allocations are significant.
Example ¶
package main import ( "fmt" "github.com/lunemec/as" ) func main() { out, err := as.SliceT[int, int8]([]int{127, 128}) fmt.Printf("Output: %+v, error: %+v\n", out, err) }
Output: Output: [127 0], error: 1 error occurred: * at index [1]: 128 (int) overflows int8
func SliceTUnchecked ¶ added in v1.1.0
func SliceTUnchecked[From SliceNumber, To SliceNumber](src []From) []To
SliceTUnchecked is generic function to convert between slices of any numeric type without overflow check.
Example ¶
package main import ( "fmt" "github.com/lunemec/as" ) func main() { out := as.SliceTUnchecked[int, int8]([]int{127, 128}) fmt.Printf("Output: %+v\n", out) }
Output: Output: [127 -128]
func T ¶ added in v1.1.0
T is generic function to allow for easier checked type cast from any type to any Number type.
Example ¶
package main import ( "fmt" "github.com/lunemec/as" ) func main() { for _, n := range []int{127, 128} { num, err := as.T[int8](n) if err != nil { fmt.Printf("Input invalid: %d, err: %s\n", num, err) } else { fmt.Printf("Input valid: %d\n", num) } } }
Output: Input valid: 127 Input invalid: -128, err: 128 (int) overflows int8
Types ¶
type InvalidTypeError ¶
type InvalidTypeError struct { ToType string Value interface{} }
InvalidTypeError is returned when user supplied invalid type to convert.
func (InvalidTypeError) Error ¶
func (e InvalidTypeError) Error() string
type Number ¶ added in v1.1.0
type Number interface { constraints.Integer }
Number constraint is used for type cast into any number. Only Integers for now until float support is added.
type OverflowError ¶
type OverflowError struct { ToType string Value interface{} }
OverflowError is returned when given value overflows maximum number a type can hold.
func (OverflowError) Error ¶
func (e OverflowError) Error() string
type SliceNumber ¶ added in v1.1.0
type SliceNumber interface { constraints.Integer | constraints.Float }
SliceNumber is type constraint for SliceTUnchecked