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, error)
- func MapKeys[K comparable, V any, M ~map[K]V](m M) []K
- func MapKeysVals[K comparable, V any, M ~map[K]V](m M) ([]K, []V)
- func MapToSlice(a any) (ks any, vs any)
- func MapToSliceE(a any) (ks any, 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 Struct2Map(a any) map[string]any
- func Struct2MapStr(obj 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 ToBoolSlice(a any) []bool
- func ToBoolSliceE(a any) ([]bool, error)
- func ToByteSlice(a any) []byte
- func ToByteSliceE(a any) ([]byte, error)
- func ToDurationE(i any) (time.Duration, error)
- func ToDurationSlice(a any) []time.Duration
- func ToDurationSliceE(a any) ([]time.Duration, error)
- func ToFloat32E(i any) (float32, error)
- func ToFloat64E(i any) (float64, error)
- func ToInt16E(i any) (int16, error)
- func ToInt16Slice(a any) []int16
- func ToInt16SliceE(a any) ([]int16, error)
- func ToInt32E(i any) (int32, error)
- func ToInt32Slice(a any) []int32
- func ToInt32SliceE(a any) ([]int32, error)
- func ToInt64E(i any) (int64, error)
- func ToInt64Slice(a any) []int64
- func ToInt64SliceE(a any) ([]int64, error)
- func ToInt8E(i any) (int8, error)
- func ToInt8Slice(a any) []int8
- func ToInt8SliceE(a any) ([]int8, error)
- func ToIntE(i any) (int, error)
- func ToIntSlice(a any) []int
- func ToIntSliceE(a 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 ToStrSlice(a any) []string
- func ToStrSliceE(a any) ([]string, error)
- func ToStringE(i any) (string, error)
- func ToUint16E(i any) (uint16, error)
- func ToUint16Slice(a any) []uint16
- func ToUint16SliceE(a any) ([]uint16, error)
- func ToUint32E(i any) (uint32, error)
- func ToUint32Slice(a any) []uint32
- func ToUint32SliceE(a any) ([]uint32, error)
- func ToUint64E(i any) (uint64, error)
- func ToUint64Slice(a any) []uint64
- func ToUint64SliceE(a any) ([]uint64, error)
- func ToUint8E(i any) (uint8, error)
- func ToUint8Slice(a any) []uint8
- func ToUint8SliceE(a any) ([]uint8, error)
- func ToUintE(i any) (uint, error)
- func ToUintSlice(a any) []uint
- func ToUintSliceE(a any) ([]uint, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func JSONToSlice ¶
JSONToSliceE converts the JSON-encoded data to any type slice with no error returned.
func JSONToSliceE ¶
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 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. You can also use standard library golang.org/x/exp/maps#Keys.
func MapKeysVals ¶
func MapKeysVals[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 MapToSlice ¶
MapToSlice converts map keys and values to slice in indeterminate order.
func MapToSliceE ¶
MapToSliceE converts keys and values of map to slice in indeterminate order with error.
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. You can also 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 Struct2Map ¶
Struct2Map 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 Struct2MapStr ¶
Struct2MapStr 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 ToBoolSliceE ¶
ToBoolSliceE converts any type slice or array to []bool with returned error.
func ToByteSlice ¶
ToByteSlice converts an type slice or array to []byte. E.g. covert []string{"1", "2", "3"} to []byte{1, 2, 3}.
func ToByteSliceE ¶
ToByteSliceE converts any type slice or array to []byte with returned error.
func ToDurationE ¶
ToDurationE casts any type to time.Duration type.
func ToDurationSlice ¶
ToDurationSlice converts any type slice or array to []time.Duration.
func ToDurationSliceE ¶
ToDurationSliceE converts any type to []time.Duration with returned error.
func ToFloat32E ¶
ToFloat32E casts any type to a float32 type.
func ToFloat64E ¶
ToFloat64E casts any type to a float64 type.
func ToInt16Slice ¶
ToInt16Slice converts any type slice or array to []int16. For example, covert []string{"1", "2", "3"} to []int16{1, 2, 3}.
func ToInt16SliceE ¶
ToInt16SliceE converts any type slice or array to []int16 with returned error.
func ToInt32Slice ¶
ToInt32Slice converts any type slice or array to []int32. For example, covert []string{"1", "2", "3"} to []int32{1, 2, 3}.
func ToInt32SliceE ¶
ToInt32SliceE converts any type slice or array []int32 with returned error.
func ToInt64Slice ¶
ToInt64Slice converts any type slice or array to []int64 slice. For example, covert []string{"1", "2", "3"} to []int64{1, 2, 3}.
func ToInt64SliceE ¶
ToInt64SliceE converts any type slice or array to []int64 slice with returned error.
func ToInt8Slice ¶
ToInt8Slice converts any type slice or array to []int8. E.g. covert []string{"1", "2", "3"} to []int8{1, 2, 3}.
func ToInt8SliceE ¶
ToInt8SliceE converts any type slice or array to []int8 with returned error.
func ToIntSlice ¶
ToIntSlice converts any type slice or array to []int. E.g. covert []string{"1", "2", "3"} to []int{1, 2, 3}.
func ToIntSliceE ¶
ToIntSliceE converts any type slice or array to []int with returned error..
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}).
func ToSliceE ¶
ToSliceE converts any type slice or array to the specified type slice. An error will be returned if an error occurred.
func ToStrSlice ¶
ToStrSlice converts any type slice or array to []string. For example, covert []int{1, 2, 3} to []string{"1", "2", "3"}.
func ToStrSliceE ¶
ToStrSliceE converts any type slice or array to []string with returned error.
func ToUint16Slice ¶
ToUint16Slice converts any type slice or array to []uint16. For example, covert []string{"1", "2", "3"} to []uint16{1, 2, 3}.
func ToUint16SliceE ¶
ToUint16SliceE converts any type slice or array to []uint16 slice with returned error.
func ToUint32Slice ¶
ToUint32Slice converts any type slice or array to []uint32. For example, covert []string{"1", "2", "3"} to []uint32{1, 2, 3}.
func ToUint32SliceE ¶
ToUint32SliceE converts any type slice or array to []uint32 slice with returned error.
func ToUint64Slice ¶
ToUint64Slice converts any type slice or array to []uint64. For example, covert []string{"1", "2", "3"} to []uint64{1, 2, 3}.
func ToUint64SliceE ¶
ToUint64SliceE converts any type slice or array to []uint64 with returned error.
func ToUint8Slice ¶
ToUint8Slice converts any type slice or array to []uint8. E.g. covert []string{"1", "2", "3"} to []uint8{1, 2, 3}.
func ToUint8SliceE ¶
ToUint8SliceE converts any type slice or array to []uint8 slice with returned error.
func ToUintSlice ¶
ToUintSlice converts any type slice or array to []uint. For example, covert []string{"1", "2", "3"} to []uint{1, 2, 3}.
func ToUintSliceE ¶
ToUintSliceE converts any type slice or array to []uint with returned error.
Types ¶
This section is empty.