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