Categorygithub.com/spf13/cast
modulepackage
1.7.1
Repository: https://github.com/spf13/cast.git
Documentation: pkg.go.dev

# README

cast

GitHub Workflow Status PkgGoDev Go Version Go Report Card

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

# Functions

StringToDate attempts to parse a string into a time.Time type using a predefined list of formats.
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.
ToBool casts an interface to a bool type.
ToBoolE casts an interface to a bool type.
ToBoolSlice casts an interface to a []bool type.
ToBoolSliceE casts an interface to a []bool type.
ToDuration casts an interface to a time.Duration type.
ToDurationE casts an interface to a time.Duration type.
ToDurationSlice casts an interface to a []time.Duration type.
ToDurationSliceE casts an interface to a []time.Duration type.
ToFloat32 casts an interface to a float32 type.
ToFloat32E casts an interface to a float32 type.
ToFloat64 casts an interface to a float64 type.
ToFloat64E casts an interface to a float64 type.
ToInt casts an interface to an int type.
ToInt16 casts an interface to an int16 type.
ToInt16E casts an interface to an int16 type.
ToInt32 casts an interface to an int32 type.
ToInt32E casts an interface to an int32 type.
ToInt64 casts an interface to an int64 type.
ToInt64E casts an interface to an int64 type.
ToInt8 casts an interface to an int8 type.
ToInt8E casts an interface to an int8 type.
ToIntE casts an interface to an int type.
ToIntSlice casts an interface to a []int type.
ToIntSliceE casts an interface to a []int type.
ToSlice casts an interface to a []interface{} type.
ToSliceE casts an interface to a []interface{} type.
ToString casts an interface to a string type.
ToStringE casts an interface to a string type.
ToStringMap casts an interface to a map[string]interface{} type.
ToStringMapBool casts an interface to a map[string]bool type.
ToStringMapBoolE casts an interface to a map[string]bool type.
ToStringMapE casts an interface to a map[string]interface{} type.
ToStringMapInt casts an interface to a map[string]int type.
ToStringMapInt64 casts an interface to a map[string]int64 type.
ToStringMapInt64E casts an interface to a map[string]int64{} type.
ToStringMapIntE casts an interface to a map[string]int{} type.
ToStringMapString casts an interface to a map[string]string type.
ToStringMapStringE casts an interface to a map[string]string type.
ToStringMapStringSlice casts an interface to a map[string][]string type.
ToStringMapStringSliceE casts an interface to a map[string][]string type.
ToStringSlice casts an interface to a []string type.
ToStringSliceE casts an interface to a []string type.
ToTime casts an interface to a time.Time type.
ToTimeE casts an interface to a time.Time type.
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.
ToUint casts an interface to a uint type.
ToUint16 casts an interface to a uint16 type.
ToUint16E casts an interface to a uint16 type.
ToUint32 casts an interface to a uint32 type.
ToUint32E casts an interface to a uint32 type.
ToUint64 casts an interface to a uint64 type.
ToUint64E casts an interface to a uint64 type.
ToUint8 casts an interface to a uint8 type.
ToUint8E casts an interface to a uint type.
ToUintE casts an interface to a uint type.