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
Must version
β
{{ "true" | toBool }} // Output: true
{{ "t" | toBool }} // Output: true
{{ 1 | toBool }} // Output: true
{{ 0.0 | toBool }} // Output: false
{{ "invalid" | toBool }} // Output: falsetoInt
toInt converts a value into a int. Using the cast package.
Signature
Must version
β
{{ "1" | toInt }} // Output: 1
{{ 1.1 | toInt }} // Output: 1
{{ true | toInt }} // Output: 1
{{ "invalid" | toInt }} // Output: 0toInt64
toInt64 converts a value into a int64. Using the cast package.
Signature
Must version
β
toUint
toUint converts a value into a uint. Utilizes the cast package for conversion.
Signature
Must version
β
toUint64
toUint64 converts a value into a uint64. Utilizes the cast package for conversion.
Signature
Must version
β
toFloat64
toFloat64 converts a value into a float64. Utilizes the cast package for conversion.
Signature
Must version
β
toOctal
toOctal parses a value as an octal (base 8) integer.
Signature
Must version
β
toString
toString converts a value to a string, handling various types effectively.
Signature
Must version
β
toDate / toMustDate
toDate converts a string to a time.Time object based on a format specification.
Signature
Must version
β
This example will takes the "2024-05-10 11:12:42" string and convert it with the layout "2006-01-02".
This example will takes the "2024-05-10 11:12:42" string and convert it with the layout "2006-01-02".
βοΈ Native Go Template error handling. In case of error, the template rendering stop.
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
Must version
β
Deprecated functions
atoi β οΈ
[DEPRECATED] Use toIntinstead.
β No error handling
int β οΈ
[DEPRECATED] Use toIntinstead.
int64 β οΈ
[DEPRECATED] Use toInt64instead.
float64 β οΈ
[DEPRECATED] Use toFloat64instead.
Last updated