https://github.com/xgfone/go-cast
Supply some functions to convert the data between types, such as ToXXX and MustToXXX.
https://github.com/xgfone/go-cast
cast casting go go-type golang type type-casting typecast
Last synced: 4 months ago
JSON representation
Supply some functions to convert the data between types, such as ToXXX and MustToXXX.
- Host: GitHub
- URL: https://github.com/xgfone/go-cast
- Owner: xgfone
- License: apache-2.0
- Created: 2020-05-12T12:18:31.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-05-12T14:19:13.000Z (over 1 year ago)
- Last Synced: 2024-10-30T20:49:04.690Z (12 months ago)
- Topics: cast, casting, go, go-type, golang, type, type-casting, typecast
- Language: Go
- Homepage:
- Size: 73.2 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go Type Cast [](https://github.com/xgfone/go-cast/actions/workflows/go.yml) [](https://pkg.go.dev/github.com/xgfone/go-cast) [](https://raw.githubusercontent.com/xgfone/go-cast/master/LICENSE)
Provide some functions, supporting `Go1.21+`, to convert the value between different types, such as `ToXXX`.
## Installation
```shell
$ go get -u github.com/xgfone/go-cast
```
## API
#### Convert Function
```go
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
```