Skip to content

Index

func Bool

go
func Bool(val any) bool

Bool converts the given value to boolean. Should be used in a known environment, when values are expected to be correct. Panics in case of failure.

Usage:

conv.Bool(5.4) // true
conv.Bool("") // false
conv.Bool(0) // false
conv.Bool(struct{}) // panic!

func Float64

go
func Float64(val any) float64

Float64 converts the given value to float64. Should be used in a known environment, when values are expected to be correct. Panics in case of failure.

Usage:

conv.Float64("5") // 5.0
conv.Float64(true) // 1.0
conv.Float64("qwe") // panic!

func Int

go
func Int(val any) int

Int converts the given value to int. Should be used in a known environment, when values are expected to be correct. Panics in case of failure.

Usage:

conv.Int("123") // 123
conv.Int("asd") // panic!

func Map

go
func Map(value any) map[string]any

Map transforms a given json-marshalable value (usually it's a struct) to the map[string]any.

Usage:

type Example struct{
	A string
	B string
}

conv.Map(Example{ // map[string]any{"A": 1, "B": 2}
	A: "1",
	B: "2",
})

func Ptr

go
func Ptr[T any](val T) *T

Ptr makes a pointer for a given value.

Usage:

conv.Ptr(1) // *int{1}

func PtrRuntime

go
func PtrRuntime(val any) any

PtrRuntime is a runtime version of conv.Ptr.

Usage:

// Please note, you'll receive "any" type which you'll need to cast.
conv.PtrRuntime(1) // any, which holds *int{1}

func String

go
func String(val any) string

String converts the given value to a string. Uses fmt.Sprintf("%v", val) as fallback for unknown types.

Usage:

conv.String(true) // "true"
conv.String(1) // "1"

Generated by gomarkdoc