The cast
package provides utilities for type casting and conversion in Go. It includes functions for converting values to various Go types, such as bool
, int
, uint
, float
, string
, and their respective slices. Additionally, it offers a Caster
interface for more structured type conversion.
To install the cast
package, use the following command:
go get github.com/go-universal/cast
Then, import the package in your Go code:
import "github.com/go-universal/cast"
Converts an interface to a bool
.
Signature:
func ToBool(value interface{}) (bool, error)
Example:
result, err := cast.ToBool("true")
fmt.Println(result) // Output: true
Converts an interface to a slice of bool
.
Signature:
func ToBoolSlice(value interface{}) ([]bool, error)
Example:
result, err := cast.ToBoolSlice([]string{"true", "false"})
fmt.Println(result) // Output: [true false]
Converts an interface to a signed integer type (int
, int8
, int16
, int32
, int64
).
Signature:
func ToSigned[T int | int8 | int16 | int32 | int64](value interface{}) (T, error)
Example:
result, err := cast.ToSigned[int]("123")
fmt.Println(result) // Output: 123
Converts an interface to a slice of signed integers.
Signature:
func ToSignedSlice[T int | int8 | int16 | int32 | int64](value interface{}) ([]T, error)
Example:
result, err := cast.ToSignedSlice[int]([]string{"1", "2"})
fmt.Println(result) // Output: [1 2]
Converts an interface to an unsigned integer type (uint
, uint8
, uint16
, uint32
, uint64
).
Signature:
func ToUnsigned[T uint | uint8 | uint16 | uint32 | uint64](value interface{}) (T, error)
Example:
result, err := cast.ToUnsigned[uint]("123")
fmt.Println(result) // Output: 123
Converts an interface to a slice of unsigned integers.
Signature:
func ToUnsignedSlice[T uint | uint8 | uint16 | uint32 | uint64](value interface{}) ([]T, error)
Example:
result, err := cast.ToUnsignedSlice[uint]([]string{"1", "2"})
fmt.Println(result) // Output: [1 2]
Converts an interface to a float type (float32
, float64
).
Signature:
func ToFloat[T float32 | float64](value interface{}) (T, error)
Example:
result, err := cast.ToFloat[float64]("123.45")
fmt.Println(result) // Output: 123.45
Converts an interface to a slice of floats.
Signature:
func ToFloatSlice[T float32 | float64](value interface{}) ([]T, error)
Example:
result, err := cast.ToFloatSlice[float64]([]string{"1.1", "2.2"})
fmt.Println(result) // Output: [1.1 2.2]
Converts an interface to a string
.
Signature:
func ToString(value interface{}) (string, error)
Example:
result, err := cast.ToString(123)
fmt.Println(result) // Output: "123"
Converts an interface to a slice of string
.
Signature:
func ToStringSlice(value interface{}) ([]string, error)
Example:
result, err := cast.ToStringSlice([]int{1, 2})
fmt.Println(result) // Output: ["1" "2"]
Converts an interface to a slice of interface{}
.
Signature:
func ToSlice(value interface{}) ([]interface{}, error)
Example:
result, err := cast.ToSlice([]int{1, 2})
fmt.Println(result) // Output: [1 2]
The Caster
interface provides methods for type casting and conversion. It allows structured and reusable type conversions with fallback mechanisms.
IsNil() bool
: Checks if the value is nil.Interface() interface{}
: Returns the value as aninterface{}
.Bool() (bool, error)
: Converts the value to abool
.BoolSafe(fallback bool) bool
: Converts the value to abool
, returning a fallback value on 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 on error.Int() (int, error)
: Converts the value to anint
.IntSafe(fallback int) int
: Converts the value to anint
, returning a fallback value on 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 on error.Float64() (float64, error)
: Converts the value to afloat64
.Float64Safe(fallback float64) float64
: Converts the value to afloat64
, returning a fallback value on error.String() (string, error)
: Converts the value to astring
.StringSafe(fallback string) string
: Converts the value to astring
, returning a fallback value on error.
caster := cast.NewCaster("123")
// Convert to int
intValue, err := caster.Int()
fmt.Println(intValue) // Output: 123
// Convert to bool with fallback
boolValue := caster.BoolSafe(false)
fmt.Println(boolValue) // Output: true
// Convert to string slice
stringSlice, err := caster.StringSlice()
fmt.Println(stringSlice) // Output: ["1" "2" "3"]
The package provides utility functions to identify specific error types:
IsNilError(err error) bool
: Checks if the error is due to a nil value.IsCastError(err error) bool
: Checks if the error is due to an invalid type conversion.IsOverflowError(err error) bool
: Checks if the error is due to a value overflow.
This documentation provides a comprehensive overview of the cast
package and its capabilities. For more details, refer to the source code or examples provided in the repository.