Documentation
¶
Overview ¶
Package conv provides utility functions related to type conversions, such as to int using ToAny[int](any), to int set using ToSet[int](any), to a slice or array to set type (map[bool]struct{}), map keys and values to slice in random order etc.
Index ¶
- func JsonToSlice[S ~[]E, E any](data []byte) S
- func JsonToSliceE[S ~[]E, E any](data []byte) (s S, err error)
- func MapKeyVals[K comparable, V any, M ~map[K]V](m M) ([]K, []V)
- func MapKeys[K comparable, V any, M ~map[K]V](m M) []K
- func MapToSlice(a any) (ks, vs any)
- func MapToSliceE(a any) (ks, vs any, err error)
- func MapVals[K comparable, V any, M ~map[K]V](m M) []V
- func SplitStrToSet(s string, sep string) map[string]struct{}
- func SplitStrToSlice[T any](s, sep string) []T
- func SplitStrToSliceE[T any](s, sep string) ([]T, error)
- func StructToMap(a any) map[string]any
- func StructToMapStr(a any) map[string]string
- func ToAny[T any](a any) T
- func ToAnyE[T any](a any) (T, error)
- func ToBoolE(a any) (bool, error)
- func ToDurationE(i any) (time.Duration, error)
- func ToFloat32E(i any) (float32, error)
- func ToFloat64E(i any) (float64, error)
- func ToInt16E(i any) (int16, error)
- func ToInt32E(i any) (int32, error)
- func ToInt64E(i any) (int64, error)
- func ToInt8E(i any) (int8, error)
- func ToIntE(i any) (int, error)
- func ToMapStr(a any) map[string]string
- func ToMapStrE(a any) (map[string]string, error)
- func ToSet[T comparable](a any) map[T]struct{}
- func ToSetE[T comparable](a any) (map[T]struct{}, error)
- func ToSlice[T any](a any) []T
- func ToSliceE[T any](a any) ([]T, error)
- func ToStringE(i any) (string, error)
- func ToUint16E(i any) (uint16, error)
- func ToUint32E(i any) (uint32, error)
- func ToUint64E(i any) (uint64, error)
- func ToUint8E(i any) (uint8, error)
- func ToUintE(i any) (uint, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func JsonToSlice ¶ added in v0.0.52
JsonToSlice converts the JSON-encoded data to any type slice with no error returned.
func JsonToSliceE ¶ added in v0.0.52
JsonToSliceE converts the JSON-encoded data to any type slice. E.g. a JSON value ["foo", "bar", "baz"] can be converted to []string{"foo", "bar", "baz"} when calling JsonToSliceE[[]string](`["foo", "bar", "baz"]`).
func MapKeyVals ¶ added in v0.0.52
func MapKeyVals[K comparable, V any, M ~map[K]V](m M) ([]K, []V)
MapKeyVals returns two slice of all the keys and values in m. The keys and values are returned in an indeterminate order.
func MapKeys ¶
func MapKeys[K comparable, V any, M ~map[K]V](m M) []K
MapKeys returns a slice of all the keys in m. The keys returned are in indeterminate order. Deprecated: As of Go 1.18, please use standard library golang.org/x/exp/maps#Keys.
func MapToSlice ¶
MapToSlice converts map keys and values to slice in indeterminate order. Deprecated: As of Go 1.18, please use standard library golang.org/x/exp/maps#Keys and Values.
func MapToSliceE ¶
MapToSliceE converts keys and values of map to slice in indeterminate order with error. Deprecated: As of Go 1.18, please use standard library golang.org/x/exp/maps#Keys and Values.
func MapVals ¶
func MapVals[K comparable, V any, M ~map[K]V](m M) []V
MapVals returns a slice of all the values in m. The values returned are in indeterminate order. Deprecated: As of Go 1.18, please use standard library golang.org/x/exp/maps#Values.
func SplitStrToSet ¶
SplitStrToSet convert a string to map set after split
func SplitStrToSlice ¶
SplitStrToSlice splits a string to a slice by the specified separator.
func SplitStrToSliceE ¶
SplitStrToSliceE splits a string to a slice by the specified separator and returns an error if occurred. Note that this function is implemented through 1.18 generics, so the element type needs to be specified when calling it, e.g. SplitStrToSliceE[int]("1,2,3", ",").
func StructToMap ¶ added in v0.0.52
StructToMap converts struct to map[string]any. Such as struct{I int, S string}{I: 1, S: "a"} to map["I":1 "S":"a"]. Note that unexported fields of struct can't be converted.
func StructToMapStr ¶ added in v0.0.52
StructToMapStr converts struct to map[string]string. Such as struct{I int, S string}{I: 1, S: "a"} to map["I":"1" "S":"a"]. Note that unexported fields of struct can't be converted.
func ToDurationE ¶
ToDurationE casts any type to time.Duration type.
func ToFloat32E ¶
ToFloat32E casts any type to a float32 type.
func ToFloat64E ¶
ToFloat64E casts any type to a float64 type.
func ToSet ¶
func ToSet[T comparable](a any) map[T]struct{}
ToSet converts a slice or array to map[T]struct{}. An error will be returned if an error occurred.
func ToSetE ¶
func ToSetE[T comparable](a any) (map[T]struct{}, error)
ToSetE converts a slice or array to map[T]struct{} and returns an error if occurred. Note that the the element type of input don't need to be equal to the map key type. For example, []uint64{1, 2, 3} can be converted to map[uint64]struct{}{1:{},2:{},3:{}} and also can be converted to map[string]struct{}{"1":{},"2":{},"3":{}} if you want. Note that this function is implemented through 1.18 generics, so the element type needs to be specified when calling it, e.g. ToSetE[int]([]int{1,2,3}).
Types ¶
This section is empty.