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 ¶
- func ConvertBool(str string) (bool, error)
- func ConvertFloat[T Float](str string) (T, error)
- func ConvertFloat32(str string) (float32, error)
- func ConvertFloat64(str string) (float64, error)
- func ConvertInt16(str string) (int16, error)
- func ConvertInt32(str string) (int32, error)
- func ConvertInt64(str string) (int64, error)
- func ConvertInt8(str string) (int8, error)
- func ConvertInteger[T Signed](str string) (T, error)
- func ConvertUint16(str string) (uint16, error)
- func ConvertUint32(str string) (uint32, error)
- func ConvertUint64(str string) (uint64, error)
- func ConvertUint8(str string) (uint8, error)
- func ConvertUinteger[T Unsigned](str string) (T, error)
- func FormatBool(value bool) string
- func FormatFloat[T Float](value T) string
- func FormatInteger[T Signed](value T) string
- func FormatUinteger[T Unsigned](value T) string
- func IsFloat64AJSONInteger(f float64) bool
- func Pointer[T any](v T) *T
- func PointerMap[K comparable, T any](src map[K]T) map[K]*T
- func PointerSlice[T any](src []T) []*T
- func Value[T any](v *T) T
- func ValueMap[K comparable, T any](src map[K]*T) map[K]T
- func ValueSlice[T any](src []*T) []T
- type Float
- type Numerical
- type Signed
- type Unsigned
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConvertBool ¶
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 ¶
ConvertFloat turns a string into a float numerical value.
func ConvertFloat32 ¶
ConvertFloat32 turns a string into a float32.
func ConvertFloat64 ¶
ConvertFloat64 turns a string into a float64
func ConvertInt16 ¶
ConvertInt16 turns a string into an int16
func ConvertInt32 ¶
ConvertInt32 turns a string into an int32
func ConvertInt64 ¶
ConvertInt64 turns a string into an int64
func ConvertInt8 ¶
ConvertInt8 turns a string into an int8
func ConvertInteger ¶
ConvertInteger turns a string into a signed integer.
func ConvertUint16 ¶
ConvertUint16 turns a string into an uint16
func ConvertUint32 ¶
ConvertUint32 turns a string into an uint32
func ConvertUint64 ¶
ConvertUint64 turns a string into an uint64
func ConvertUint8 ¶
ConvertUint8 turns a string into an uint8
func ConvertUinteger ¶
ConvertUinteger turns a string into an unsigned integer.
func FormatFloat ¶
FormatFloat turns a floating point numerical value into a string.
func FormatInteger ¶
FormatInteger turns an integer type into a string.
func FormatUinteger ¶
FormatUinteger turns an unsigned integer type into a string.
func IsFloat64AJSONInteger ¶
IsFloat64AJSONInteger allows for integers [-2^53, 2^53-1] inclusive.
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 ¶
Float numerical types, cf. golang.org/x/exp/constraints.Float
type Signed ¶
Signed integer types, cf. golang.org/x/exp/constraints.Signed