Documentation
¶
Overview ¶
Package gonv provides type conversion utilities for Go applications. It offers safe and flexible casting between different data types with generic support.
Package gonv provides type conversion utilities for Go applications. It offers safe and flexible casting between different data types with generic support. This file contains functions for converting values to slice types.
Package gonv provides type conversion utilities for Go applications. It offers safe and flexible casting between different data types with generic support. This file contains functions for converting values to time.Time types.
Package gonv provides utility functions for the gonv type conversion library. This file contains helper functions used internally by other conversion functions.
Index ¶
- Variables
- func AnySlice(o any) []any
- func AnySliceE(o any) ([]any, error)
- func Bool[E ~bool](o any) E
- func BoolE[E ~bool](o any) (E, error)
- func BoolS[S ~[]E, E ~bool](o any) S
- func BoolSE[S ~[]E, E ~bool](o any) (S, error)
- func BoolSlice[S ~[]E, E ~bool](o any) S
- func BoolSliceE[S ~[]E, E ~bool](o any) (S, error)
- func BytesToString(b []byte) string
- func Duration(o any) time.Duration
- func DurationE(o any) (time.Duration, error)
- func DurationS(o any) []time.Duration
- func DurationSE(o any) ([]time.Duration, error)
- func Float[E constraints.Float](o any) E
- func FloatE[E constraints.Float](o any) (E, error)
- func FloatS[S ~[]E, E constraints.Float](o any) S
- func FloatSE[S ~[]E, E constraints.Float](o any) (S, error)
- func FloatSlice[S ~[]E, E constraints.Float](o any) S
- func FloatSliceE[S ~[]E, E constraints.Float](o any) (S, error)
- func Int[E constraints.Signed](o any) E
- func IntE[E constraints.Signed](o any) (E, error)
- func IntS[S ~[]E, E constraints.Signed](o any) S
- func IntSE[S ~[]E, E constraints.Signed](o any) (S, error)
- func IntSlice[S ~[]E, E constraints.Signed](o any) S
- func IntSliceE[S ~[]E, E constraints.Signed](o any) (S, error)
- func MapE[M ~map[K]V, K comparable, V any](o any, key func(o any) (K, error), val func(o any) (V, error)) (M, error)
- func Slice[S ~[]E, E any](o any, to func(o any) (E, error)) (S, error)
- func SliceE[S ~[]E, E any](o any, to func(o any) (E, error)) (S, error)
- func String[E ~string](o any) E
- func StringAnyMap[K ~string](o any) map[K]any
- func StringAnyMapE[K ~string](o any) (map[K]any, error)
- func StringBoolMap[K ~string, V ~bool](o any) map[K]V
- func StringBoolMapE[K ~string, V ~bool](o any) (map[K]V, error)
- func StringE[E ~string](o any) (E, error)
- func StringFloatMap[K ~string, V constraints.Float](o any) map[K]V
- func StringFloatMapE[K ~string, V constraints.Float](o any) (map[K]V, error)
- func StringIntMap[K ~string, V constraints.Signed](o any) map[K]V
- func StringIntMapE[K ~string, V constraints.Signed](o any) (map[K]V, error)
- func StringS[S ~[]E, E ~string](o any) S
- func StringSE[S ~[]E, E ~string](o any) (S, error)
- func StringSlice[S ~[]E, E ~string](o any) S
- func StringSliceE[S ~[]E, E ~string](o any) (S, error)
- func StringStringMap[K ~string, V ~string](o any) map[K]V
- func StringStringMapE[K ~string, V ~string](o any) (map[K]V, error)
- func StringStringSliceMap(o any) map[string][]string
- func StringStringSliceMapE(o any) (map[string][]string, error)
- func StringToBytes(s string) []byte
- func StringUintMap[K ~string, V constraints.Unsigned](o any) map[K]V
- func StringUintMapE[K ~string, V constraints.Unsigned](o any) (map[K]V, error)
- func Time(o any) time.Time
- func TimeE(o any) (time.Time, error)
- func TimeInLocation(o any, location *time.Location) time.Time
- func TimeInLocationE(o any, location *time.Location) (time.Time, error)
- func Uint[E constraints.Unsigned](o any) E
- func UintE[E constraints.Unsigned](o any) (E, error)
- func UintS[S ~[]E, E constraints.Unsigned](o any) S
- func UintSE[S ~[]E, E constraints.Unsigned](o any) (S, error)
- func UintSlice[S ~[]E, E constraints.Unsigned](o any) S
- func UintSliceE[S ~[]E, E constraints.Unsigned](o any) (S, error)
Constants ¶
This section is empty.
Variables ¶
var DefaultTimeFormat = time.RFC3339
DefaultTimeFormat is the default time format used for time string conversions. It uses RFC3339 format (YYYY-MM-DDTHH:MM:SSZ).
var TimeFormats = []string{ time.Layout, time.ANSIC, time.UnixDate, time.RubyDate, time.RFC822, time.RFC822Z, time.RFC850, time.RFC1123, time.RFC1123Z, time.RFC3339, time.RFC3339Nano, time.Kitchen, time.Stamp, time.StampMilli, time.StampMicro, time.StampNano, time.DateTime, time.DateOnly, time.TimeOnly, "2006-01-02 15:04:05Z07:00", "02 Jan 2006", "2006-01-02 15:04:05 -07:00", "2006-01-02 15:04:05 -0700", "2006-01-02T15:04:05", "2006-01-02 15:04:05.999999999 -0700 MST", "2006-01-02T15:04:05-0700", "2006-01-02 15:04:05Z0700", }
TimeFormats is a list of time formats supported for parsing time strings. It includes common formats like RFC822, RFC850, RFC1123, RFC3339, and several custom formats.
Functions ¶
func AnySlice ¶
AnySlice casts an interface to a []any type, ignoring any conversion errors. It returns an empty slice if conversion fails.
Example:
result := AnySlice([]string{"a", "b", "c"}) // returns []interface{}{"a", "b", "c"}
result := AnySlice([3]string{"a", "b", "c"}) // returns []interface{}{"a", "b", "c"}
func AnySliceE ¶
AnySliceE casts an interface to a []any type, returning both the converted slice and any error encountered. This function is useful when you need to handle conversion errors explicitly.
Example:
result, err := AnySliceE([]string{"a", "b", "c"}) // returns []interface{}{"a", "b", "c"}, nil
result, err := AnySliceE("not a slice") // returns nil, error
func Bool ¶
Bool casts an interface to a bool type, ignoring any conversion errors. It returns the zero value of type E if conversion fails.
Example:
result := Bool[bool]("true") // returns true
result := Bool[bool]("false") // returns false
result := Bool[bool](1) // returns true
result := Bool[bool](0) // returns false
func BoolE ¶
BoolE casts an interface to a bool type, returning both the converted value and any error encountered. This function is useful when you need to handle conversion errors explicitly.
Example:
result, err := BoolE[bool]("true") // returns true, nil
result, err := BoolE[bool]("invalid") // returns false, error
func BoolS ¶
BoolS casts an interface to a []bool type, ignoring any conversion errors. It's designed for converting slice-like data structures to boolean slices.
Example:
result := BoolS[[]bool]([]string{"true", "false", "1", "0"}) // returns []bool{true, false, true, false}
func BoolSE ¶
BoolSE casts an interface to a []bool type, returning both the converted slice and any error encountered. This function is useful when you need to handle conversion errors for slice data explicitly.
Example:
result, err := BoolSE[[]bool]([]string{"true", "false"}) // returns []bool{true, false}, nil
result, err := BoolSE[[]bool]([]string{"true", "invalid"}) // returns []bool{true, false}, error
func BoolSlice ¶
BoolSlice casts an interface to a boolean slice type, ignoring any conversion errors. It returns an empty slice if conversion fails. S is a slice type with elements of boolean type. E must be a boolean type.
Example:
result := BoolSlice[[]bool, bool]([]bool{true, false, true}) // returns []bool{true, false, true}
func BoolSliceE ¶
BoolSliceE casts an interface to a boolean slice type, returning both the converted slice and any error encountered. This function is useful when you need to handle conversion errors explicitly. S is a slice type with elements of boolean type. E must be a boolean type.
Example:
result, err := BoolSliceE[[]bool, bool]([]bool{true, false, true}) // returns []bool{true, false, true}, nil
result, err := BoolSliceE[[]bool, bool]("not a slice") // returns nil, error
func BytesToString ¶
BytesToString converts byte slice to string without a memory allocation. For more details, see https://github.com/golang/go/issues/53003#issuecomment-1140276077.
func Duration ¶
Duration casts an interface to a time.Duration type, ignoring any conversion errors. It returns zero duration if conversion fails.
Example:
result := Duration("1h30m") // returns 1 hour 30 minutes
result := Duration(3600) // returns 3600 nanoseconds
result := Duration(int64(3600000000000)) // returns 3600 seconds
func DurationE ¶
DurationE casts an interface to a time.Duration type, returning both the converted value and any error encountered. This function is useful when you need to handle conversion errors explicitly.
Example:
result, err := DurationE("1h30m") // returns 5400000000000, nil
result, err := DurationE("invalid") // returns 0, error
func DurationS ¶
DurationS casts an interface to a []time.Duration type, ignoring any conversion errors. It's designed for converting slice-like data structures to duration slices.
Example:
result := DurationS([]string{"1h", "30m", "45s"}) // returns []time.Duration{3600000000000, 1800000000000, 45000000000}
func DurationSE ¶
DurationSE casts an interface to a []time.Duration type, returning both the converted slice and any error encountered. This function is useful when you need to handle conversion errors for slice data explicitly.
Example:
result, err := DurationSE([]string{"1h", "30m"}) // returns []time.Duration{3600000000000, 1800000000000}, nil
result, err := DurationSE([]string{"1h", "invalid"}) // returns nil, error
func Float ¶
func Float[E constraints.Float](o any) E
Float converts an interface to a floating-point type, ignoring any conversion errors. It returns the zero value of the target type if conversion fails. E must be a floating-point type (float32 or float64).
Example:
result := Float[float64]("3.14") // returns 3.14
result := Float[float32](true) // returns 1.0
result := Float[float64](42) // returns 42.0
func FloatE ¶
func FloatE[E constraints.Float](o any) (E, error)
FloatE converts an interface to a floating-point type, returning both the converted value and any error encountered. This function is useful when you need to handle conversion errors explicitly. E must be a floating-point type (float32 or float64).
Example:
result, err := FloatE[float64]("3.14") // returns 3.14, nil
result, err := FloatE[float64]("invalid") // returns 0.0, error
func FloatS ¶
func FloatS[S ~[]E, E constraints.Float](o any) S
FloatS converts an interface to a floating-point slice type, ignoring any conversion errors. It's designed for converting slice-like data structures to floating-point slices. S is a slice type with elements of floating-point type. E must be a floating-point type (float32 or float64).
Example:
result := FloatS[[]float64]([]string{"1.1", "2.2", "3.3"}) // returns []float64{1.1, 2.2, 3.3}
func FloatSE ¶
func FloatSE[S ~[]E, E constraints.Float](o any) (S, error)
FloatSE converts an interface to a floating-point slice type, returning both the converted slice and any error encountered. This function is useful when you need to handle conversion errors for slice data explicitly. S is a slice type with elements of floating-point type. E must be a floating-point type (float32 or float64).
Example:
result, err := FloatSE[[]float64]([]string{"1.1", "2.2"}) // returns []float64{1.1, 2.2}, nil
result, err := FloatSE[[]float64]([]string{"1.1", "invalid"}) // returns nil, error
func FloatSlice ¶
func FloatSlice[S ~[]E, E constraints.Float](o any) S
FloatSlice casts an interface to a floating-point slice type, ignoring any conversion errors. It returns an empty slice if conversion fails. S is a slice type with elements of floating-point type. E must be a floating-point type (float32 or float64).
Example:
result := FloatSlice[[]float64, float64]([]float64{1.1, 2.2, 3.3}) // returns []float64{1.1, 2.2, 3.3}
func FloatSliceE ¶
func FloatSliceE[S ~[]E, E constraints.Float](o any) (S, error)
FloatSliceE casts an interface to a floating-point slice type, returning both the converted slice and any error encountered. This function is useful when you need to handle conversion errors explicitly. S is a slice type with elements of floating-point type. E must be a floating-point type (float32 or float64).
Example:
result, err := FloatSliceE[[]float64, float64]([]float64{1.1, 2.2, 3.3}) // returns []float64{1.1, 2.2, 3.3}, nil
result, err := FloatSliceE[[]float64, float64]("not a slice") // returns nil, error
func Int ¶
func Int[E constraints.Signed](o any) E
Int converts an interface to a signed integer type, ignoring any conversion errors. It returns the zero value of the target type if conversion fails. E must be a signed integer type (int, int8, int16, int32, int64).
Example:
result := Int[int64]("42") // returns 42
result := Int[int](true) // returns 1
result := Int[int](3.14) // returns 3
func IntE ¶
func IntE[E constraints.Signed](o any) (E, error)
IntE converts an interface to a signed integer type, returning both the converted value and any error encountered. This function is useful when you need to handle conversion errors explicitly. E must be a signed integer type (int, int8, int16, int32, int64).
Example:
result, err := IntE[int64]("42") // returns 42, nil
result, err := IntE[int64]("invalid") // returns 0, error
func IntS ¶
func IntS[S ~[]E, E constraints.Signed](o any) S
IntS converts an interface to a signed integer slice type, ignoring any conversion errors. It's designed for converting slice-like data structures to signed integer slices. S is a slice type with elements of signed integer type. E must be a signed integer type (int, int8, int16, int32, int64).
Example:
result := IntS[[]int64]([]string{"1", "2", "3"}) // returns []int64{1, 2, 3}
func IntSE ¶
func IntSE[S ~[]E, E constraints.Signed](o any) (S, error)
IntSE converts an interface to a signed integer slice type, returning both the converted slice and any error encountered. This function is useful when you need to handle conversion errors for slice data explicitly. S is a slice type with elements of signed integer type. E must be a signed integer type (int, int8, int16, int32, int64).
Example:
result, err := IntSE[[]int64]([]string{"1", "2"}) // returns []int64{1, 2}, nil
result, err := IntSE[[]int64]([]string{"1", "invalid"}) // returns nil, error
func IntSlice ¶
func IntSlice[S ~[]E, E constraints.Signed](o any) S
IntSlice casts an interface to a signed integer slice type, ignoring any conversion errors. It returns an empty slice if conversion fails. S is a slice type with elements of signed integer type. E must be a signed integer type (int, int8, int16, int32, int64).
Example:
result := IntSlice[[]int64, int64]([]int64{1, 2, 3}) // returns []int64{1, 2, 3}
func IntSliceE ¶
func IntSliceE[S ~[]E, E constraints.Signed](o any) (S, error)
IntSliceE casts an interface to a signed integer slice type, returning both the converted slice and any error encountered. This function is useful when you need to handle conversion errors explicitly. S is a slice type with elements of signed integer type. E must be a signed integer type (int, int8, int16, int32, int64).
Example:
result, err := IntSliceE[[]int64, int64]([]int64{1, 2, 3}) // returns []int64{1, 2, 3}, nil
result, err := IntSliceE[[]int64, int64]("not a slice") // returns nil, error
func MapE ¶
func MapE[M ~map[K]V, K comparable, V any](o any, key func(o any) (K, error), val func(o any) (V, error)) (M, error)
MapE is a generic function that casts an interface to a map type, returning both the converted map and any error encountered. M is the target map type, K is the key type, and V is the value type. key is a function that converts keys, and val is a function that converts values.
Example:
result, err := MapE[map[string]int](map[string]string{"key": "42"}, StringE[string], IntE[int])
// returns map[string]int{"key": 42}, nil
func Slice ¶
Slice is a generic function that casts an interface to a slice type, returning both the converted slice and any error encountered. S is a slice type with elements of type E. E is the element type of the slice. to is a function that converts individual elements.
Example:
result, err := Slice[[]int, int]([]string{"1", "2", "3"}, IntE[int])
// returns []int{1, 2, 3}, nil
func SliceE ¶
SliceE is a generic function that casts an interface to a slice type, returning both the converted slice and any error encountered. S is a slice type with elements of type E. E is the element type of the slice. to is a function that converts individual elements.
Example:
result, err := SliceE[[]int, int]([]string{"1", "2", "3"}, IntE[int])
// returns []int{1, 2, 3}, nil
func String ¶
String casts an interface to a string type, ignoring any conversion errors. It returns an empty string if conversion fails. E must be a string type.
Example:
result := String[string](42) // returns "42" result := String[string](true) // returns "true" result := String[string](3.14) // returns "3.14"
func StringAnyMap ¶
StringAnyMap casts an interface to a map[string]any type, ignoring any conversion errors. It returns an empty map if conversion fails. K must be a string type.
Example:
result := StringAnyMap[string](map[string]interface{}{"key": "value"}) // returns map[string]interface{}{"key": "value"}
result := StringAnyMap[string](`{"key": "value"}`) // returns map[string]interface{}{"key": "value"}
func StringAnyMapE ¶
StringAnyMapE casts an interface to a map[string]any type, returning both the converted map and any error encountered. This function is useful when you need to handle conversion errors explicitly. K must be a string type.
Example:
result, err := StringAnyMapE[string](map[string]interface{}{"key": "value"}) // returns map[string]interface{}{"key": "value"}, nil
result, err := StringAnyMapE[string]("invalid") // returns nil, error
func StringBoolMap ¶
StringBoolMap casts an interface to a map[string]bool type, ignoring any conversion errors. It returns an empty map if conversion fails. K must be a string type and V must be a boolean type.
Example:
result := StringBoolMap[string, bool](map[string]bool{"key": true}) // returns map[string]bool{"key": true}
func StringBoolMapE ¶
StringBoolMapE casts an interface to a map[string]bool type, returning both the converted map and any error encountered. This function is useful when you need to handle conversion errors explicitly. K must be a string type and V must be a boolean type.
Example:
result, err := StringBoolMapE[string, bool](map[string]bool{"key": true}) // returns map[string]bool{"key": true}, nil
result, err := StringBoolMapE[string, bool]("invalid") // returns nil, error
func StringE ¶
StringE casts an interface to a string type, returning both the converted string and any error encountered. This function is useful when you need to handle conversion errors explicitly. E must be a string type.
Example:
result, err := StringE[string](42) // returns "42", nil result, err := StringE[string](nil) // returns "", nil
func StringFloatMap ¶
func StringFloatMap[K ~string, V constraints.Float](o any) map[K]V
StringFloatMap casts an interface to a map[string]float type, ignoring any conversion errors. It returns an empty map if conversion fails. K must be a string type and V must be a floating-point type.
Example:
result := StringFloatMap[string, float64](map[string]float64{"key": 3.14}) // returns map[string]float64{"key": 3.14}
func StringFloatMapE ¶
func StringFloatMapE[K ~string, V constraints.Float](o any) (map[K]V, error)
StringFloatMapE casts an interface to a map[string]float type, returning both the converted map and any error encountered. This function is useful when you need to handle conversion errors explicitly. K must be a string type and V must be a floating-point type.
Example:
result, err := StringFloatMapE[string, float64](map[string]float64{"key": 3.14}) // returns map[string]float64{"key": 3.14}, nil
result, err := StringFloatMapE[string, float64]("invalid") // returns nil, error
func StringIntMap ¶
func StringIntMap[K ~string, V constraints.Signed](o any) map[K]V
StringIntMap casts an interface to a map[string]int type, ignoring any conversion errors. It returns an empty map if conversion fails. K must be a string type and V must be a signed integer type.
Example:
result := StringIntMap[string, int64](map[string]int64{"key": 42}) // returns map[string]int64{"key": 42}
func StringIntMapE ¶
func StringIntMapE[K ~string, V constraints.Signed](o any) (map[K]V, error)
StringIntMapE casts an interface to a map[string]int type, returning both the converted map and any error encountered. This function is useful when you need to handle conversion errors explicitly. K must be a string type and V must be a signed integer type.
Example:
result, err := StringIntMapE[string, int64](map[string]int64{"key": 42}) // returns map[string]int64{"key": 42}, nil
result, err := StringIntMapE[string, int64]("invalid") // returns nil, error
func StringS ¶
StringS casts an interface to a []string type, ignoring any conversion errors. It returns an empty slice if conversion fails. S is a slice type with elements of string type. E must be a string type.
Example:
result := StringS[[]string, string]([]int{1, 2, 3}) // returns []string{"1", "2", "3"}
func StringSE ¶
StringSE casts an interface to a []string type, returning both the converted slice and any error encountered. This function is useful when you need to handle conversion errors explicitly. S is a slice type with elements of string type. E must be a string type.
Example:
result, err := StringSE[[]string, string]([]int{1, 2, 3}) // returns []string{"1", "2", "3"}, nil
result, err := StringSE[[]string, string]("not a slice") // returns nil, error
func StringSlice ¶
StringSlice casts an interface to a string slice type, ignoring any conversion errors. It returns an empty slice if conversion fails. S is a slice type with elements of string type. E must be a string type.
Example:
result := StringSlice[[]string, string]([]string{"a", "b", "c"}) // returns []string{"a", "b", "c"}
func StringSliceE ¶
StringSliceE casts an interface to a string slice type, returning both the converted slice and any error encountered. This function is useful when you need to handle conversion errors explicitly. S is a slice type with elements of string type. E must be a string type.
Example:
result, err := StringSliceE[[]string, string]([]string{"a", "b", "c"}) // returns []string{"a", "b", "c"}, nil
result, err := StringSliceE[[]string, string]("not a slice") // returns nil, error
func StringStringMap ¶
StringStringMap casts an interface to a map[string]string type, ignoring any conversion errors. It returns an empty map if conversion fails. K and V must be string types.
Example:
result := StringStringMap[string, string](map[string]string{"key": "value"}) // returns map[string]string{"key": "value"}
func StringStringMapE ¶
StringStringMapE casts an interface to a map[string]string type, returning both the converted map and any error encountered. This function is useful when you need to handle conversion errors explicitly. K and V must be string types.
Example:
result, err := StringStringMapE[string, string](map[string]string{"key": "value"}) // returns map[string]string{"key": "value"}, nil
result, err := StringStringMapE[string, string]("invalid") // returns nil, error
func StringStringSliceMap ¶
StringStringSliceMap casts an interface to a map[string][]string type, ignoring any conversion errors. It returns an empty map if conversion fails.
Example:
result := StringStringSliceMap(map[string][]string{"key": {"value1", "value2"}}) // returns map[string][]string{"key": {"value1", "value2"}}
func StringStringSliceMapE ¶
StringStringSliceMapE casts an interface to a map[string][]string type, returning both the converted map and any error encountered. This function is useful when you need to handle conversion errors explicitly.
Example:
result, err := StringStringSliceMapE(map[string][]string{"key": {"value1", "value2"}}) // returns map[string][]string{"key": {"value1", "value2"}}, nil
result, err := StringStringSliceMapE("invalid") // returns nil, error
func StringToBytes ¶
StringToBytes converts string to byte slice without a memory allocation. For more details, see https://github.com/golang/go/issues/53003#issuecomment-1140276077.
func StringUintMap ¶
func StringUintMap[K ~string, V constraints.Unsigned](o any) map[K]V
StringUintMap casts an interface to a map[string]uint type, ignoring any conversion errors. It returns an empty map if conversion fails. K must be a string type and V must be an unsigned integer type.
Example:
result := StringUintMap[string, uint64](map[string]uint64{"key": 42}) // returns map[string]uint64{"key": 42}
func StringUintMapE ¶
func StringUintMapE[K ~string, V constraints.Unsigned](o any) (map[K]V, error)
StringUintMapE casts an interface to a map[string]uint type, returning both the converted map and any error encountered. This function is useful when you need to handle conversion errors explicitly. K must be a string type and V must be an unsigned integer type.
Example:
result, err := StringUintMapE[string, uint64](map[string]uint64{"key": 42}) // returns map[string]uint64{"key": 42}, nil
result, err := StringUintMapE[string, uint64]("invalid") // returns nil, error
func Time ¶
Time casts an interface to a time.Time type, ignoring any conversion errors. It returns the zero time value if conversion fails. The time is interpreted in UTC location.
Example:
result := Time("2023-01-01T12:00:00Z") // returns time.Time representing the given timestamp
result := Time(1672574400) // returns time.Time representing Unix timestamp
func TimeE ¶
TimeE casts an interface to a time.Time type, returning both the converted time and any error encountered. The time is interpreted in UTC location. This function is useful when you need to handle conversion errors explicitly.
Example:
result, err := TimeE("2023-01-01T12:00:00Z") // returns time.Time and nil
result, err := TimeE("invalid") // returns zero time and error
func TimeInLocation ¶
TimeInLocation casts an empty interface to time.Time, interpreting inputs without a timezone to be in the given location, or the local timezone if nil. It returns the zero time value if conversion fails.
Example:
loc, _ := time.LoadLocation("America/New_York")
result := TimeInLocation("2023-01-01 12:00:00", loc) // returns time.Time in New York timezone
func TimeInLocationE ¶
TimeInLocationE casts an empty interface to time.Time, interpreting inputs without a timezone to be in the given location, or the local timezone if nil. It returns both the converted time and any error encountered. This function is useful when you need to handle conversion errors explicitly.
Example:
loc, _ := time.LoadLocation("America/New_York")
result, err := TimeInLocationE("2023-01-01 12:00:00", loc) // returns time.Time and nil
result, err := TimeInLocationE("invalid", loc) // returns zero time and error
func Uint ¶
func Uint[E constraints.Unsigned](o any) E
Uint converts an interface to an unsigned integer type, ignoring any conversion errors. It returns the zero value of the target type if conversion fails. E must be an unsigned integer type (uint, uint8, uint16, uint32, uint64).
Example:
result := Uint[uint64]("42") // returns 42
result := Uint[uint](true) // returns 1
result := Uint[uint](3.14) // returns 3
func UintE ¶
func UintE[E constraints.Unsigned](o any) (E, error)
UintE converts an interface to an unsigned integer type, returning both the converted value and any error encountered. This function is useful when you need to handle conversion errors explicitly. E must be an unsigned integer type (uint, uint8, uint16, uint32, uint64).
Example:
result, err := UintE[uint64]("42") // returns 42, nil
result, err := UintE[uint64]("-1") // returns 0, error (negative values are not allowed)
func UintS ¶
func UintS[S ~[]E, E constraints.Unsigned](o any) S
UintS converts an interface to an unsigned integer slice type, ignoring any conversion errors. It returns an empty slice if conversion fails. S is a slice type with elements of unsigned integer type. E must be an unsigned integer type (uint, uint8, uint16, uint32, uint64).
Example:
result := UintS[[]uint64, uint64]([]string{"1", "2", "3"}) // returns []uint64{1, 2, 3}
func UintSE ¶
func UintSE[S ~[]E, E constraints.Unsigned](o any) (S, error)
UintSE converts an interface to an unsigned integer slice type, returning both the converted slice and any error encountered. This function is useful when you need to handle conversion errors for slice data explicitly. S is a slice type with elements of unsigned integer type. E must be an unsigned integer type (uint, uint8, uint16, uint32, uint64).
Example:
result, err := UintSE[[]uint64, uint64]([]string{"1", "2"}) // returns []uint64{1, 2}, nil
result, err := UintSE[[]uint64, uint64]([]string{"1", "-1"}) // returns nil, error (negative values are not allowed)
func UintSlice ¶
func UintSlice[S ~[]E, E constraints.Unsigned](o any) S
UintSlice casts an interface to an unsigned integer slice type, ignoring any conversion errors. It returns an empty slice if conversion fails. S is a slice type with elements of unsigned integer type. E must be an unsigned integer type (uint, uint8, uint16, uint32, uint64).
Example:
result := UintSlice[[]uint64, uint64]([]uint64{1, 2, 3}) // returns []uint64{1, 2, 3}
func UintSliceE ¶
func UintSliceE[S ~[]E, E constraints.Unsigned](o any) (S, error)
UintSliceE casts an interface to an unsigned integer slice type, returning both the converted slice and any error encountered. This function is useful when you need to handle conversion errors explicitly. S is a slice type with elements of unsigned integer type. E must be an unsigned integer type (uint, uint8, uint16, uint32, uint64).
Example:
result, err := UintSliceE[[]uint64, uint64]([]uint64{1, 2, 3}) // returns []uint64{1, 2, 3}, nil
result, err := UintSliceE[[]uint64, uint64]("not a slice") // returns nil, error
Types ¶
This section is empty.