cast

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

README

Go Type Cast Build Status GoDoc License

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

Installation

$ go get -u github.com/xgfone/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)

// 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

Documentation

Overview

Package cast provides some functions to convert any value between the different types.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ToBoolHook     func(src interface{}) (dst bool, err error)
	ToInt64Hook    func(src interface{}) (dst int64, err error)
	ToUint64Hook   func(src interface{}) (dst uint64, err error)
	ToFloat64Hook  func(src interface{}) (dst float64, err error)
	ToStringHook   func(src interface{}) (dst string, err error)
	ToDurationHook func(src interface{}) (dst time.Duration, err error)
	ToTimeHook     func(src interface{}, loc *time.Location, layouts ...string) (dst time.Time, err error)
)

Define some hook functions to intercept the ToXXX conversion.

Functions

func Must added in v0.6.0

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

Must checks whether err is not nil. If not nil, panic with it. Or, return value.

Example
Must(ToBool("true"))
Must(ToInt64("123"))
Must(ToString(1234))
Output:

func MustParseTime added in v0.6.0

func MustParseTime(value string, loc *time.Location, layouts ...string) time.Time

MustParseTime is the same as TryParseTime, but panics if there is an error.

Example
MustParseTime("2009-02-13T23:31:30Z", nil)
MustParseTime("2009-02-13 23:31:30", time.UTC)
Output:

func MustToTimeInLocation added in v0.6.0

func MustToTimeInLocation(any interface{}, loc *time.Location, layouts ...string) time.Time

MustToTimeInLocation is the same as ToTimeInLocation, but panics if there is an error.

Example
MustToTimeInLocation("2009-02-13T23:31:30Z", nil)
MustToTimeInLocation(1234567890, nil)
Output:

func Set added in v0.6.0

func Set(dst, src interface{}) (err error)

Set does the best, using the ToXXX function, to set the value of dst to src.

Support the types as follow:

  • *bool
  • *int
  • *int8
  • *int16
  • *int32
  • *int64
  • *uint
  • *uint8
  • *uint16
  • *uint32
  • *uint64
  • *uintptr
  • *string
  • *float32
  • *float64
  • *time.Time
  • *time.Duration
  • reflect.Value
  • interface sql.Scanner
  • interface { Set(interface{}) error }

func ToBool

func ToBool(any interface{}) (dst bool, err error)

ToBool converts any to a bool value.

Example
fmt.Println(ToBool(nil))
fmt.Println(ToBool(false))
fmt.Println(ToBool(true))
fmt.Println(ToBool(""))
fmt.Println(ToBool("0"))
fmt.Println(ToBool("1"))
fmt.Println(ToBool("false"))
fmt.Println(ToBool("true"))
fmt.Println(ToBool([]byte{'\x00'})) // => byte(0)     => false
fmt.Println(ToBool([]byte{'\x01'})) // => byte(1)     => true
fmt.Println(ToBool([]byte{'\x31'})) // => string("1") => true
fmt.Println(ToBool(0))
fmt.Println(ToBool(1))
fmt.Println(ToBool(2))
fmt.Println(ToBool(String("f"))) // Use the method String()
fmt.Println(ToBool(String("t"))) // Use the method String()
Output:

false <nil>
false <nil>
true <nil>
false <nil>
false <nil>
true <nil>
false <nil>
true <nil>
false <nil>
true <nil>
true <nil>
false <nil>
true <nil>
true <nil>
false <nil>
true <nil>

func ToDuration

func ToDuration(any interface{}) (dst time.Duration, err error)

ToDuration converts any to a time.Duration value.

Example
fmt.Println(ToDuration(nil))
fmt.Println(ToDuration(""))     // Parse string as time.Millisecond
fmt.Println(ToDuration("1000")) // Parse string as time.Millisecond
fmt.Println(ToDuration("2s"))   // Use time.ParseDuration
fmt.Println(ToDuration([]byte("3000")))
fmt.Println(ToDuration([]byte("4s")))
fmt.Println(ToDuration(5000.0)) // Use float as time.Millisecond
fmt.Println(ToDuration(6000))   // Use integer as time.Millisecond
fmt.Println(ToDuration(time.Second))
fmt.Println(ToDuration(String("7000"))) // Use the method String() to be parsed as time.Millisecond
fmt.Println(ToDuration(String("8s")))   // Use the method String() to be parsed by time.ParseDurationParseDuration
Output:

0s <nil>
0s <nil>
1s <nil>
2s <nil>
3s <nil>
4s <nil>
5s <nil>
6s <nil>
1s <nil>
7s <nil>
8s <nil>

func ToFloat64

func ToFloat64(any interface{}) (dst float64, err error)

ToFloat64 converts any to a float64 value.

Example
fmt.Println(ToFloat64(nil))
fmt.Println(ToFloat64(false))
fmt.Println(ToFloat64(true))
fmt.Println(ToFloat64(""))
fmt.Println(ToFloat64("123"))
fmt.Println(ToFloat64([]byte("456")))
fmt.Println(ToFloat64(789.0))
fmt.Println(ToFloat64(100))
fmt.Println(ToFloat64(String("200.0"))) // Use the method String()
// fmt.Println(ToFloat64(time.Second))              // unsupported
// fmt.Println(ToFloat64(time.Unix(1234567890, 0))) // unsupported
Output:

0 <nil>
0 <nil>
1 <nil>
0 <nil>
123 <nil>
456 <nil>
789 <nil>
100 <nil>
200 <nil>

func ToInt64

func ToInt64(any interface{}) (dst int64, err error)

ToInt64 converts any to a int64 value.

Example
fmt.Println(ToInt64(nil))
fmt.Println(ToInt64(false))
fmt.Println(ToInt64(true))
fmt.Println(ToInt64(""))
fmt.Println(ToInt64("123"))
fmt.Println(ToInt64([]byte("456")))
fmt.Println(ToInt64(789.0))
fmt.Println(ToInt64(100))
fmt.Println(ToInt64(time.Second)) // => int64(ms)
fmt.Println(ToInt64(time.Unix(1234567890, 0)))
fmt.Println(ToInt64(String("123456789"))) // Use the method String()
Output:

0 <nil>
0 <nil>
1 <nil>
0 <nil>
123 <nil>
456 <nil>
789 <nil>
100 <nil>
1000 <nil>
1234567890 <nil>
123456789 <nil>

func ToString

func ToString(any interface{}) (dst string, err error)

ToString converts any to a string value.

Example
fmt.Println(ToString(nil))
fmt.Println(ToString(false))
fmt.Println(ToString(true))
fmt.Println(ToString("123"))
fmt.Println(ToString([]byte("456")))
fmt.Println(ToString(789.0))
fmt.Println(ToString(100))
fmt.Println(ToString(String("123456789")))                   // Use the method String()
fmt.Println(ToString(time.Second))                           // Use the method String()
fmt.Println(ToString(time.Unix(1234567890, 0).In(time.UTC))) // Use time.RFC3339Nano
Output:

 <nil>
false <nil>
true <nil>
123 <nil>
456 <nil>
789 <nil>
100 <nil>
123456789 <nil>
1s <nil>
2009-02-13T23:31:30Z <nil>

func ToTime

func ToTime(any interface{}) (dst time.Time, err error)

ToTime is a convenient function, which is equal to ToTimeInLocation(any, nil).

Example
fmt.Println(ToTime(nil))
fmt.Println(ToTime(""))                                   // Use the method String() to be parsed by time.Parse
fmt.Println(ToTime("0000-00-00 00:00:00"))                // Use the method String() to be parsed by time.Parse
fmt.Println(ToTime("0000-00-00 00:00:00.000"))            // Use the method String() to be parsed by time.Parse
fmt.Println(ToTime("0000-00-00 00:00:00.000000"))         // Use the method String() to be parsed by time.Parse
fmt.Println(ToTime("2009-02-13 23:31:30"))                // Use the method String() to be parsed by time.Parse
fmt.Println(ToTime("2009-02-13T23:31:30Z"))               // Use the method String() to be parsed by time.Parse
fmt.Println(ToTime([]byte{}))                             // Use the method String() to be parsed by time.Parse
fmt.Println(ToTime([]byte("0000-00-00 00:00:00")))        // Use the method String() to be parsed by time.Parse
fmt.Println(ToTime([]byte("0000-00-00 00:00:00.000")))    // Use the method String() to be parsed by time.Parse
fmt.Println(ToTime([]byte("0000-00-00 00:00:00.000000"))) // Use the method String() to be parsed by time.Parse
fmt.Println(ToTime([]byte("2009-02-13 23:31:30")))        // Use the method String() to be parsed by time.Parse
fmt.Println(ToTime([]byte("2009-02-13T23:31:30Z")))       // Use the method String() to be parsed by time.Parse
fmt.Println(ToTime(String("2009-02-13T23:31:30Z")))       // Use the method String() to be parsed by time.Parse
fmt.Println(ToTime(time.Unix(1234567890, 0)))
fmt.Println(ToTime(1234567890.0)) // Use float as the unix timestamp
fmt.Println(ToTime(1234567890))   // Use float as the unix timestamp
Output:

0001-01-01 00:00:00 +0000 UTC <nil>
0001-01-01 00:00:00 +0000 UTC <nil>
0001-01-01 00:00:00 +0000 UTC <nil>
0001-01-01 00:00:00 +0000 UTC <nil>
0001-01-01 00:00:00 +0000 UTC <nil>
2009-02-13 23:31:30 +0000 UTC <nil>
2009-02-13 23:31:30 +0000 UTC <nil>
0001-01-01 00:00:00 +0000 UTC <nil>
0001-01-01 00:00:00 +0000 UTC <nil>
0001-01-01 00:00:00 +0000 UTC <nil>
0001-01-01 00:00:00 +0000 UTC <nil>
2009-02-13 23:31:30 +0000 UTC <nil>
2009-02-13 23:31:30 +0000 UTC <nil>
2009-02-13 23:31:30 +0000 UTC <nil>
2009-02-13 23:31:30 +0000 UTC <nil>
2009-02-13 23:31:30 +0000 UTC <nil>
2009-02-13 23:31:30 +0000 UTC <nil>

func ToTimeInLocation added in v0.3.0

func ToTimeInLocation(any interface{}, loc *time.Location, layouts ...string) (dst time.Time, err error)

ToTimeInLocation converts any to a time.Time value.

If loc is nil, use defaults.TimeLocation instead. If any is a string-like, use TryParseTime to parse it with layouts.

func ToUint64

func ToUint64(any interface{}) (dst uint64, err error)

ToUint64 converts any to a uint64 value.

Example
fmt.Println(ToUint64(nil))
fmt.Println(ToUint64(false))
fmt.Println(ToUint64(true))
fmt.Println(ToUint64(""))
fmt.Println(ToUint64("123"))
fmt.Println(ToUint64([]byte("456")))
fmt.Println(ToUint64(789.0))
fmt.Println(ToUint64(100))
fmt.Println(ToUint64(String("123456789"))) // Use the method String()
// fmt.Println(ToUint64(time.Second))              // unsupported
// fmt.Println(ToUint64(time.Unix(1234567890, 0))) // unsupported
Output:

0 <nil>
0 <nil>
1 <nil>
0 <nil>
123 <nil>
456 <nil>
789 <nil>
100 <nil>
123456789 <nil>

func TryParseTime added in v0.6.0

func TryParseTime(value string, loc *time.Location, layouts ...string) (time.Time, error)

TryParseTime tries to parse the string value with the layouts in turn to time.Time.

If loc is nil, use defaults.TimeLocation instead. If layouts is empty, use defaults.TimeFormats instead. If value is a integer string, it will be parsee as the unix timestamp.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL