Skip to content
/ go-cast Public

Supply some functions to convert the data between types, such as ToXXX and MustToXXX.

License

Notifications You must be signed in to change notification settings

xgfone/go-cast

Repository files navigation

Go Type Cast Build Status GoDoc License

Provide some functions, supporting Go1.21+, to convert the value between different types, such as ToXXX.

Installation

$ go get -u github.com/xgfone/go-cast

API

Convert Function

func ToTime(any interface{}) (dst time.Time, err error)
func ToBool(any interface{}) (dst bool, err error)
func ToInt64(any interface{}) (dst int64, err error)
func ToUint64(any interface{}) (dst uint64, err error)
func ToFloat64(any interface{}) (dst float64, err error)
func ToString(any interface{}) (dst string, err error)
func ToDuration(any interface{}) (dst time.Duration, err error)

func ToTimeInLocation(any interface{}, loc *time.Location, layouts ...string) (time.Time, error)
func MustToTimeInLocation(any interface{}, loc *time.Location, layouts ...string) time.Time
func MustParseTime(value string, loc *time.Location, layouts ...string) time.Time
func TryParseTime(value string, loc *time.Location, layouts ...string) (time.Time, error)
func Set(dst, src interface{}) (err error)

// Must is the generic function and used by associating with ToXXX. For example,
//   Must(ToBool(any))
//   Must(ToInt64(any))
//   Must(ToUint64(any))
//   Must(ToFloat64(any))
//   Must(ToString(any))
//   Must(ToDuration(any))
//   Must(ToTime(any))
func Must[T any](value T, err error) T