Thanks to visit codestin.com
Credit goes to github.com

Skip to content

cybergarage/go-safecast

Repository files navigation

go-safecast

GitHub tag (latest SemVer) test Go Reference codecov

go-safecast is a utility function package for safe casting between primitive data types of Go. The following shows an unsafe cast example that causes cast overflow problems, but the unsafe casting is not detected at runtime and compile time.

package main

import (
    "fmt"
    "math"
)

func main() {
    from := math.MaxInt8 + 1
    to := int8(from)
    fmt.Printf("%v != %v", to, from)

    // Output:
    // -128 != 128
}

go-safecast provides utility functions for safe casting, and raises an error if the casting is unsafe as the following.

package main

import (
    "github.com/cybergarage/go-safecast/safecast"
)

func main() {
    from := math.MaxInt8 + 1
    var to int8
    err := safecast.FromInt(from, &to)
    if err := nil {
        fmt.Println(err.Error())
    }

    // Output:
    // cast error : out of range 128 > *int8
}

Currently, go-safecast provides the following safe casting functions.Please see Go Reference to know the functions and examples in more detail.

To functions

The To functions allow you to convert from various types to a specific type, ensuring that the conversion is safe and within the bounds of the target type.

Function From
func ToInt(from any, to *int) error int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, string, bool
func ToInt8(from any, to *int8) error int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, string, bool
func ToInt16(from any, to *int16) error int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, string, bool
func ToInt32(from any, to *int32) error int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, string, bool
func ToInt64(from any, to *int64) error int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, string, bool
func ToUint(from any, to *uint) error int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, string, bool
func ToUint8(from any, to *uint8) error int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, string, bool
func ToUint16(from any, to *uint16) error int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, string, bool
func ToUint32(from any, to *uint32) error int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, string, bool
func ToUint64(from any, to *uint64) error int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, string, bool
func ToFloat32(from any, to *float32) error int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float64, float32, string
func ToFloat64(from any, to *float64) error int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float64, float32, string
func ToString(from any, to *string) error int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float64, float32, bool, string []byte
func ToBool(from any, to *bool) error int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, bool, string
func ToTime(from any, layout string, to *time.Time) error string
func ToBytes(from any, to *[]byte) error string, []byte
func To(from any, to any) error any

From functions

The From functions allow you to convert from various types to a specific type, ensuring that the conversion is safe and within the bounds of the target type. If the conversion is not safe, an error is returned.

Function To
func FromInt(from int, to any) error *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64, *float64, *float32, *string, *bool
func FromInt8(from int8, to any) error *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64, *float64, *float32, *string, *bool
func FromInt16(from int16, to any) error *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64, *float64, *float32, *string, *bool
func FromInt32(from int32, to any) error *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64, *float64, *float32, *string, *bool
func FromInt64(from int64, to any) error *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64, *float64, *float32, *string, *bool
func FromUint(from uint, to any) error *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64, *float64, *float32, *string, *bool
func FromUint8(from uint8, to any) error *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64, *float64, *float32, *string, *bool
func FromUint16(from uint16, to any) error *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64, *float64, *float32, *string, *bool
func FromUint32(from uint32, to any) error *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64, *float64, *float32, *string, *bool
func FromUint64(from uint64, to any) error *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64, *float64, *float32, *string, *bool
func FromFloat32(from float32, to any) error *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64, *float64, *float32, *string
func FromFloat64(from float64, to any) error *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64, *float64, *float32, *string
func FromString(from string, to any) error *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64, *float64, *float32, *bool, *string *[]byte
func FromBool(from bool, to any) error *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64, *bool, *string
func FromByte(from []byte, to any) error *string, *[]byte
func From(from any, to any) error any

Conversion Functions

The conversion functions allow you to convert between different types safely and efficiently. The Equal function checks if two values are equal, while the Compare function compares two values and returns an integer indicating their relative order.

Function
func Equal(v1 any, v2 any) bool
func Compare(v1 any, v2 any) (int, error)

About

Safety cast function package for go

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages