githubEdit

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.

circle-info

You can easily import all the functions from the conversion registry by including the following import statement in your code

import "github.com/go-sprout/sprout/registry/conversion"

toBool

toBool converts a value from any types reasonably be converted to a boolean value. Using the cast arrow-up-rightpackage.

Signature

{{ "true" | toBool }} // Output: true
{{ "t" | toBool }} // Output: true
{{ 1 | toBool }} // Output: true
{{ 0.0 | toBool }} // Output: false
{{ "invalid" | toBool }} // Error

toInt

toInt converts a value into an int. Using the cast arrow-up-rightpackage.

Signature

{{ "1" | toInt }} // Output: 1
{{ 1.1 | toInt }} // Output: 1
{{ true | toInt }} // Output: 1
{{ "invalid" | toInt }} // Error

toInt64

toInt64 converts a value into an int64. Using the cast arrow-up-rightpackage.

Signature

toUint

toUint converts a value into a uint. Utilizes the castarrow-up-right package for conversion.

Signature

toUint64

toUint64 converts a value into a uint64. Utilizes the castarrow-up-right package for conversion.

Signature

toFloat64

toFloat64 converts a value into a float64. Utilizes the castarrow-up-right package for conversion.

Signature

toOctal

toOctal parses a value as an octal (base 8) integer.

Signature

toString

toString converts a value to a string, handling various types effectively.

Signature

circle-info

Note: toString can handle various types as:

  • error and output err.Error()

  • fmt.Stringer and output o.String()

toDate

toDate converts a string to a time.Time object based on a format specification.

Signature

This example will takes the "2024-05-10" string and convert it with the layout "2006-01-02".

circle-info

See more about Golang Layout on the official documentationarrow-up-right.

toLocalDate

toLocalDate converts a string to a time.Time object based on a format specification and the local timezone.

Signature

circle-info

See more about Golang Layout on the official documentationarrow-up-right.

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

Deprecated functions

atoi ⚠️

circle-exclamation

No error handling

int ⚠️

circle-exclamation

int64 ⚠️

circle-exclamation

float64 ⚠️

circle-exclamation

Last updated