Std

The Std registry provides a set of standard functions for common tasks, included by default, making it easy to perform basic operations without additional setup.

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

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

hello

The function returns a simple greeting string, "Hello!" It serves as a basic test function to verify that the system is working correctly.

Signature

Hello() string
{{ hello }} // Output: Hello!

default

The function returns the first non-empty value from a provided list of arguments. If the list is empty or the first value is empty, it returns a specified default value. If you're looking to find the first non-empty value from a list of multiple options, the Coalesce function is a better choice.

Signature

Default(defaultValue any, given ...any) any
{{ nil | default "default" }} // Output: "default"
{{ "" | default "default" }}  // Output: "default"
{{ "first" | default "default" }} // Output: "first"
{{ "first" | default "default" "second" }} // Output: "second"

empty

The function checks if the provided value is empty, returning true if it is considered empty based on its type. This function is useful for determining whether a value is present or absent across different data types.

Signature

Empty(given any) bool
{{ nil | empty }} // Output: true
{{ "" | empty }} // Output: true
{{ 0 | empty }} // Output: true
{{ false | empty }} // Output: true
{{ struct{}{} | empty }} // Output: false

all

The function checks if all values in the provided variadic slice are non-empty. It returns true only if every value is considered non-empty according to the criteria used by the Empty method.

Signature

All(values ...any) bool
{{ 1, "hello", true | all }} // Output: true
{{ 1, "", true | all }} // Output: false

any

The function checks if any of the provided values are non-empty. It returns true if at least one value is considered non-empty.

Signature

Any(values ...any) bool
{{ "", 0, false | any }} // Output: false
{{ "", 0, "text" | any }} // Output: true

coalesce

The function returns the first non-empty value from the provided list. If all values are empty, it returns nil.

Signature

Coalesce(values ...any) any
{{ nil, "", "first", "second" | coalesce }} // Output: "first"

ternary

The function mimics the ternary conditional operator found in many programming languages. It returns trueValue if the condition is true; otherwise, it returns falseValue.

Signature

Ternary(trueValue any, falseValue any, condition bool) any
{{ true | ternary "yes", "no" }} // Output: "yes"
{{ false | ternary "yes", "no" }} // Output: "no"

cat

The function concatenates a series of values into a single string, converting each value to its string representation and separating them with spaces. Nil values are skipped, and no trailing spaces are added.

Signature

Cat(values ...any) string
{{ "Hello", nil, 123, true | cat }} // Output: "Hello 123 true"

Last updated

Change request #24: reflect safe functions feature and new signatures