SafeConversion provides safe type casting functions for Go.
go get github.com/corentings/safeconversion
import "github.com/corentings/safeconversion"
CastInt provides a safe way to cast an integer from one type to another.
func CastInt[From, To safeconversion.Integer](value From) (To, error)
result, err := safeconversion.CastInt[int32, int64](math.MaxInt32)
// result is 9223372036854775807 and err is nil
result, err = safeconversion.CastInt[int64, int32](math.MaxInt64)
// result is 0 and err is safeconversion.ErrValueOutOfRange
- TODO
- TODO
This library is not meant to replace the standard type casting functions. It is meant to provide a safe alternative when the standard functions would silently overflow or underflow. It's not recommended to use this library without auditing the code first to be sure it fulfills your needs.
I am not a security expert, so use this library at your own risk.
This library is licensed under the MIT License. See the LICENSE file for more details.
Contributions are welcome! Please open an issue or submit a pull request.
- rung/go-safecast
- cybergarage/go-safecast
- fortio/safecast - I used this as a reference for the CastInt implementation and highly recommend it.