conversion functions
These are a collection of functions that mostly help converting from one type
to another - generally from a string to something else, and vice-versa.
conv.Bool
Alias: bool
Note: See also conv.ToBool for a more flexible variant.
Converts a true-ish string to a boolean. Can be used to simplify conditional statements based on environment variables or other text input.
Added in gomplate v0.2.0
Usage
Arguments
| name | description |
|---|---|
in |
(required) the input string |
Examples
input.tmpl:
conv.Default
Alias: default
Provides a default value given an empty input. Empty inputs are 0 for numeric
types, "" for strings, false for booleans, empty arrays/maps, and nil.
Note that this will not provide a default for the case where the input is undefined
(i.e. referencing things like .foo where there is no foo field of .), but
coll.Has can be used for that.
Added in gomplate v2.5.0
Usage
Arguments
| name | description |
|---|---|
default |
(required) the default value |
in |
(required) the input |
Examples
conv.Join
Alias: join
Concatenates the elements of an array to create a string. The separator string sep is placed between elements in the resulting string.
Added in gomplate v0.4.0
Usage
Arguments
| name | description |
|---|---|
in |
(required) the array or slice |
sep |
(required) the separator |
Examples
conv.URL
Alias: urlParse
Parses a string as a URL for later use. Equivalent to url.Parse
Any of url.URL’s methods can be called on the result.
Added in gomplate v2.0.0
Usage
Arguments
| name | description |
|---|---|
in |
(required) the URL string to parse |
Examples
input.tmpl:
Call Redacted to hide the password in the output:
conv.ParseInt
Note: See conv.ToInt64 instead for a simpler and more flexible variant of this function.
Parses a string as an int64. Equivalent to strconv.ParseInt
Added in gomplate v1.4.0
Usage
Examples
input.tmpl:
conv.ParseFloat
Note: See conv.ToFloat64 instead for a simpler and more flexible variant of this function.
Parses a string as an float64 for later use. Equivalent to strconv.ParseFloat
Added in gomplate v1.4.0
Usage
Examples
input.tmpl:
conv.ParseUint
Parses a string as an uint64 for later use. Equivalent to strconv.ParseUint
Added in gomplate v1.4.0
Usage
Examples
input.tmpl:
conv.Atoi
Note: See conv.ToInt and conv.ToInt64 instead for simpler and more flexible variants of this function.
Parses a string as an int for later use. Equivalent to strconv.Atoi
Added in gomplate v1.4.0
Usage
Examples
input.tmpl:
conv.ToBool
Converts the input to a boolean value.
Possible true values are: 1 or the strings "t", "true", or "yes"
(any capitalizations). All other values are considered false.
Added in gomplate v2.7.0
Usage
Arguments
| name | description |
|---|---|
input |
(required) The input to convert |
Examples
conv.ToBools
Converts a list of inputs to an array of boolean values.
Possible true values are: 1 or the strings "t", "true", or "yes"
(any capitalizations). All other values are considered false.
Added in gomplate v2.7.0
Usage
Arguments
| name | description |
|---|---|
input |
(required) The input array to convert |
Examples
conv.ToInt64
Converts the input to an int64 (64-bit signed integer).
This function attempts to convert most types of input (strings, numbers, and booleans).
Unconvertible inputs will result in errors.
Floating-point numbers (with decimal points) are truncated.
Added in gomplate v2.2.0
Usage
Arguments
| name | description |
|---|---|
in |
(required) the value to convert |
Examples
conv.ToInt
Converts the input to an int (signed integer, 32- or 64-bit depending
on platform). This is similar to conv.ToInt64 on 64-bit
platforms, but is useful when input to another function must be provided
as an int.
Unconvertible inputs will result in errors.
On 32-bit systems, given a number that is too large to fit in an int,
the result is -1. This is done to protect against
CWE-190 and
CWE-681.
See also conv.ToInt64.
Added in gomplate v2.2.0
Usage
Arguments
| name | description |
|---|---|
in |
(required) the value to convert |
Examples
conv.ToInt64s
Converts the inputs to an array of int64s.
Unconvertible inputs will result in errors.
This delegates to conv.ToInt64 for each input argument.
Added in gomplate v2.2.0
Usage
Arguments
| name | description |
|---|---|
in... |
(required) the inputs to be converted |
Examples
conv.ToInts
Converts the inputs to an array of ints.
Unconvertible inputs will result in errors.
This delegates to conv.ToInt for each input argument.
Added in gomplate v2.2.0
Usage
Arguments
| name | description |
|---|---|
in... |
(required) the inputs to be converted |
Examples
conv.ToFloat64
Converts the input to a float64.
This function attempts to convert most types of input (strings, numbers, and booleans).
Unconvertible inputs will result in errors.
Added in gomplate v2.2.0
Usage
Arguments
| name | description |
|---|---|
in |
(required) the value to convert |
Examples
conv.ToFloat64s
Converts the inputs to an array of float64s.
Unconvertible inputs will result in errors.
This delegates to conv.ToFloat64 for each input argument.
Added in gomplate v2.2.0
Usage
Arguments
| name | description |
|---|---|
in... |
(required) the inputs to be converted |
Examples
conv.ToString
Converts the input (of any type) to a string.
The input will always be represented in some way.
Added in gomplate v2.5.0
Usage
Arguments
| name | description |
|---|---|
in |
(required) the value to convert |
Examples
conv.ToStrings
Converts the inputs (of any type) to an array of strings
This delegates to conv.ToString for each input argument.
Added in gomplate v2.5.0
Usage
Arguments
| name | description |
|---|---|
in... |
(required) the inputs to be converted |