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 ¶
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.
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.