cast

package module
v1.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 8, 2025 License: MIT Imports: 11 Imported by: 14,032

README

cast

GitHub Workflow Status go.dev reference GitHub go.mod Go version OpenSSF Scorecard

Easy and safe casting from one type to another in Go

Don’t Panic! ... Cast

What is Cast?

Cast is a library to convert between different go types in a consistent and easy way.

Cast provides simple functions to easily convert a number to a string, an interface into a bool, etc. Cast does this intelligently when an obvious conversion is possible. It doesn’t make any attempts to guess what you meant, for example you can only convert a string to an int when it is a string representation of an int such as “8”. Cast was developed for use in Hugo, a website engine which uses YAML, TOML or JSON for meta data.

Why use Cast?

When working with dynamic data in Go you often need to cast or convert the data from one type into another. Cast goes beyond just using type assertion (though it uses that when possible) to provide a very straightforward and convenient library.

If you are working with interfaces to handle things like dynamic content you’ll need an easy way to convert an interface into a given type. This is the library for you.

If you are taking in data from YAML, TOML or JSON or other formats which lack full types, then Cast is the library for you.

Usage

Cast provides a handful of To_____ methods. These methods will always return the desired type. If input is provided that will not convert to that type, the 0 or nil value for that type will be returned.

Cast also provides identical methods To_____E. These return the same result as the To_____ methods, plus an additional error which tells you if it successfully converted. Using these methods you can tell the difference between when the input matched the zero value or when the conversion failed and the zero value was returned.

The following examples are merely a sample of what is available. Please review the code for a complete set.

Example ‘ToString’:
cast.ToString("mayonegg")         // "mayonegg"
cast.ToString(8)                  // "8"
cast.ToString(8.31)               // "8.31"
cast.ToString([]byte("one time")) // "one time"
cast.ToString(nil)                // ""

var foo interface{} = "one more time"
cast.ToString(foo)                // "one more time"
Example ‘ToInt’:
cast.ToInt(8)                  // 8
cast.ToInt(8.31)               // 8
cast.ToInt("8")                // 8
cast.ToInt(true)               // 1
cast.ToInt(false)              // 0

var eight interface{} = 8
cast.ToInt(eight)              // 8
cast.ToInt(nil)                // 0

License

The project is licensed under the MIT License.

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Must added in v1.9.0

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 added in v1.4.0

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 added in v1.9.0

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 added in v1.1.0

func ToDurationSlice(i any) []time.Duration

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

func ToDurationSliceE added in v1.1.0

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

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

func ToE added in v1.9.0

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 added in v1.9.0

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 added in v1.8.0

func ToFloat64Slice(i any) []float64

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

func ToFloat64SliceE added in v1.8.0

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 added in v1.9.0

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 added in v1.9.0

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 added in v1.8.0

func ToInt64Slice(i any) []int64

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

func ToInt64SliceE added in v1.8.0

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 added in v1.9.0

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 added in v1.9.0

func ToNumber[T Number](i any) T

ToNumber casts any value to a Number type.

func ToNumberE added in v1.9.0

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 added in v1.3.0

func ToStringMapInt(i any) map[string]int

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

func ToStringMapInt64 added in v1.3.0

func ToStringMapInt64(i any) map[string]int64

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

func ToStringMapInt64E added in v1.3.0

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

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

func ToStringMapIntE added in v1.3.0

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 added in v1.4.0

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

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

func ToTimeInDefaultLocationE added in v1.4.0

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 added in v1.9.0

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 added in v1.9.0

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 added in v1.9.0

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 added in v1.9.0

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 added in v1.8.0

func ToUintSlice(i any) []uint

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

func ToUintSliceE added in v1.8.0

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

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

Types

type Basic added in v1.9.0

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 added in v1.9.0

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.

Directories

Path Synopsis
generator module