Error-safe value caster.
Create new caster instance from value.
// Signature
NewCaster(data any) Caster
// Example
import "github.com/gomig/caster"
c := caster.NewCaster(1234)
Note: Slice caster methods is type safe and only returns typed items.
import "github.com/gomig/caster"
c := caster.NewCaster([]any{"First", true, false})
c.Slice() // ["First", true, false]
c.BoolSlice(nil) // [true, false]
c.StringSlice(nil) // ["First"]
c.UIntSlice(nil) // nil
Check if returns error is nil error.
// Signature
IsNilErr(err error) bool
// Example
import "github.com/gomig/caster"
c := caster.NewCaster(nil)
v, err := c.Int()
if caster.IsNilErr(err) {
// ...
}
Caster has following methods:
Check if value of caster is nil
IsNil() bool
Get raw value.
Interface() any
Parse data as []any
Slice() []any
Parse data as boolean
or return error on fail
Bool() (bool, error)
Parse data as boolean
or return fallback
BoolSafe(fallback bool) bool
Parse data as []bool
or return fallback
BoolSlice(fallback []bool) []bool
Parse data as int
or return error on fail
Int() (int, error)
Parse data as int
or return fallback
IntSafe(fallback int) int
Parse data as []int
or return fallback
IntSlice(fallback []int) []int
Parse data as int8
or return error on fail
Int8() (int8, error)
Parse data as int8
or return fallback
Int8Safe(fallback int8) int8
Parse data as []int8
or return fallback
Int8Slice(fallback []int8) []int8
Parse data as int16
or return error on fail
Int16() (int16, error)
Parse data as int16
or return fallback
Int16Safe(fallback int16) int16
Parse data as []int16
or return fallback
Int16Slice(fallback []int16) []int16
Parse data as int32
or return error on fail
Int32() (int32, error)
Parse data as int32
or return fallback
Int32Safe(fallback int32) int32
Parse data as []int32
or return fallback
Int32Slice(fallback []int32) []int32
Parse data as int64
or return error on fail
Int64() (int64, error)
Parse data as int64
or return fallback
Int64Safe(fallback int64) int64
Parse data as []int64
or return fallback
Int64Slice(fallback []int64) []int64
Parse data as uint
or return error on fail
UInt() (uint, error)
Parse data as uint
or return fallback
UIntSafe(fallback uint) uint
Parse data as []uint
or return fallback
UIntSlice(fallback []uint) []uint
Parse data as uint8
or return error on fail
UInt8() (uint8, error)
Parse data as uint8
or return fallback
UInt8Safe(fallback uint8) uint8
Parse data as []uint8
or return fallback
UInt8Slice(fallback []uint8) []uint8
Parse data as uint16
or return error on fail
UInt16() (uint16, error)
Parse data as uint16
or return fallback
UInt16Safe(fallback uint16) uint16
Parse data as []uint16
or return fallback
UInt16Slice(fallback []uint16) []uint16
Parse data as uint32
or return error on fail
UInt32() (uint32, error)
Parse data as uint32
or return fallback
UInt32Safe(fallback uint32) uint32
Parse data as []uint32
or return fallback
UInt32Slice(fallback []uint32) []uint32
Parse data as uint64
or return error on fail
UInt64() (uint64, error)
Parse data as uint64
or return fallback
UInt64Safe(fallback uint64) uint64
Parse data as []uint64
or return fallback
UInt64Slice(fallback []uint64) []uint64
Parse data as float32
or return error on fail
Float32() (float32, error)
Parse data as float32
or return fallback
Float32Safe(fallback float32) float32
Parse data as []float32
or return fallback
Float32Slice(fallback []float32) []float32
Parse data as float64
or return error on fail
Float64() (float64, error)
Parse data as float64
or return fallback
Float64Safe(fallback float64) float64
Parse data as []float64
or return fallback
Float64Slice(fallback []float64) []float64
Parse data as string
or return error on fail
String() (string, error)
Parse data as string
or return fallback
StringSafe(fallback string) string
Parse data as []string
or return fallback
StringSlice(fallback []string) []string