gonv

package module
v0.0.0-...-aeefef5 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2025 License: MIT Imports: 14 Imported by: 2

README

gonv - Go Type Conversion Utilities

Package gonv provides comprehensive type conversion utilities for Go applications. It offers safe and flexible casting between different data types with generic support, making it easy to convert values between various types without worrying about runtime panics.

Features

  • Generic type conversion functions for all basic Go types
  • Safe conversions with error handling options
  • Support for slices, maps, and complex types
  • Protobuf and database driver.Valuer support
  • Zero-allocation string/bytes conversion (Go 1.20 and below)

Installation

go get github.com/go-leo/gonv

Basic Usage

import "github.com/go-leo/gonv"

// Convert string to int
result := gonv.Int[int]("42") // returns 42

// Convert with error handling
result, err := gonv.IntE[int]("invalid") // returns 0, error

// Convert slice of strings to slice of ints
nums := gonv.IntS[[]int]([]string{"1", "2", "3"}) // returns []int{1, 2, 3}

Supported Types

  • Basic types: string, bool, int, uint, float, time.Duration, time.Time
  • Slice types for all basic types
  • Map types with string keys and various value types
  • Protobuf wrapper types
  • Database driver.Valuer implementations

Error Handling

Functions ending with 'E' (e.g., IntE, StringE) return both the converted value and an error, allowing for explicit error handling. Functions without 'E' (e.g., Int, String) ignore errors and return the zero value of the target type when conversion fails.

Examples

String conversions
str := gonv.String[string](42)           // "42"
str := gonv.String[string](true)         // "true"
str := gonv.String[string](3.14)         // "3.14"
Boolean conversions
b := gonv.Bool[bool]("true")             // true
b := gonv.Bool[bool]("false")            // false
b := gonv.Bool[bool](1)                  // true
b := gonv.Bool[bool](0)                  // false
Numeric conversions
i := gonv.Int[int64]("42")               // 42
f := gonv.Float[float64]("3.14")         // 3.14
u := gonv.Uint[uint64]("-1")             // error (negative not allowed)
Time conversions
t := gonv.Time("2023-01-01T12:00:00Z")   // time.Time
d := gonv.Duration("1h30m")              // 1 hour 30 minutes
Slice conversions
strs := gonv.StringS[[]string]([]int{1, 2, 3}) // []string{"1", "2", "3"}
bools := gonv.BoolS[[]bool]([]string{"true", "false"}) // []bool{true, false}
Map conversions
m := gonv.StringIntMap[string, int](map[string]string{"key": "42"}) // map[string]int{"key": 42}

Safety

All conversions are safe and will not panic. When a conversion is not possible, functions either return the zero value of the target type or an error, depending on whether you use the error-handling variant.

Performance

The package uses optimized conversion paths for common type combinations and falls back to reflection only when necessary. String/bytes conversions use unsafe operations for zero-allocation performance on Go 1.20 and earlier versions.

License

MIT

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

Constants

This section is empty.

Variables

View Source
var DefaultTimeFormat = time.RFC3339

DefaultTimeFormat is the default time format used for time string conversions. It uses RFC3339 format (YYYY-MM-DDTHH:MM:SSZ).

View Source
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

func AnySlice(o any) []any

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

func AnySliceE(o any) ([]any, error)

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

func Bool[E ~bool](o any) E

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

func BoolE[E ~bool](o any) (E, error)

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

func BoolS[S ~[]E, E ~bool](o any) S

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

func BoolSE[S ~[]E, E ~bool](o any) (S, error)

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

func BoolSlice[S ~[]E, E ~bool](o any) S

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

func BoolSliceE[S ~[]E, E ~bool](o any) (S, error)

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

func BytesToString(b []byte) string

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

func Duration(o any) time.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

func DurationE(o any) (time.Duration, error)

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

func DurationS(o any) []time.Duration

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

func DurationSE(o any) ([]time.Duration, error)

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

func Slice[S ~[]E, E any](o any, to func(o any) (E, error)) (S, error)

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

func SliceE[S ~[]E, E any](o any, to func(o any) (E, error)) (S, error)

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

func String[E ~string](o any) E

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

func StringAnyMap[K ~string](o any) map[K]any

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

func StringAnyMapE[K ~string](o any) (map[K]any, error)

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

func StringBoolMap[K ~string, V ~bool](o any) map[K]V

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

func StringBoolMapE[K ~string, V ~bool](o any) (map[K]V, error)

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

func StringE[E ~string](o any) (E, error)

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

func StringS[S ~[]E, E ~string](o any) S

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

func StringSE[S ~[]E, E ~string](o any) (S, error)

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

func StringSlice[S ~[]E, E ~string](o any) S

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

func StringSliceE[S ~[]E, E ~string](o any) (S, error)

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

func StringStringMap[K ~string, V ~string](o any) map[K]V

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

func StringStringMapE[K ~string, V ~string](o any) (map[K]V, error)

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

func StringStringSliceMap(o any) map[string][]string

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

func StringStringSliceMapE(o any) (map[string][]string, error)

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

func StringToBytes(s string) []byte

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

func Time(o any) time.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

func TimeE(o any) (time.Time, error)

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

func TimeInLocation(o any, location *time.Location) time.Time

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

func TimeInLocationE(o any, location *time.Location) (time.Time, error)

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.

Jump to

Keyboard shortcuts

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