| 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 |
{{ 1 | default 42 }} → 1 |
| 28 |
{{ "foo" | default 42 }} → foo |
| 29 |
{{ dict "k" "v" | default 42 }} → map[k:v] |
| 30 |
{{ slice "a" "b" | default 42 }} → [a b] |
| 31 |
{{ true | default 42 }} → true |
| 32 |
|
| 33 |
<!-- As noted above, the boolean "false" is considered set --> |
| 34 |
{{ false | default 42 }} → false |
| 35 |
``` |
| 36 |
|
| 37 |
The `default` function returns the first argument if the second argument is not set: |
| 38 |
|
| 39 |
```go-html-template |
| 40 |
{{ 0 | default 42 }} → 42 |
| 41 |
{{ "" | default 42 }} → 42 |
| 42 |
{{ dict | default 42 }} → 42 |
| 43 |
{{ slice | default 42 }} → 42 |
| 44 |
{{ nil | default 42 }} → 42 |
| 45 |
``` |
| 46 |
|
| 47 |
[`or`]: /functions/go-template/or/ |