cast – github.com/spf13/cast Index | Examples | Files | Directories

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.

Example

Code:play 

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

Examples

Functions

func Must

func Must[T any](i any, err error) T

Must is a helper that wraps a call to a cast function and panics if the error is non-nil.

func StringToDate

func StringToDate(s string) (time.Time, error)

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

func StringToDateInDefaultLocation(s string, location *time.Location) (time.Time, error)

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

func To[T Basic](i any) T

To casts any value to a Basic type.

func ToBool

func ToBool(i any) bool

ToBool casts any value to a(n) bool type.

func ToBoolE

func ToBoolE(i any) (bool, error)

ToBoolE casts any value to a bool type.

func ToBoolSlice

func ToBoolSlice(i any) []bool

ToBoolSlice casts any value to a(n) []bool type.

func ToBoolSliceE

func ToBoolSliceE(i any) ([]bool, error)

ToBoolSliceE casts any value to a(n) []bool type.

func ToDuration

func ToDuration(i any) time.Duration

ToDuration casts any value to a(n) time.Duration type.

func ToDurationE

func ToDurationE(i any) (time.Duration, error)

ToDurationE casts any value to a time.Duration type.

func ToDurationSlice

func ToDurationSlice(i any) []time.Duration

ToDurationSlice casts any value to a(n) []time.Duration type.

func ToDurationSliceE

func ToDurationSliceE(i any) ([]time.Duration, error)

ToDurationSliceE casts any value to a(n) []time.Duration type.

func ToE

func ToE[T Basic](i any) (T, error)

ToE casts any value to a Basic type.

func ToFloat32

func ToFloat32(i any) float32

ToFloat32 casts any value to a(n) float32 type.

func ToFloat32E

func ToFloat32E(i any) (float32, error)

ToFloat32E casts an interface to a float32 type.

func ToFloat32SliceE

func ToFloat32SliceE(i any) ([]float32, error)

ToFloat32SliceE casts any value to a(n) []float32 type.

func ToFloat64

func ToFloat64(i any) float64

ToFloat64 casts any value to a(n) float64 type.

func ToFloat64E

func ToFloat64E(i any) (float64, error)

ToFloat64E casts an interface to a float64 type.

func ToFloat64Slice

func ToFloat64Slice(i any) []float64

ToFloat64Slice casts any value to a(n) []float64 type.

func ToFloat64SliceE

func ToFloat64SliceE(i any) ([]float64, error)

ToFloat64SliceE casts any value to a(n) []float64 type.

func ToInt

func ToInt(i any) int

ToInt casts any value to a(n) int type.

func ToInt16

func ToInt16(i any) int16

ToInt16 casts any value to a(n) int16 type.

func ToInt16E

func ToInt16E(i any) (int16, error)

ToInt16E casts an interface to an int16 type.

func ToInt16SliceE

func ToInt16SliceE(i any) ([]int16, error)

ToInt16SliceE casts any value to a(n) []int16 type.

func ToInt32

func ToInt32(i any) int32

ToInt32 casts any value to a(n) int32 type.

func ToInt32E

func ToInt32E(i any) (int32, error)

ToInt32E casts an interface to an int32 type.

func ToInt32SliceE

func ToInt32SliceE(i any) ([]int32, error)

ToInt32SliceE casts any value to a(n) []int32 type.

func ToInt64

func ToInt64(i any) int64

ToInt64 casts any value to a(n) int64 type.

func ToInt64E

func ToInt64E(i any) (int64, error)

ToInt64E casts an interface to an int64 type.

func ToInt64Slice

func ToInt64Slice(i any) []int64

ToInt64Slice casts any value to a(n) []int64 type.

func ToInt64SliceE

func ToInt64SliceE(i any) ([]int64, error)

ToInt64SliceE casts any value to a(n) []int64 type.

func ToInt8

func ToInt8(i any) int8

ToInt8 casts any value to a(n) int8 type.

func ToInt8E

func ToInt8E(i any) (int8, error)

ToInt8E casts an interface to an int8 type.

func ToInt8SliceE

func ToInt8SliceE(i any) ([]int8, error)

ToInt8SliceE casts any value to a(n) []int8 type.

func ToIntE

func ToIntE(i any) (int, error)

ToIntE casts an interface to an int type.

func ToIntSlice

func ToIntSlice(i any) []int

ToIntSlice casts any value to a(n) []int type.

func ToIntSliceE

func ToIntSliceE(i any) ([]int, error)

ToIntSliceE casts any value to a(n) []int type.

func ToNumber

func ToNumber[T Number](i any) T

ToNumber casts any value to a Number type.

func ToNumberE

func ToNumberE[T Number](i any) (T, error)

ToNumberE casts any value to a Number type.

func ToSlice

func ToSlice(i any) []any

ToSlice casts any value to a(n) []any type.

func ToSliceE

func ToSliceE(i any) ([]any, error)

ToSliceE casts any value to a []any type.

func ToString

func ToString(i any) string

ToString casts any value to a(n) string type.

func ToStringE

func ToStringE(i any) (string, error)

ToStringE casts any value to a string type.

func ToStringMap

func ToStringMap(i any) map[string]any

ToStringMap casts any value to a(n) map[string]any type.

func ToStringMapBool

func ToStringMapBool(i any) map[string]bool

ToStringMapBool casts any value to a(n) map[string]bool type.

func ToStringMapBoolE

func ToStringMapBoolE(i any) (map[string]bool, error)

ToStringMapBoolE casts any value to a map[string]bool type.

func ToStringMapE

func ToStringMapE(i any) (map[string]any, error)

ToStringMapE casts any value to a map[string]any type.

func ToStringMapInt

func ToStringMapInt(i any) map[string]int

ToStringMapInt casts any value to a(n) map[string]int type.

func ToStringMapInt64

func ToStringMapInt64(i any) map[string]int64

ToStringMapInt64 casts any value to a(n) map[string]int64 type.

func ToStringMapInt64E

func ToStringMapInt64E(i any) (map[string]int64, error)

ToStringMapInt64E casts any value to a map[string]int64 type.

func ToStringMapIntE

func ToStringMapIntE(i any) (map[string]int, error)

ToStringMapIntE casts any value to a map[string]int type.

func ToStringMapString

func ToStringMapString(i any) map[string]string

ToStringMapString casts any value to a(n) map[string]string type.

func ToStringMapStringE

func ToStringMapStringE(i any) (map[string]string, error)

ToStringMapStringE casts any value to a map[string]string type.

func ToStringMapStringSlice

func ToStringMapStringSlice(i any) map[string][]string

ToStringMapStringSlice casts any value to a(n) map[string][]string type.

func ToStringMapStringSliceE

func ToStringMapStringSliceE(i any) (map[string][]string, error)

ToStringMapStringSliceE casts any value to a map[string][]string type.

func ToStringSlice

func ToStringSlice(i any) []string

ToStringSlice casts any value to a(n) []string type.

func ToStringSliceE

func ToStringSliceE(i any) ([]string, error)

ToStringSliceE casts any value to a []string type.

func ToTime

func ToTime(i any) time.Time

ToTime casts any value to a(n) time.Time type.

func ToTimeE

func ToTimeE(i any) (time.Time, error)

ToTimeE any value to a time.Time type.

func ToTimeInDefaultLocation

func ToTimeInDefaultLocation(i any, location *time.Location) time.Time

ToTimeInDefaultLocation casts any value to a(n) time.Time type.

func ToTimeInDefaultLocationE

func ToTimeInDefaultLocationE(i any, location *time.Location) (tim time.Time, err error)

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

func ToUint(i any) uint

ToUint casts any value to a(n) uint type.

func ToUint16

func ToUint16(i any) uint16

ToUint16 casts any value to a(n) uint16 type.

func ToUint16E

func ToUint16E(i any) (uint16, error)

ToUint16E casts an interface to a uint16 type.

func ToUint16SliceE

func ToUint16SliceE(i any) ([]uint16, error)

ToUint16SliceE casts any value to a(n) []uint16 type.

func ToUint32

func ToUint32(i any) uint32

ToUint32 casts any value to a(n) uint32 type.

func ToUint32E

func ToUint32E(i any) (uint32, error)

ToUint32E casts an interface to a uint32 type.

func ToUint32SliceE

func ToUint32SliceE(i any) ([]uint32, error)

ToUint32SliceE casts any value to a(n) []uint32 type.

func ToUint64

func ToUint64(i any) uint64

ToUint64 casts any value to a(n) uint64 type.

func ToUint64E

func ToUint64E(i any) (uint64, error)

ToUint64E casts an interface to a uint64 type.

func ToUint64SliceE

func ToUint64SliceE(i any) ([]uint64, error)

ToUint64SliceE casts any value to a(n) []uint64 type.

func ToUint8

func ToUint8(i any) uint8

ToUint8 casts any value to a(n) uint8 type.

func ToUint8E

func ToUint8E(i any) (uint8, error)

ToUint8E casts an interface to a uint type.

func ToUint8SliceE

func ToUint8SliceE(i any) ([]uint8, error)

ToUint8SliceE casts any value to a(n) []uint8 type.

func ToUintE

func ToUintE(i any) (uint, error)

ToUintE casts an interface to a uint type.

func ToUintSlice

func ToUintSlice(i any) []uint

ToUintSlice casts any value to a(n) []uint type.

func ToUintSliceE

func ToUintSliceE(i any) ([]uint, error)

ToUintSliceE casts any value to a(n) []uint type.

Types

type Basic

type Basic interface {
	string | bool | Number | time.Time | time.Duration
}

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

PathSynopsis
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.