Package gocast
provides utilities for type casting and conversion in Go. It defines an interface Caster
with methods for converting values to various Go types, including primary types like bool
, int
, uint
, float
, and string
, as well as slices of these types. The package also includes functions for casting values to these types and handling errors related to type conversion.
To install the gocast
package, use the following command:
go get github.com/mekramy/gocast
Then, import the package in your Go code:
import (
"github.com/mekramy/gocast"
)
The package provides functions for casting values to various types and handling errors related to type conversion.
func ToBool(value interface{}) (bool, error)
Casts an interface to a bool
type.
func ToSigned[T int | int8 | int16 | int32 | int64](value interface{}) (T, error)
Casts an interface to a signed integer type.
func ToUnsigned[T uint | uint8 | uint16 | uint32 | uint64](value interface{}) (T, error)
Casts an interface to an unsigned integer type.
func ToFloat[T float32 | float64](value interface{}) (T, error)
Casts an interface to a float type.
func ToString(value interface{}) (string, error)
Casts an interface to a string
type.
func ToSlice(value interface{}) ([]interface{}, error)
Casts an interface to a []interface{}
type.
func ToBoolSlice(i interface{}) ([]bool, error)
Casts an interface to a []bool
type.
func ToSignedSlice[T int | int8 | int16 | int32 | int64](i interface{}) ([]T, error)
Casts an interface to a signed integer slice type.
func ToUnsignedSlice[T uint | uint8 | uint16 | uint32 | uint64](i interface{}) ([]T, error)
Casts an interface to an unsigned integer slice type.
func ToFloatSlice[T float32 | float64](i interface{}) ([]T, error)
Casts an interface to a float slice type.
func ToStringSlice(i interface{}) ([]string, error)
Casts an interface to a []string
type.
package main
import (
"fmt"
"github.com/mekramy/gocast"
)
func main() {
// Example of ToBool
boolValue, err := gocast.ToBool("true")
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Bool value:", boolValue) // output: true
}
// Example of ToSigned
intValue, err := gocast.ToSigned[int]("-123.32")
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Int value:", intValue) // output: -123
}
// Example of ToString
stringValue, err := gocast.ToString(123)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("String value:", stringValue) // output: "123"
}
}
The Caster
interface provides methods for type casting and conversion. It includes methods for checking if a value is nil, retrieving the value as an interface, and converting the value to primary Go types such as bool
, int
, uint
, float
, and string
. Each type conversion method has a corresponding safe method that returns a fallback value in case of an error, and methods for converting to slices of each type.
IsNil() bool
: Checks if the value is nil.Interface() any
: Returns the value as aninterface{}
.Unmarshal(out any) error
: Unmarshals the value using a JSON decoder.Bool() (bool, error)
: Converts the value to abool
.BoolSafe(fallback bool) bool
: Converts the value to abool
, returning a fallback value in case of an error.Int() (int, error)
: Converts the value to anint
.IntSafe(fallback int) int
: Converts the value to anint
, returning a fallback value in case of an error.Int8() (int8, error)
: Converts the value to anint8
.Int8Safe(fallback int8) int8
: Converts the value to anint8
, returning a fallback value in case of an error.Int16() (int16, error)
: Converts the value to anint16
.Int16Safe(fallback int16) int16
: Converts the value to anint16
, returning a fallback value in case of an error.Int32() (int32, error)
: Converts the value to anint32
.Int32Safe(fallback int32) int32
: Converts the value to anint32
, returning a fallback value in case of an error.Int64() (int64, error)
: Converts the value to anint64
.Int64Safe(fallback int64) int64
: Converts the value to anint64
, returning a fallback value in case of an error.Uint() (uint, error)
: Converts the value to auint
.UintSafe(fallback uint) uint
: Converts the value to auint
, returning a fallback value in case of an error.Uint8() (uint8, error)
: Converts the value to auint8
.Uint8Safe(fallback uint8) uint8
: Converts the value to auint8
, returning a fallback value in case of an error.Uint16() (uint16, error)
: Converts the value to auint16
.Uint16Safe(fallback uint16) uint16
: Converts the value to auint16
, returning a fallback value in case of an error.Uint32() (uint32, error)
: Converts the value to auint32
.Uint32Safe(fallback uint32) uint32
: Converts the value to auint32
, returning a fallback value in case of an error.Uint64() (uint64, error)
: Converts the value to auint64
.Uint64Safe(fallback uint64) uint64
: Converts the value to auint64
, returning a fallback value in case of an error.Float32() (float32, error)
: Converts the value to afloat32
.Float32Safe(fallback float32) float32
: Converts the value to afloat32
, returning a fallback value in case of an error.Float64() (float64, error)
: Converts the value to afloat64
.Float64Safe(fallback float64) float64
: Converts the value to afloat64
, returning a fallback value in case of an error.String() (string, error)
: Converts the value to astring
.StringSafe(fallback string) string
: Converts the value to astring
, returning a fallback value in case of an error.Slice() ([]any, error)
: Returns the value as a slice ofinterface{}
.SliceSafe(fallback []any) []any
: Returns the value as a slice ofinterface{}
, returning a fallback value in case of an error.BoolSlice() ([]bool, error)
: Converts the value to a slice ofbool
.BoolSliceSafe(fallback []bool) []bool
: Converts the value to a slice ofbool
, returning a fallback value in case of an error.IntSlice() ([]int, error)
: Converts the value to a slice ofint
.IntSliceSafe(fallback []int) []int
: Converts the value to a slice ofint
, returning a fallback value in case of an error.Int8Slice() ([]int8, error)
: Converts the value to a slice ofint8
.Int8SliceSafe(fallback []int8) []int8
: Converts the value to a slice ofint8
, returning a fallback value in case of an error.Int16Slice() ([]int16, error)
: Converts the value to a slice ofint16
.Int16SliceSafe(fallback []int16) []int16
: Converts the value to a slice ofint16
, returning a fallback value in case of an error.Int32Slice() ([]int32, error)
: Converts the value to a slice ofint32
.Int32SliceSafe(fallback []int32) []int32
: Converts the value to a slice ofint32
, returning a fallback value in case of an error.Int64Slice() ([]int64, error)
: Converts the value to a slice ofint64
.Int64SliceSafe(fallback []int64) []int64
: Converts the value to a slice ofint64
, returning a fallback value in case of an error.UintSlice() ([]uint, error)
: Converts the value to a slice ofuint
.UintSliceSafe(fallback []uint) []uint
: Converts the value to a slice ofuint
, returning a fallback value in case of an error.Uint8Slice() ([]uint8, error)
: Converts the value to a slice ofuint8
.Uint8SliceSafe(fallback []uint8) []uint8
: Converts the value to a slice ofuint8
, returning a fallback value in case of an error.Uint16Slice() ([]uint16, error)
: Converts the value to a slice ofuint16
.Uint16SliceSafe(fallback []uint16) []uint16
: Converts the value to a slice ofuint16
, returning a fallback value in case of an error.Uint32Slice() ([]uint32, error)
: Converts the value to a slice ofuint32
.Uint32SliceSafe(fallback []uint32) []uint32
: Converts the value to a slice ofuint32
, returning a fallback value in case of an error.Uint64Slice() ([]uint64, error)
: Converts the value to a slice ofuint64
.Uint64SliceSafe(fallback []uint64) []uint64
: Converts the value to a slice ofuint64
, returning a fallback value in case of an error.Float32Slice() ([]float32, error)
: Converts the value to a slice offloat32
.Float32SliceSafe(fallback []float32) []float32
: Converts the value to a slice offloat32
, returning a fallback value in case of an error.Float64Slice() ([]float64, error)
: Converts the value to a slice offloat64
.Float64SliceSafe(fallback []float64) []float64
: Converts the value to a slice offloat64
, returning a fallback value in case of an error.StringSlice() ([]string, error)
: Converts the value to a slice ofstring
.StringSliceSafe(fallback []string) []string
: Converts the value to a slice ofstring
, returning a fallback value in case of an error.
package main
import (
"fmt"
"github.com/mekramy/gocast"
)
// Parse int
var value interface{} = "asdfasdf"
caster := gocast.New(value)
intValue, err := caster.Int() // output: error
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Int value:", intValue)
}
// Parse bool
var value interface{} = "1"
caster := gocast.New(value)
boolValue, err := caster.Bool()
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Bool value:", boolValue) // output: true
}
// Parse string
var value interface{} = 123.392
caster := gocast.New(value)
stringValue, err := caster.String()
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("String value:", stringValue) // output: "123.392"
}
The package defines error handling functions for type conversion errors.
func IsNilError(err error) bool
Checks if the provided error is a nil error. Returns true if the error is not nil and is a nil error.
func IsCastError(err error) bool
Checks if the provided error is a casting error. Returns true if the error is not nil and is a casting error.
func IsOverflowError(err error) bool
Checks if the provided error is a overflow error. Returns true if the error is not nil and is a overflow error.
The library also defines several provider interfaces for different types and slices. These interfaces provide methods to converting custom types like standard go Stringer
interface.
SliceProvider
: Provides a method to return a slice of any type.SliceErrorProvider
: Provides a method to return a slice of any type with an error.
BoolProvider
: Provides a method to return a boolean value.BoolErrorProvider
: Provides a method to return a boolean value with an error.BoolSliceProvider
: Provides a method to return a slice of boolean values.BoolSliceErrorProvider
: Provides a method to return a slice of boolean values with an error.
IntProvider
: Provides a method to return an integer value.IntErrorProvider
: Provides a method to return an integer value with an error.IntSliceProvider
: Provides a method to return a slice of integer values.IntSliceErrorProvider
: Provides a method to return a slice of integer values with an error.
Int8Provider
: Provides a method to return an int8 value.Int8ErrorProvider
: Provides a method to return an int8 value with an error.Int8SliceProvider
: Provides a method to return a slice of int8 values.Int8SliceErrorProvider
: Provides a method to return a slice of int8 values with an error.
Int16Provider
: Provides a method to return an int16 value.Int16ErrorProvider
: Provides a method to return an int16 value with an error.Int16SliceProvider
: Provides a method to return a slice of int16 values.Int16SliceErrorProvider
: Provides a method to return a slice of int16 values with an error.
Int32Provider
: Provides a method to return an int32 value.Int32ErrorProvider
: Provides a method to return an int32 value with an error.Int32SliceProvider
: Provides a method to return a slice of int32 values.Int32SliceErrorProvider
: Provides a method to return a slice of int32 values with an error.
Int64Provider
: Provides a method to return an int64 value.Int64ErrorProvider
: Provides a method to return an int64 value with an error.Int64SliceProvider
: Provides a method to return a slice of int64 values.Int64SliceErrorProvider
: Provides a method to return a slice of int64 values with an error.
UintProvider
: Provides a method to return a uint value.UintErrorProvider
: Provides a method to return a uint value with an error.UintSliceProvider
: Provides a method to return a slice of uint values.UintSliceErrorProvider
: Provides a method to return a slice of uint values with an error.
Uint8Provider
: Provides a method to return a uint8 value.Uint8ErrorProvider
: Provides a method to return a uint8 value with an error.Uint8SliceProvider
: Provides a method to return a slice of uint8 values.Uint8SliceErrorProvider
: Provides a method to return a slice of uint8 values with an error.
Uint16Provider
: Provides a method to return a uint16 value.Uint16ErrorProvider
: Provides a method to return a uint16 value with an error.Uint16SliceProvider
: Provides a method to return a slice of uint16 values.Uint16SliceErrorProvider
: Provides a method to return a slice of uint16 values with an error.
Uint32Provider
: Provides a method to return a uint32 value.Uint32ErrorProvider
: Provides a method to return a uint32 value with an error.Uint32SliceProvider
: Provides a method to return a slice of uint32 values.Uint32SliceErrorProvider
: Provides a method to return a slice of uint32 values with an error.
Uint64Provider
: Provides a method to return a uint64 value.Uint64ErrorProvider
: Provides a method to return a uint64 value with an error.Uint64SliceProvider
: Provides a method to return a slice of uint64 values.Uint64SliceErrorProvider
: Provides a method to return a slice of uint64 values with an error.
Float32Provider
: Provides a method to return a float32 value.Float32ErrorProvider
: Provides a method to return a float32 value with an error.Float32SliceProvider
: Provides a method to return a slice of float32 values.Float32SliceErrorProvider
: Provides a method to return a slice of float32 values with an error.
Float64Provider
: Provides a method to return a float64 value.Float64ErrorProvider
: Provides a method to return a float64 value with an error.Float64SliceProvider
: Provides a method to return a slice of float64 values.Float64SliceErrorProvider
: Provides a method to return a slice of float64 values with an error.
StringProvider
: Provides a method to return a string value.StringErrorProvider
: Provides a method to return a string value with an error.StringSliceProvider
: Provides a method to return a slice of string values.StringSliceErrorProvider
: Provides a method to return a slice of string values with an error.