Documentation
¶
Overview ¶
Package safecast provide a safe way to cast a numeric value from type A to type B, with overflow and underflow check.
Index ¶
- func To[ToType numericType, FromType numericType](value FromType) (result ToType, ok bool)
- func ToFloat32[FromType numericType](value FromType) (float32, bool)
- func ToFloat64[FromType numericType](value FromType) (float64, bool)
- func ToInt[FromType numericType](value FromType) (int, bool)
- func ToInt16[FromType numericType](value FromType) (int16, bool)
- func ToInt32[FromType numericType](value FromType) (int32, bool)
- func ToInt64[FromType numericType](value FromType) (int64, bool)
- func ToInt8[FromType numericType](value FromType) (int8, bool)
- func ToUint[FromType numericType](value FromType) (uint, bool)
- func ToUint16[FromType numericType](value FromType) (uint16, bool)
- func ToUint32[FromType numericType](value FromType) (uint32, bool)
- func ToUint64[FromType numericType](value FromType) (uint64, bool)
- func ToUint8[FromType numericType](value FromType) (uint8, bool)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func To ¶
func To[ToType numericType, FromType numericType](value FromType) (result ToType, ok bool)
To converts a numeric value from the FromType to the specified ToType type safely. result will always be same as the usual type cast (type(value)), but ok is false when overflow or underflow occured.
Example (FloatOverflow) ¶
package main import ( "fmt" "math" "github.com/chen3feng/safecast" ) func main() { n, ok := safecast.To[float32](math.MaxFloat32 * 2) fmt.Print(n, ok) }
Output: +Inf false
Example (IntNoOverflow) ¶
package main import ( "fmt" "github.com/chen3feng/safecast" ) func main() { b, ok := safecast.To[byte](255) fmt.Print(b, ok) }
Output: 255 true
Example (IntOverflow) ¶
package main import ( "fmt" "github.com/chen3feng/safecast" ) func main() { b, ok := safecast.To[byte](256) fmt.Print(b, ok) }
Output: 0 false
Example (ValueInRange) ¶
package main import ( "fmt" "github.com/chen3feng/safecast" ) func main() { n, ok := safecast.To[uint](1) fmt.Print(n, ok) }
Output: 1 true
Example (ValueOutOfRange) ¶
package main import ( "fmt" "github.com/chen3feng/safecast" ) func main() { n, ok := safecast.To[uint32](-1) fmt.Print(n, ok) }
Output: 4294967295 false
func ToFloat32 ¶
ToFloat32 converts value to float32 type safely. result will always be same as the usual type cast(float32(value)), but ok is false when overflow or underflow occured.
func ToFloat64 ¶
ToFloat64 converts value to float64 type safely. result will always be same as the usual type cast(float64(value)), but ok is false when overflow or underflow occured.
func ToInt ¶
ToInt converts value to int type safely. result will always be same as the usual type cast(int(value)), but ok is false when overflow or underflow occured.
func ToInt16 ¶
ToInt16 converts value to int16 type safely. result will always be same as the usual type cast(int16(value)), but ok is false when overflow or underflow occured.
func ToInt32 ¶
ToInt32 converts value to int32 type safely. result will always be same as the usual type cast(int32(value)), but ok is false when overflow or underflow occured.
func ToInt64 ¶
ToInt64 converts value to int64 type safely. result will always be same as the usual type cast(int64(value)), but ok is false when overflow or underflow occured.
func ToInt8 ¶
ToInt8 converts value to int8 type safely. result will always be same as the usual type cast(int8(value)), but ok is false when overflow or underflow occured.
func ToUint ¶
ToUint converts value to uint type safely. result will always be same as the usual type cast(uint(value)), but ok is false when overflow or underflow occured.
func ToUint16 ¶
ToUint16 converts value to uint16 type safely. result will always be same as the usual type cast(uint16(value)), but ok is false when overflow or underflow occured.
func ToUint32 ¶
ToUint32 converts value to uint32 type safely. result will always be same as the usual type cast(uint32(value)), but ok is false when overflow or underflow occured.
Types ¶
This section is empty.