Documentation
¶
Overview ¶
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.
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)
}
}
Output: 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 ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Must ¶ added in v1.9.0
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 ¶ added in v1.4.0
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 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 ¶ added in v1.1.0
ToDurationSlice casts any value to a(n) []time.Duration type.
func ToDurationSliceE ¶ added in v1.1.0
ToDurationSliceE casts any value to a(n) []time.Duration type.
func ToFloat32E ¶
ToFloat32E casts an interface to a float32 type.
func ToFloat32SliceE ¶ added in v1.9.0
ToFloat32SliceE casts any value to a(n) []float32 type.
func ToFloat64E ¶
ToFloat64E casts an interface to a float64 type.
func ToFloat64Slice ¶ added in v1.8.0
ToFloat64Slice casts any value to a(n) []float64 type.
func ToFloat64SliceE ¶ added in v1.8.0
ToFloat64SliceE casts any value to a(n) []float64 type.
func ToInt16SliceE ¶ added in v1.9.0
ToInt16SliceE casts any value to a(n) []int16 type.
func ToInt32SliceE ¶ added in v1.9.0
ToInt32SliceE casts any value to a(n) []int32 type.
func ToInt64Slice ¶ added in v1.8.0
ToInt64Slice casts any value to a(n) []int64 type.
func ToInt64SliceE ¶ added in v1.8.0
ToInt64SliceE casts any value to a(n) []int64 type.
func ToInt8SliceE ¶ added in v1.9.0
ToInt8SliceE casts any value to a(n) []int8 type.
func ToIntSliceE ¶
ToIntSliceE casts any value to a(n) []int 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 ¶ added in v1.3.0
ToStringMapInt casts any value to a(n) map[string]int type.
func ToStringMapInt64 ¶ added in v1.3.0
ToStringMapInt64 casts any value to a(n) map[string]int64 type.
func ToStringMapInt64E ¶ added in v1.3.0
ToStringMapInt64E casts any value to a map[string]int64 type.
func ToStringMapIntE ¶ added in v1.3.0
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 ToTimeInDefaultLocation ¶ added in v1.4.0
ToTimeInDefaultLocation casts any value to a(n) time.Time type.
func ToTimeInDefaultLocationE ¶ added in v1.4.0
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 ToUint16SliceE ¶ added in v1.9.0
ToUint16SliceE casts any value to a(n) []uint16 type.
func ToUint32SliceE ¶ added in v1.9.0
ToUint32SliceE casts any value to a(n) []uint32 type.
func ToUint64SliceE ¶ added in v1.9.0
ToUint64SliceE casts any value to a(n) []uint64 type.
func ToUint8SliceE ¶ added in v1.9.0
ToUint8SliceE casts any value to a(n) []uint8 type.
func ToUintSlice ¶ added in v1.8.0
ToUintSlice casts any value to a(n) []uint type.
func ToUintSliceE ¶ added in v1.8.0
ToUintSliceE casts any value to a(n) []uint type.