cast

package
v1.5.3 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToBool

func ToBool(v any) (bool, error)

ToBool converts a value of any type to a boolean. It supports boolean types, string representations of booleans, and numeric types (0 is false, non-zero is true). Returns an error if the conversion is not possible.

func ToBoolDefault

func ToBoolDefault(v any, def bool) bool

ToBoolDefault converts a value of any type to a boolean, returning a default value if conversion fails. It uses ToBool for the conversion and returns the provided default value if an error occurs.

func ToDuration

func ToDuration(v any) (time.Duration, error)

ToDuration converts a value of any type to a time.Duration. It supports time.Duration, string representations (using time.ParseDuration), and numeric types (interpreted as seconds). Returns an error if the conversion is not possible.

func ToDurationDefault

func ToDurationDefault(v any, def time.Duration) time.Duration

ToDurationDefault converts a value of any type to a time.Duration, returning a default value if conversion fails. It uses ToDuration for the conversion and returns the provided default value if an error occurs.

func ToFloat64

func ToFloat64(v any) (float64, error)

ToFloat64 converts a value of any type to a float64. It supports various numeric types, string representations of floats, and fmt.Stringer implementations. Returns an error if the conversion is not possible.

func ToFloat64Default

func ToFloat64Default(v any, def float64) float64

ToFloat64Default converts a value of any type to a float64, returning a default value if conversion fails. It uses ToFloat64 for the conversion and returns the provided default value if an error occurs.

func ToInt

func ToInt(v any) (int, error)

ToInt converts a value of any type to an int. It supports various numeric types, string representations of integers, and fmt.Stringer implementations. Returns an error if the conversion is not possible or if the value overflows.

func ToIntDefault

func ToIntDefault(v any, def int) int

ToIntDefault converts a value of any type to an int, returning a default value if conversion fails. It uses ToInt for the conversion and returns the provided default value if an error occurs.

Types

This section is empty.

Jump to

Keyboard shortcuts

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