March 2020
Intermediate to advanced
406 pages
8h 39m
English
Sprig's default functions return default values for templated functions. We can check the default values for particular data structures and whether or not they are empty. Empty is defined for each data type:
| Numeric |
0 |
| String | "" (empty string) |
| Lists |
[] (empty list) |
| Dicts |
{} (empty dict) |
| Boolean |
false |
| And always |
Nil (also referred to as null) |
| Structs |
No definition for empty; will never return the default |
We start with our imports:
package main import ( "fmt" "os" "text/template" "github.com/Masterminds/sprig")
Next, we set up our empty and non-empty template variables:
func main() { emptyTemplate := map[string]interface{}{"Name": ""} fullTemplate := map[string]interface{}{"Name": "Bob"} ...Read now
Unlock full access