conv

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package conv converts loosely typed input (form strings, decoded JSON, named scalar types) into Go values without silent data loss: overflow, non-finite floats and unparsable strings return errors wrapping ErrConvert, and float-to-integer conversion truncates toward zero. The validator package uses it for filters, binding and value rendering; it has no other dependencies and is safe for concurrent use.

Index

Constants

This section is empty.

Variables

View Source
var DefaultTimeLayouts = []string{
	time.RFC3339Nano,
	time.RFC3339,
	"2006-01-02 15:04:05",
	"2006-01-02T15:04:05",
	"2006-01-02",
	"2006/01/02",
	"15:04:05",
}

DefaultTimeLayouts are tried in order by ToTime when no layout is given: RFC 3339 with and without fractional seconds, then common date-time, date and time-only forms. Replace or reorder the slice at start-up to change the accepted formats; it is read on every call, so it must not be modified while conversions run.

View Source
var ErrConvert = errors.New("conv: cannot convert value")

ErrConvert is wrapped by every conversion error, so errors.Is(err, ErrConvert) matches any failure; the message names the value and target.

Functions

func ParseBool

func ParseBool(s string) (bool, error)

ParseBool parses the form-style boolean tokens, case-insensitively and after trimming whitespace: "1", "t", "true", "yes", "y" and "on" are true; "", "0", "f", "false", "no", "n" and "off" are false.

Returns an error wrapping ErrConvert for any other input.

func ToBool

func ToBool(v any) (bool, error)

ToBool converts a bool, string, []byte or number to bool: strings through ParseBool, numbers as non-zero, nil as false. Named types convert through their underlying kind.

Returns an error wrapping ErrConvert for unrecognized strings and unsupported types.

func ToFloat

func ToFloat(v any) (float64, error)

ToFloat converts a number, bool, string or []byte to float64; nil and a blank string are 0. Named types convert through their underlying kind.

Returns an error wrapping ErrConvert for NaN or infinite input, strings that do not parse as a Go float literal, and unsupported types.

func ToInt

func ToInt(v any) (int64, error)

ToInt converts a number, bool, string or []byte to int64. Floats and float strings truncate toward zero; nil and a blank string are 0. Named types convert through their underlying kind.

Returns an error wrapping ErrConvert for unsigned values above MaxInt64, non-finite floats, out-of-range strings and unsupported types.

func ToString

func ToString(v any) string

ToString renders v as text for comparison, messages and error keys: strings and []byte as-is, numbers in their shortest exact form, a fmt.Stringer or error through its method, and anything else with %v. Pointers are dereferenced, nil renders as "", and a panicking String or a cyclic value renders as "<unprintable value>" rather than crashing.

func ToTime

func ToTime(v any, layouts ...string) (time.Time, error)

ToTime converts a time.Time, string, []byte or integer (Unix seconds) to a time.Time in UTC, so values from different sources compare consistently. Strings are parsed with layouts, or DefaultTimeLayouts when none are given; nil and a blank string yield the zero time with no error, which callers must treat as absent rather than as year 1.

Returns an error wrapping ErrConvert when no layout matches or the type is unsupported.

func ToUint

func ToUint(v any) (uint64, error)

ToUint converts a number, bool, string or []byte to uint64. Floats and float strings truncate toward zero; nil and a blank string are 0. Named types convert through their underlying kind.

Returns an error wrapping ErrConvert for negative values, non-finite floats, out-of-range strings and unsupported types.

Types

This section is empty.

Jump to

Keyboard shortcuts

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