package cast
import "github.com/spf13/cast"
Copyright © 2014 Steve Francia <spf@spf13.com>.
Use of this source code is governed by an MIT-style license that can be found in the LICENSE file.
Package cast provides easy and safe casting in Go.
Code:play
Output:Example¶
package main
import (
"fmt"
"github.com/spf13/cast"
)
func main() {
// Cast a value to another type
{
v, err := cast.ToIntE("1234")
if err != nil {
panic(err)
}
fmt.Printf("%T(%v)\n", v, v)
}
// Alternatively, you can use the generic [ToE] function for [Basic] types
{
v, err := cast.ToE[int]("4321")
if err != nil {
panic(err)
}
fmt.Printf("%T(%v)\n", v, v)
}
// You can suppress errors by using the non-error versions
{
v := cast.ToInt("9876")
fmt.Printf("%T(%v)\n", v, v)
}
// Similarly, there is a generic [To] function for [Basic] types
{
v := cast.To[int]("6789")
fmt.Printf("%T(%v)\n", v, v)
}
// Finally, you can use [Must] to panic if there is an error.
{
v := cast.Must[int](cast.ToE[int]("5555"))
fmt.Printf("%T(%v)\n", v, v)
}
}
int(1234)
int(4321)
int(9876)
int(6789)
int(5555)
Index ¶
- func Must[T any](i any, err error) T
- func StringToDate(s string) (time.Time, error)
- func StringToDateInDefaultLocation(s string, location *time.Location) (time.Time, error)
- func To[T Basic](i any) T
- func ToBool(i any) bool
- func ToBoolE(i any) (bool, error)
- func ToBoolSlice(i any) []bool
- func ToBoolSliceE(i any) ([]bool, error)
- func ToDuration(i any) time.Duration
- func ToDurationE(i any) (time.Duration, error)
- func ToDurationSlice(i any) []time.Duration
- func ToDurationSliceE(i any) ([]time.Duration, error)
- func ToE[T Basic](i any) (T, error)
- func ToFloat32(i any) float32
- func ToFloat32E(i any) (float32, error)
- func ToFloat32SliceE(i any) ([]float32, error)
- func ToFloat64(i any) float64
- func ToFloat64E(i any) (float64, error)
- func ToFloat64Slice(i any) []float64
- func ToFloat64SliceE(i any) ([]float64, error)
- func ToInt(i any) int
- func ToInt16(i any) int16
- func ToInt16E(i any) (int16, error)
- func ToInt16SliceE(i any) ([]int16, error)
- func ToInt32(i any) int32
- func ToInt32E(i any) (int32, error)
- func ToInt32SliceE(i any) ([]int32, error)
- func ToInt64(i any) int64
- func ToInt64E(i any) (int64, error)
- func ToInt64Slice(i any) []int64
- func ToInt64SliceE(i any) ([]int64, error)
- func ToInt8(i any) int8
- func ToInt8E(i any) (int8, error)
- func ToInt8SliceE(i any) ([]int8, error)
- func ToIntE(i any) (int, error)
- func ToIntSlice(i any) []int
- func ToIntSliceE(i any) ([]int, error)
- func ToNumber[T Number](i any) T
- func ToNumberE[T Number](i any) (T, error)
- func ToSlice(i any) []any
- func ToSliceE(i any) ([]any, error)
- func ToString(i any) string
- func ToStringE(i any) (string, error)
- func ToStringMap(i any) map[string]any
- func ToStringMapBool(i any) map[string]bool
- func ToStringMapBoolE(i any) (map[string]bool, error)
- func ToStringMapE(i any) (map[string]any, error)
- func ToStringMapInt(i any) map[string]int
- func ToStringMapInt64(i any) map[string]int64
- func ToStringMapInt64E(i any) (map[string]int64, error)
- func ToStringMapIntE(i any) (map[string]int, error)
- func ToStringMapString(i any) map[string]string
- func ToStringMapStringE(i any) (map[string]string, error)
- func ToStringMapStringSlice(i any) map[string][]string
- func ToStringMapStringSliceE(i any) (map[string][]string, error)
- func ToStringSlice(i any) []string
- func ToStringSliceE(i any) ([]string, error)
- func ToTime(i any) time.Time
- func ToTimeE(i any) (time.Time, error)
- func ToTimeInDefaultLocation(i any, location *time.Location) time.Time
- func ToTimeInDefaultLocationE(i any, location *time.Location) (tim time.Time, err error)
- func ToUint(i any) uint
- func ToUint16(i any) uint16
- func ToUint16E(i any) (uint16, error)
- func ToUint16SliceE(i any) ([]uint16, error)
- func ToUint32(i any) uint32
- func ToUint32E(i any) (uint32, error)
- func ToUint32SliceE(i any) ([]uint32, error)
- func ToUint64(i any) uint64
- func ToUint64E(i any) (uint64, error)
- func ToUint64SliceE(i any) ([]uint64, error)
- func ToUint8(i any) uint8
- func ToUint8E(i any) (uint8, error)
- func ToUint8SliceE(i any) ([]uint8, error)
- func ToUintE(i any) (uint, error)
- func ToUintSlice(i any) []uint
- func ToUintSliceE(i any) ([]uint, error)
- type Basic
- type Number
Examples ¶
Functions ¶
func Must ¶
Must is a helper that wraps a call to a cast function and panics if the error is non-nil.
func StringToDate ¶
StringToDate attempts to parse a string into a time.Time type using a predefined list of formats.
If no suitable format is found, an error is returned.
func StringToDateInDefaultLocation ¶
StringToDateInDefaultLocation casts an empty interface to a time.Time, interpreting inputs without a timezone to be in the given location, or the local timezone if nil.
func To ¶
To casts any value to a Basic type.
func ToBool ¶
ToBool casts any value to a(n) bool type.
func ToBoolE ¶
ToBoolE casts any value to a bool type.
func ToBoolSlice ¶
ToBoolSlice casts any value to a(n) []bool type.
func ToBoolSliceE ¶
ToBoolSliceE casts any value to a(n) []bool type.
func ToDuration ¶
ToDuration casts any value to a(n) time.Duration type.
func ToDurationE ¶
ToDurationE casts any value to a time.Duration type.
func ToDurationSlice ¶
ToDurationSlice casts any value to a(n) []time.Duration type.
func ToDurationSliceE ¶
ToDurationSliceE casts any value to a(n) []time.Duration type.
func ToE ¶
ToE casts any value to a Basic type.
func ToFloat32 ¶
ToFloat32 casts any value to a(n) float32 type.
func ToFloat32E ¶
ToFloat32E casts an interface to a float32 type.
func ToFloat32SliceE ¶
ToFloat32SliceE casts any value to a(n) []float32 type.
func ToFloat64 ¶
ToFloat64 casts any value to a(n) float64 type.
func ToFloat64E ¶
ToFloat64E casts an interface to a float64 type.
func ToFloat64Slice ¶
ToFloat64Slice casts any value to a(n) []float64 type.
func ToFloat64SliceE ¶
ToFloat64SliceE casts any value to a(n) []float64 type.
func ToInt ¶
ToInt casts any value to a(n) int type.
func ToInt16 ¶
ToInt16 casts any value to a(n) int16 type.
func ToInt16E ¶
ToInt16E casts an interface to an int16 type.
func ToInt16SliceE ¶
ToInt16SliceE casts any value to a(n) []int16 type.
func ToInt32 ¶
ToInt32 casts any value to a(n) int32 type.
func ToInt32E ¶
ToInt32E casts an interface to an int32 type.
func ToInt32SliceE ¶
ToInt32SliceE casts any value to a(n) []int32 type.
func ToInt64 ¶
ToInt64 casts any value to a(n) int64 type.
func ToInt64E ¶
ToInt64E casts an interface to an int64 type.
func ToInt64Slice ¶
ToInt64Slice casts any value to a(n) []int64 type.
func ToInt64SliceE ¶
ToInt64SliceE casts any value to a(n) []int64 type.
func ToInt8 ¶
ToInt8 casts any value to a(n) int8 type.
func ToInt8E ¶
ToInt8E casts an interface to an int8 type.
func ToInt8SliceE ¶
ToInt8SliceE casts any value to a(n) []int8 type.
func ToIntE ¶
ToIntE casts an interface to an int type.
func ToIntSlice ¶
ToIntSlice casts any value to a(n) []int type.
func ToIntSliceE ¶
ToIntSliceE casts any value to a(n) []int type.
func ToNumber ¶
ToNumber casts any value to a Number type.
func ToNumberE ¶
ToNumberE casts any value to a Number type.
func ToSlice ¶
ToSlice casts any value to a(n) []any type.
func ToSliceE ¶
ToSliceE casts any value to a []any type.
func ToString ¶
ToString casts any value to a(n) string type.
func ToStringE ¶
ToStringE casts any value to a string type.
func ToStringMap ¶
ToStringMap casts any value to a(n) map[string]any type.
func ToStringMapBool ¶
ToStringMapBool casts any value to a(n) map[string]bool type.
func ToStringMapBoolE ¶
ToStringMapBoolE casts any value to a map[string]bool type.
func ToStringMapE ¶
ToStringMapE casts any value to a map[string]any type.
func ToStringMapInt ¶
ToStringMapInt casts any value to a(n) map[string]int type.
func ToStringMapInt64 ¶
ToStringMapInt64 casts any value to a(n) map[string]int64 type.
func ToStringMapInt64E ¶
ToStringMapInt64E casts any value to a map[string]int64 type.
func ToStringMapIntE ¶
ToStringMapIntE casts any value to a map[string]int type.
func ToStringMapString ¶
ToStringMapString casts any value to a(n) map[string]string type.
func ToStringMapStringE ¶
ToStringMapStringE casts any value to a map[string]string type.
func ToStringMapStringSlice ¶
ToStringMapStringSlice casts any value to a(n) map[string][]string type.
func ToStringMapStringSliceE ¶
ToStringMapStringSliceE casts any value to a map[string][]string type.
func ToStringSlice ¶
ToStringSlice casts any value to a(n) []string type.
func ToStringSliceE ¶
ToStringSliceE casts any value to a []string type.
func ToTime ¶
ToTime casts any value to a(n) time.Time type.
func ToTimeE ¶
ToTimeE any value to a time.Time type.
func ToTimeInDefaultLocation ¶
ToTimeInDefaultLocation casts any value to a(n) time.Time type.
func ToTimeInDefaultLocationE ¶
ToTimeInDefaultLocationE casts an empty interface to time.Time, interpreting inputs without a timezone to be in the given location, or the local timezone if nil.
func ToUint ¶
ToUint casts any value to a(n) uint type.
func ToUint16 ¶
ToUint16 casts any value to a(n) uint16 type.
func ToUint16E ¶
ToUint16E casts an interface to a uint16 type.
func ToUint16SliceE ¶
ToUint16SliceE casts any value to a(n) []uint16 type.
func ToUint32 ¶
ToUint32 casts any value to a(n) uint32 type.
func ToUint32E ¶
ToUint32E casts an interface to a uint32 type.
func ToUint32SliceE ¶
ToUint32SliceE casts any value to a(n) []uint32 type.
func ToUint64 ¶
ToUint64 casts any value to a(n) uint64 type.
func ToUint64E ¶
ToUint64E casts an interface to a uint64 type.
func ToUint64SliceE ¶
ToUint64SliceE casts any value to a(n) []uint64 type.
func ToUint8 ¶
ToUint8 casts any value to a(n) uint8 type.
func ToUint8E ¶
ToUint8E casts an interface to a uint type.
func ToUint8SliceE ¶
ToUint8SliceE casts any value to a(n) []uint8 type.
func ToUintE ¶
ToUintE casts an interface to a uint type.
func ToUintSlice ¶
ToUintSlice casts any value to a(n) []uint type.
func ToUintSliceE ¶
ToUintSliceE casts any value to a(n) []uint type.
Types ¶
type Basic ¶
Basic is a type parameter constraint for functions accepting basic types.
It represents the supported basic types this package can cast to.
type Number ¶
type Number interface { int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 }
Number is a type parameter constraint for functions accepting number types.
It represents the supported number types this package can cast to.
Source Files ¶
alias.go basic.go cast.go indirect.go map.go number.go slice.go time.go zz_generated.go
Directories ¶
Path | Synopsis |
---|---|
internal |
- Version
- v1.9.2 (latest)
- Published
- Jun 2, 2025
- Platform
- js/wasm
- Imports
- 11 packages
- Last checked
- 3 weeks ago –
Tools for package owners.