[go: up one dir, main page]

conv

package module
v0.0.0-...-1b223d6 Latest Latest
Warning

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

Go to latest
Published: May 9, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package conv exposes utilities to convert types.

The Convert and Format families of functions are essentially a shorthand to strconv functions, using the decimal representation of numbers.

Features:

  • from string representation to value ("Convert*") and reciprocally ("Format*")
  • from pointer to value (Value) and reciprocally (Pointer)
  • from slice of values to slice of pointers (PointerSlice) and reciprocally (ValueSlice)
  • from map of values to map of pointers (PointerMap) and reciprocally (ValueMap)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertBool

func ConvertBool(str string) (bool, error)

ConvertBool turns a string into a boolean.

It supports a few more "true" strings than strconv.ParseBool:

  • it is not case sensitive ("trUe" or "FalsE" work)
  • "ok", "yes", "y", "on", "selected", "checked", "enabled" are all true
  • everything that is not true is false: there is never an actual error returned

func ConvertFloat

func ConvertFloat[T Float](str string) (T, error)

ConvertFloat turns a string into a float numerical value.

func ConvertFloat32

func ConvertFloat32(str string) (float32, error)

ConvertFloat32 turns a string into a float32.

func ConvertFloat64

func ConvertFloat64(str string) (float64, error)

ConvertFloat64 turns a string into a float64

func ConvertInt16

func ConvertInt16(str string) (int16, error)

ConvertInt16 turns a string into an int16

func ConvertInt32

func ConvertInt32(str string) (int32, error)

ConvertInt32 turns a string into an int32

func ConvertInt64

func ConvertInt64(str string) (int64, error)

ConvertInt64 turns a string into an int64

func ConvertInt8

func ConvertInt8(str string) (int8, error)

ConvertInt8 turns a string into an int8

func ConvertInteger

func ConvertInteger[T Signed](str string) (T, error)

ConvertInteger turns a string into a signed integer.

func ConvertUint16

func ConvertUint16(str string) (uint16, error)

ConvertUint16 turns a string into an uint16

func ConvertUint32

func ConvertUint32(str string) (uint32, error)

ConvertUint32 turns a string into an uint32

func ConvertUint64

func ConvertUint64(str string) (uint64, error)

ConvertUint64 turns a string into an uint64

func ConvertUint8

func ConvertUint8(str string) (uint8, error)

ConvertUint8 turns a string into an uint8

func ConvertUinteger

func ConvertUinteger[T Unsigned](str string) (T, error)

ConvertUinteger turns a string into an unsigned integer.

func FormatBool

func FormatBool(value bool) string

FormatBool turns a boolean into a string.

func FormatFloat

func FormatFloat[T Float](value T) string

FormatFloat turns a floating point numerical value into a string.

func FormatInteger

func FormatInteger[T Signed](value T) string

FormatInteger turns an integer type into a string.

func FormatUinteger

func FormatUinteger[T Unsigned](value T) string

FormatUinteger turns an unsigned integer type into a string.

func IsFloat64AJSONInteger

func IsFloat64AJSONInteger(f float64) bool

IsFloat64AJSONInteger allows for integers [-2^53, 2^53-1] inclusive.

func Pointer

func Pointer[T any](v T) *T

Pointer returns a pointer to the value passed in.

func PointerMap

func PointerMap[K comparable, T any](src map[K]T) map[K]*T

PointerMap converts a map of values into a map of pointers.

func PointerSlice

func PointerSlice[T any](src []T) []*T

PointerSlice converts a slice of values into a slice of pointers.

func Value

func Value[T any](v *T) T

Value returns a shallow copy of the value of the pointer passed in.

If the pointer is nil, the returned value is the zero value.

func ValueMap

func ValueMap[K comparable, T any](src map[K]*T) map[K]T

ValueMap converts a map of pointers into a map of values.

nil elements are skipped.

func ValueSlice

func ValueSlice[T any](src []*T) []T

ValueSlice converts a slice of pointers into a slice of values.

nil elements are zero values.

Types

type Float

type Float interface {
	~float32 | ~float64
}

Float numerical types, cf. golang.org/x/exp/constraints.Float

type Numerical

type Numerical interface {
	Signed | Unsigned | Float
}

Numerical types

type Signed

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

Signed integer types, cf. golang.org/x/exp/constraints.Signed

type Unsigned

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

Unsigned integer types, cf. golang.org/x/exp/constraints.Unsigned

Jump to

Keyboard shortcuts

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