/safecast

Safe Numeric Type Cast Library for Go

Primary LanguageGoApache License 2.0Apache-2.0

safecast

Safe Numeric Type Cast Library for Go

English | 简体中文

License Apache 2.0 Golang Build Status Coverage Status GoReport Go Reference

Safe numeric type cast library. suppoer all integral and floating types, except uintptr.

This library depends on go generics, which is introduced in 1.18+.

Usage:

val, ok := To[type](value)

ok == false indicates overflow occured. but whatever,val is always equals to the result of the normal type cast (type(value)) expression。

There are also non-parametric forms of the target types:

val, ok := ToInt32(value)

The usage is the same as the according full generic version, and the performance will be better.

safecast

import "github.com/chen3feng/safecast"

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

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 (Float Overflow)

package main

import (
	"fmt"
	"github.com/chen3feng/safecast"
	"math"
)

func main() {
	n, ok := safecast.To[float32](math.MaxFloat32 * 2)
	fmt.Print(n, ok)
}

Output

+Inf false

Example (Int No Overflow)

package main

import (
	"fmt"
	"github.com/chen3feng/safecast"
)

func main() {
	b, ok := safecast.To[byte](255)
	fmt.Print(b, ok)
}

Output

255 true

Example (Int Overflow)

package main

import (
	"fmt"
	"github.com/chen3feng/safecast"
)

func main() {
	b, ok := safecast.To[byte](256)
	fmt.Print(b, ok)
}

Output

0 false

Example (Value In Range)

package main

import (
	"fmt"
	"github.com/chen3feng/safecast"
)

func main() {
	n, ok := safecast.To[uint](1)
	fmt.Print(n, ok)
}

Output

1 true

Example (Value Out Of Range)

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[FromType numericType](value FromType) (float32, bool)

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[FromType numericType](value FromType) (float64, bool)

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

func ToInt[FromType numericType](value FromType) (int, bool)

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

func ToInt16[FromType numericType](value FromType) (int16, bool)

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

func ToInt32[FromType numericType](value FromType) (int32, bool)

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

func ToInt64[FromType numericType](value FromType) (int64, bool)

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

func ToInt8[FromType numericType](value FromType) (int8, bool)

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

func ToUint[FromType numericType](value FromType) (uint, bool)

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[FromType numericType](value FromType) (uint16, bool)

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[FromType numericType](value FromType) (uint32, bool)

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.

func ToUint64[FromType numericType](value FromType) (uint64, bool)

ToUint64 converts value to uint64 type safely. result will always be same as the usual type cast(uint64(value)), but ok is false when overflow or underflow occured.

func ToUint8

func ToUint8[FromType numericType](value FromType) (uint8, bool)

ToUint8 converts value to uint8 type safely. result will always be same as the usual type cast(uint8(value)), but ok is false when overflow or underflow occured.

Generated by gomarkdoc