xconv

package
v2.6.2 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: MIT Imports: 11 Imported by: 13

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Anys added in v2.1.0

func Anys(val any) []any

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

func AnysPointer(val any) *[]any

AnysPointer converts val to a pointer to a slice of any.

func B added in v2.2.7

func B(val any) float64

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

func Bool(val any) 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 BoolPointer

func BoolPointer(any any) *bool

BoolPointer converts val to a pointer to bool.

func Bools

func Bools(val any) (slice []bool)

Bools converts val to a bool slice.

func BoolsPointer

func BoolsPointer(any any) *[]bool

BoolsPointer converts val to a pointer to a bool slice.

func Bs added in v2.2.7

func Bs(val any) (slice []float64)

Bs converts val to a slice of storage-capacity bytes as float64.

func Byte

func Byte(val any) byte

Byte converts val to a byte (equivalent to Uint8).

func BytePointer

func BytePointer(any any) *byte

BytePointer converts val to a pointer to byte.

func Bytes

func Bytes(val any) []byte

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

func BytesPointer(any any) *[]byte

BytesPointer converts val to a pointer to a byte slice.

func BytesToString

func BytesToString(b []byte) string

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

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

func DurationPointer(any any) *time.Duration

DurationPointer converts val to a pointer to time.Duration.

func Durations

func Durations(val any) (slice []time.Duration)

Durations converts val to a time.Duration slice.

func DurationsPointer

func DurationsPointer(any any) *[]time.Duration

DurationsPointer converts val to a pointer to a time.Duration slice.

func Float32

func Float32(val any) float32

Float32 converts val to a float32.

func Float32Pointer

func Float32Pointer(any any) *float32

Float32Pointer converts val to a pointer to float32.

func Float32s

func Float32s(val any) (slice []float32)

Float32s converts val to a float32 slice.

func Float32sPointer

func Float32sPointer(any any) *[]float32

Float32sPointer converts val to a pointer to a float32 slice.

func Float64

func Float64(val any) 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

func Float64Pointer(any any) *float64

Float64Pointer converts val to a pointer to float64.

func Float64s

func Float64s(val any) (slice []float64)

Float64s converts val to a float64 slice.

func Float64sPointer

func Float64sPointer(any any) *[]float64

Float64sPointer converts val to a pointer to a float64 slice.

func GenericNumbers added in v2.5.3

func GenericNumbers[T any](val any) (slice []T)

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 Int

func Int(val any) int

Int converts val to an int.

func Int8

func Int8(val any) int8

Int8 converts val to an int8.

func Int8Pointer

func Int8Pointer(any any) *int8

Int8Pointer converts val to a pointer to int8.

func Int8s

func Int8s(val any) (slice []int8)

Int8s converts val to an int8 slice.

func Int8sPointer

func Int8sPointer(any any) *[]int8

Int8sPointer converts val to a pointer to an int8 slice.

func Int16

func Int16(val any) int16

Int16 converts val to an int16.

func Int16Pointer

func Int16Pointer(any any) *int16

Int16Pointer converts val to a pointer to int16.

func Int16s

func Int16s(val any) (slice []int16)

Int16s converts val to an int16 slice.

func Int16sPointer

func Int16sPointer(any any) *[]int16

Int16sPointer converts val to a pointer to an int16 slice.

func Int32

func Int32(val any) int32

Int32 converts val to an int32.

func Int32Pointer

func Int32Pointer(val any) *int32

Int32Pointer converts val to a pointer to int32.

func Int32s

func Int32s(val any) (slice []int32)

Int32s converts val to an int32 slice.

func Int32sPointer

func Int32sPointer(val any) *[]int32

Int32sPointer converts val to a pointer to an int32 slice.

func Int64

func Int64(val any) 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

func Int64Pointer(any any) *int64

Int64Pointer converts val to a pointer to int64.

func Int64s

func Int64s(val any) (slice []int64)

Int64s converts val to an int64 slice.

func Int64sPointer

func Int64sPointer(any any) *[]int64

Int64sPointer converts val to a pointer to an int64 slice.

func IntPointer

func IntPointer(any any) *int

IntPointer converts val to a pointer to int.

func Ints

func Ints(val any) (slice []int)

Ints converts val to an int slice.

func IntsPointer

func IntsPointer(any any) *[]int

IntsPointer converts val to a pointer to an int slice.

func Json

func Json(val any) string

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 Rune added in v2.3.0

func Rune(val any) rune

Rune converts val to a rune (equivalent to Int32).

func RunePointer added in v2.3.0

func RunePointer(val any) *int32

RunePointer converts val to a pointer to rune.

func Runes added in v2.3.0

func Runes(val any) []rune

Runes converts val to a rune slice (equivalent to Int32s).

func RunesPointer added in v2.3.0

func RunesPointer(val any) *[]int32

RunesPointer converts val to a pointer to a rune slice.

func String

func String(val any) 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

func StringPointer(val any) *string

StringPointer converts val to a pointer to string.

func StringToBytes

func StringToBytes(s string) (b []byte)

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 Strings

func Strings(val any) (slice []string)

Strings converts val to a string slice.

func StringsPointer

func StringsPointer(val any) *[]string

StringsPointer converts val to a pointer to a string slice.

func Uint

func Uint(val any) uint

Uint converts val to a uint.

func Uint8

func Uint8(val any) uint8

Uint8 converts val to a uint8.

func Uint8Pointer

func Uint8Pointer(val any) *uint8

Uint8Pointer converts val to a pointer to uint8.

func Uint8s

func Uint8s(val any) (slice []uint8)

Uint8s converts val to a uint8 slice.

func Uint8sPointer

func Uint8sPointer(val any) *[]uint8

Uint8sPointer converts val to a pointer to a uint8 slice.

func Uint16

func Uint16(val any) uint16

Uint16 converts val to a uint16.

func Uint16Pointer

func Uint16Pointer(val any) *uint16

Uint16Pointer converts val to a pointer to uint16.

func Uint16s

func Uint16s(val any) (slice []uint16)

Uint16s converts val to a uint16 slice.

func Uint16sPointer

func Uint16sPointer(val any) *[]uint16

Uint16sPointer converts val to a pointer to a uint16 slice.

func Uint32

func Uint32(val any) uint32

Uint32 converts val to a uint32.

func Uint32Pointer

func Uint32Pointer(val any) *uint32

Uint32Pointer converts val to a pointer to uint32.

func Uint32s

func Uint32s(val any) (slice []uint32)

Uint32s converts val to a uint32 slice.

func Uint32sPointer

func Uint32sPointer(val any) *[]uint32

Uint32sPointer converts val to a pointer to a uint32 slice.

func Uint64

func Uint64(val any) 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

func Uint64Pointer(val any) *uint64

Uint64Pointer converts val to a pointer to uint64.

func Uint64s

func Uint64s(val any) (slice []uint64)

Uint64s converts val to a uint64 slice.

func Uint64sPointer

func Uint64sPointer(val any) *[]uint64

Uint64sPointer converts val to a pointer to a uint64 slice.

func UintPointer

func UintPointer(val any) *uint

UintPointer converts val to a pointer to uint.

func Uints

func Uints(val any) (slice []uint)

Uints converts val to a uint slice.

func UintsPointer

func UintsPointer(val any) *[]uint

UintsPointer converts val to a pointer to a uint slice.

Types

This section is empty.

Jump to

Keyboard shortcuts

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