Conversion
The Conversion registry includes a collection of functions designed to convert one data type to another directly within your templates. This allows for seamless type transformations.
toBool
toBool converts a value from any types reasonably be converted to a boolean. Using the cast package.
Signature
ToBool(v any) (bool, error){{ "true" | toBool }} // Output: true
{{ "t" | toBool }} // Output: true
{{ 1 | toBool }} // Output: true
{{ 0.0 | toBool }} // Output: false
{{ "invalid" | toBool }} // ErrortoInt
toInt converts a value into a int. Using the cast package.
Signature
ToInt(v any) (int, error){{ "1" | toInt }} // Output: 1
{{ 1.1 | toInt }} // Output: 1
{{ true | toInt }} // Output: 1
{{ "invalid" | toInt }} // ErrortoInt64
toInt64 converts a value into a int64. Using the cast package.
Signature
ToInt64(v any) (int64, error){{ "1" | toInt }} // Output: 1
{{ 1.1 | toInt }} // Output: 1
{{ true | toInt }} // Output: 1
{{ "invalid" | toInt }} // ErrortoUint
toUint converts a value into a uint. Utilizes the cast package for conversion.
Signature
ToUint(v any) (uint, error){{ "1" | toUint }} // Output: 1
{{ 1.1 | toUint }} // Output: 1
{{ true | toUint }} // Output: 1
{{ "invalid" | toUint }} // ErrortoUint64
toUint64 converts a value into a uint64. Utilizes the cast package for conversion.
Signature
ToUint64(v any) (uint64, error){{ "1" | toUint64 }} // Output: 1
{{ 1.1 | toUint64 }} // Output: 1
{{ true | toUint64 }} // Output: 1
{{ "invalid" | toUint64 }} // ErrortoFloat64
toFloat64 converts a value into a float64. Utilizes the cast package for conversion.
Signature
ToFloat64(v any) (float64, error){{ "1" | toFloat64 }} // Output: 1
{{ 1.42 | toFloat64 }} // Output: 1.42
{{ true | toFloat64 }} // Output: 1
{{ "invalid" | toFloat64 }} // ErrortoOctal
toOctal parses a value as an octal (base 8) integer.
Signature
ToOctal(v any) (int64, error){{ 777 | toOctal }} // Output: "511"
{{ "770" | toOctal }} // Output: "504"
{{ true | toOctal }} // Output: "1"
{{ "invalid" | toOctal }} // ErrortoString
toString converts a value to a string, handling various types effectively.
Signature
ToString(v any) string{{ 1 | toString }} // Output: "1"
{{ 1.42 | toString }} // Output: "1.42"
{{ true | toString }} // Output: "true"
{{ nil | toString }} // Output: "<nil>"toDate
toDate converts a string to a time.Time object based on a format specification.
Signature
ToDate(layout string, value string) (time.Time, error){{ toDate "2006-01-02", "2024-05-10 11:12:42" }}
// Output: 2024-05-10 00:00:00 +0000 UTC, nilThis example will takes the "2024-05-10 11:12:42" string and convert it with the layout "2006-01-02".
toDuration
toDuration converts a value to a time.Duration. Taking a possibly signed sequence of decimal numbers, each optional fraction and a unit suffix, such 300ms, -1.5h or 2h45m.
Valid time units are ns, us (or µs), ms, s, m and h.
Signature
ToDuration(v any) (time.Duration, error){{ 1 | toDuration }} // Output: 1ns
{{ (1000.0 * 1000.0) | toDuration }} // Output: 1ms
{{ "1m" | toDuration }} // Output: 1m
{{ "invalid" | toDuration }} // Error
{{ (toDuration "1h30m").Seconds }} // Output: 5400Deprecated functions
atoi ⚠️
[DEPRECATED] Use toIntinstead.
{{ "42" | atoi }} // Output: 42❌ No error handling
int ⚠️
[DEPRECATED] Use toIntinstead.
`{{ "42" | int }} // Output: 42int64 ⚠️
[DEPRECATED] Use toInt64instead.
{{ "42" | int64 }} // Output: 42float64 ⚠️
[DEPRECATED] Use toFloat64instead.
`{{ "42.42" | float64 }} // Output: 42.42Last updated