Documentation
¶
Overview ¶
Package xconv provides conversion utilities between arbitrary types, covering conversions among numeric, string, boolean, byte, rune, duration and storage-capacity types as well as slice and pointer conversions.
Index ¶
- func Anys(val any) []any
- func AnysPointer(val any) *[]any
- func B(val any) float64
- func Bool(val any) bool
- func BoolPointer(any any) *bool
- func Bools(val any) (slice []bool)
- func BoolsPointer(any any) *[]bool
- func Bs(val any) (slice []float64)
- func Byte(val any) byte
- func BytePointer(any any) *byte
- func Bytes(val any) []byte
- func BytesPointer(any any) *[]byte
- func BytesToString(b []byte) string
- func Duration(val any) time.Duration
- func DurationPointer(any any) *time.Duration
- func Durations(val any) (slice []time.Duration)
- func DurationsPointer(any any) *[]time.Duration
- func Float32(val any) float32
- func Float32Pointer(any any) *float32
- func Float32s(val any) (slice []float32)
- func Float32sPointer(any any) *[]float32
- func Float64(val any) float64
- func Float64Pointer(any any) *float64
- func Float64s(val any) (slice []float64)
- func Float64sPointer(any any) *[]float64
- func GenericNumbers[T any](val any) (slice []T)
- func Int(val any) int
- func Int8(val any) int8
- func Int8Pointer(any any) *int8
- func Int8s(val any) (slice []int8)
- func Int8sPointer(any any) *[]int8
- func Int16(val any) int16
- func Int16Pointer(any any) *int16
- func Int16s(val any) (slice []int16)
- func Int16sPointer(any any) *[]int16
- func Int32(val any) int32
- func Int32Pointer(val any) *int32
- func Int32s(val any) (slice []int32)
- func Int32sPointer(val any) *[]int32
- func Int64(val any) int64
- func Int64Pointer(any any) *int64
- func Int64s(val any) (slice []int64)
- func Int64sPointer(any any) *[]int64
- func IntPointer(any any) *int
- func Ints(val any) (slice []int)
- func IntsPointer(any any) *[]int
- func Json(val any) string
- func Rune(val any) rune
- func RunePointer(val any) *int32
- func Runes(val any) []rune
- func RunesPointer(val any) *[]int32
- func String(val any) string
- func StringPointer(val any) *string
- func StringToBytes(s string) (b []byte)
- func Strings(val any) (slice []string)
- func StringsPointer(val any) *[]string
- func Uint(val any) uint
- func Uint8(val any) uint8
- func Uint8Pointer(val any) *uint8
- func Uint8s(val any) (slice []uint8)
- func Uint8sPointer(val any) *[]uint8
- func Uint16(val any) uint16
- func Uint16Pointer(val any) *uint16
- func Uint16s(val any) (slice []uint16)
- func Uint16sPointer(val any) *[]uint16
- func Uint32(val any) uint32
- func Uint32Pointer(val any) *uint32
- func Uint32s(val any) (slice []uint32)
- func Uint32sPointer(val any) *[]uint32
- func Uint64(val any) uint64
- func Uint64Pointer(val any) *uint64
- func Uint64s(val any) (slice []uint64)
- func Uint64sPointer(val any) *[]uint64
- func UintPointer(val any) *uint
- func Uints(val any) (slice []uint)
- func UintsPointer(val any) *[]uint
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Anys ¶ added in v2.1.0
Anys converts val to a slice of any.
Only slice and array types are supported; other types yield nil.
func AnysPointer ¶ added in v2.3.0
AnysPointer converts val to a pointer to a slice of any.
func B ¶ added in v2.2.7
B converts val to a number of storage-capacity bytes as a float64.
Numeric values are returned as is; strings accept capacity units such as "10KB" or "5M" (case-insensitive); the conversion returns 0 when it fails.
func Bool ¶
Bool converts val to a bool.
A numeric value is true when it is non-zero; a string is true when it is non-empty, not "0" and not "false" (case-insensitive); a zero time.Time is false; a slice or map is true when it is non-nil and non-empty; other types are judged by their reflected value.
func BoolsPointer ¶
BoolsPointer converts val to a pointer to a bool slice.
func Bytes ¶
Bytes converts val to a byte slice.
Basic numeric types are encoded in big-endian order (int as 8 bytes, uint as 8 bytes and the others at their own width); a string is converted without copying; a []byte is returned as is; other types are preferentially JSON-marshalled. It returns nil when the conversion fails.
func BytesPointer ¶
BytesPointer converts val to a pointer to a byte slice.
func BytesToString ¶
BytesToString converts b to a string without copying.
The resulting string shares its backing memory with the byte slice, so do not modify the original byte slice.
func Duration ¶
Duration converts val to a time.Duration.
Numeric values are converted directly as nanoseconds; strings accept time units such as "ns", "us" (or "µs"), "ms", "s", "m", "h" and "d" (day), where "d" is replaced by the corresponding number of nanoseconds before parsing.
func DurationPointer ¶
DurationPointer converts val to a pointer to time.Duration.
func DurationsPointer ¶
DurationsPointer converts val to a pointer to a time.Duration slice.
func Float32Pointer ¶
Float32Pointer converts val to a pointer to float32.
func Float32sPointer ¶
Float32sPointer converts val to a pointer to a float32 slice.
func Float64 ¶
Float64 converts val to a float64.
It supports all basic numeric types (the real part is taken for complex numbers), bool, time.Time (as Unix nanoseconds) and types handled through reflection such as strings; it returns 0 when the conversion fails.
func Float64Pointer ¶
Float64Pointer converts val to a pointer to float64.
func Float64sPointer ¶
Float64sPointer converts val to a pointer to a float64 slice.
func GenericNumbers ¶ added in v2.5.3
GenericNumbers converts a slice or array of arbitrary numeric values to a slice of the generic numeric type T.
Elements are converted directly; T may be an integer, unsigned integer, floating-point or a named type based on them. The conversion does not go through JSON serialization and therefore avoids precision loss for large integers; an element that cannot be converted becomes the zero value.
func Int8sPointer ¶
Int8sPointer converts val to a pointer to an int8 slice.
func Int16Pointer ¶
Int16Pointer converts val to a pointer to int16.
func Int16sPointer ¶
Int16sPointer converts val to a pointer to an int16 slice.
func Int32Pointer ¶
Int32Pointer converts val to a pointer to int32.
func Int32sPointer ¶
Int32sPointer converts val to a pointer to an int32 slice.
func Int64 ¶
Int64 converts val to an int64.
It supports all basic numeric types, bool, time.Time, []byte (parsed as a big-endian 64-bit integer and at most 8 bytes long) and types handled through reflection such as strings; it returns 0 when the conversion fails.
func Int64Pointer ¶
Int64Pointer converts val to a pointer to int64.
func Int64sPointer ¶
Int64sPointer converts val to a pointer to an int64 slice.
func IntsPointer ¶
IntsPointer converts val to a pointer to an int slice.
func Json ¶
Json converts val to a JSON string.
A string or byte slice that already looks like JSON (wrapped in { } or [ ]) is returned as is; maps, arrays, slices, structs and the like are JSON-marshalled; other types yield an empty string.
func RunePointer ¶ added in v2.3.0
RunePointer converts val to a pointer to rune.
func RunesPointer ¶ added in v2.3.0
RunesPointer converts val to a pointer to a rune slice.
func String ¶
String converts val to a string.
Numeric types are formatted in decimal, float32 and float64 use the shortest representation, bool becomes true or false, a []byte is converted without copying, a zero time.Time yields an empty string, and other types are preferentially JSON-marshalled.
func StringPointer ¶
StringPointer converts val to a pointer to string.
func StringToBytes ¶
StringToBytes converts s to a byte slice without copying.
The resulting byte slice shares its backing memory with the string; modifying the bytes would change the original string, so do not modify them.
func StringsPointer ¶
StringsPointer converts val to a pointer to a string slice.
func Uint8Pointer ¶
Uint8Pointer converts val to a pointer to uint8.
func Uint8sPointer ¶
Uint8sPointer converts val to a pointer to a uint8 slice.
func Uint16Pointer ¶
Uint16Pointer converts val to a pointer to uint16.
func Uint16sPointer ¶
Uint16sPointer converts val to a pointer to a uint16 slice.
func Uint32Pointer ¶
Uint32Pointer converts val to a pointer to uint32.
func Uint32sPointer ¶
Uint32sPointer converts val to a pointer to a uint32 slice.
func Uint64 ¶
Uint64 converts val to a uint64.
It supports all basic numeric types, bool, time.Time, []byte (parsed as a big-endian 64-bit integer and at most 8 bytes long) and types handled through reflection such as strings; it returns 0 when the conversion fails.
func Uint64Pointer ¶
Uint64Pointer converts val to a pointer to uint64.
func Uint64sPointer ¶
Uint64sPointer converts val to a pointer to a uint64 slice.
func UintsPointer ¶
UintsPointer converts val to a pointer to a uint slice.
Types ¶
This section is empty.