| 1 |
--- |
| 2 |
title: compare.Default |
| 3 |
description: Returns the second argument if set, else the first argument. |
| 4 |
keywords: [] |
| 5 |
params: |
| 6 |
functions_and_methods: |
| 7 |
aliases: [default] |
| 8 |
returnType: any |
| 9 |
signatures: [compare.Default DEFAULT INPUT] |
| 10 |
aliases: [/functions/default] |
| 11 |
--- |
| 12 |
|
| 13 |
The `default` function returns the second argument if set, else the first argument. |
| 14 |
|
| 15 |
> [!note] |
| 16 |
> When the second argument is the boolean `false` value, the `default` function returns `false`. All _other_ falsy values are considered unset. |
| 17 |
> |
| 18 |
> The falsy values are `false`, `0`, any `nil` pointer or interface value, any array, slice, map, or string of length zero, and zero `time.Time` values. |
| 19 |
> |
| 20 |
> Everything else is truthy. |
| 21 |
> |
| 22 |
> To set a default value based on truthiness, use the [`or`] operator instead. |
| 23 |
|
| 24 |
The `default` function returns the second argument if set: |
| 25 |
|
| 26 |
```go-html-template |
| 27 |
{{ default 42 1 }} → 1 |
| 28 |
{{ default 42 "foo" }} → foo |
| 29 |
{{ default 42 (dict "k" "v") }} → map[k:v] |
| 30 |
{{ default 42 (slice "a" "b") }} → [a b] |
| 31 |
{{ default 42 true }} → true |
| 32 |
|
| 33 |
<!-- As noted above, the boolean "false" is considered set --> |
| 34 |
{{ default 42 false }} → false |
| 35 |
``` |
| 36 |
|
| 37 |
The `default` function returns the first argument if the second argument is not set: |
| 38 |
|
| 39 |
```go-html-template |
| 40 |
{{ default 42 0 }} → 42 |
| 41 |
{{ default 42 "" }} → 42 |
| 42 |
{{ default 42 dict }} → 42 |
| 43 |
{{ default 42 slice }} → 42 |
| 44 |
{{ default 42 nil }} → 42 |
| 45 |
``` |
| 46 |
|
| 47 |
[`or`]: /functions/go-template/or/ |