🌱
Sprout
Github
  • About
  • Roadmap to Sprout v1.0
  • Migration from Sprig
  • Introduction
    • Getting Started
    • Templating Conventions
  • Features
    • Loader System (Registry)
    • Loader System (Registry Group)
    • Function Aliases
    • Function Notices
    • Safe Functions
  • Registries
    • List of all registries
    • Backward
    • Checksum
    • Conversion
    • Crypto
    • Encoding
    • Env
    • Filesystem
    • Maps
    • Numeric
    • Network
    • Random
    • Reflect
    • Regexp
    • SemVer
    • Slices
    • Std
    • Strings
    • Time
    • Uniqueid
  • Groups
    • List of all registry groups
    • All
    • Hermetic
  • Advanced
    • How to create a handler
    • How to create a registry
    • How to create a registry group
  • Links
    • GitHub repository
Powered by GitBook
On this page
  • toBool
  • toInt
  • toInt64
  • toUint
  • toUint64
  • toFloat64
  • toOctal
  • toString
  • toDate
  • toLocalDate
  • toDuration
  • Deprecated functions
  • atoi
  • int
  • int64
  • float64
Edit on GitHub
  1. Registries

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.

PreviousChecksumNextCrypto

Last updated 4 months ago

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

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

Signature

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

toInt64

Signature

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

toUint

Signature

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

toUint64

Signature

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

toFloat64

Signature

{{ "1" | toFloat64 }} // Output: 1
{{ 1.42 | toFloat64 }} // Output: 1.42
{{ true | toFloat64 }} // Output: 1
{{ "invalid" | toFloat64 }} // Error

toOctal

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

Signature

{{ 777 | toOctal }} // Output: "511"
{{ "770" | toOctal }} // Output: "504"
{{ true | toOctal }} // Error
{{ "invalid" | toOctal }} // Error

toString

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

Signature

{{ 1 | toString }} // Output: "1"
{{ 1.42 | toString }} // Output: "1.42"
{{ true | toString }} // Output: "true"
{{ .UnknownOrNil | toString }} // Output: "<nil>"

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

{{ toDate "2006-01-02" "2024-05-10" }}
// Output: 2024-05-10 00:00:00 +0000 UTC

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

toLocalDate

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

Signature

{{ "2024-09-17" | toLocalDate "2006-01-02" "Europe/Paris" }}
// Output: 2024-09-17 00:00:00 +0200 CEST
{{ "2024-09-17" | toLocalDate "2006-01-02" "MST" }}
// Output: 2024-09-17 00:00:00 -0700 MST
{{ "2024-09-17" | toLocalDate "2006-01-02" "invalid" }}
// Error

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

{{ 1 | toDuration }} // Output: 1ns
{{ 1000.0 | toDuration }} // Output: 1µs
{{ "1m" | toDuration }} // Output: 1m0s
{{ "invalid" | toDuration }} // Error
{{ (toDuration "1h30m").Seconds }} // Output: 5400

Deprecated functions

{{ "42" | atoi }} // Output: 42
{{ "42" | int }} // Output: 42
{{ "42" | int64 }} // Output: 42
{{ "42.42" | float64 }} // Output: 42.42

toInt64 converts a value into an int64. Using the package.

toUint converts a value into a uint. Utilizes the package for conversion.

toUint64 converts a value into a uint64. Utilizes the package for conversion.

toFloat64 converts a value into a float64. Utilizes the package for conversion.

See more about Golang Layout on the .

See more about Golang Layout on the .

atoi

[DEPRECATED] Use instead.

No error handling

int

[DEPRECATED] Use instead.

int64

[DEPRECATED] Use instead.

float64

[DEPRECATED] Use instead.

ToBool(v any) (bool, error)
ToInt(v any) (int, error)
ToInt64(v any) (int64, error)
ToUint(v any) (uint, error)
ToUint64(v any) (uint64, error)
ToFloat64(v any) (float64, error)
ToOctal(v any) (int64, error)
ToString(v any) string
ToDate(layout string, value string) (time.Time, error)
ToLocalDate(layout, timezone, value string) (time.Time, error)
ToDuration(v any) (time.Duration, error)
⚠️
❌
⚠️
⚠️
⚠️
cast
cast
cast
cast
cast
cast
official documentation
official documentation
toInt
toInt
toInt64
toFloat64