| 1 |
--- |
| 2 |
title: compare.Conditional |
| 3 |
description: Returns one of two arguments depending on the value of the control argument. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [cond] |
| 9 |
returnType: any |
| 10 |
signatures: [compare.Conditional CONTROL ARG1 ARG2] |
| 11 |
aliases: [/functions/cond] |
| 12 |
--- |
| 13 |
|
| 14 |
If CONTROL is truthy the function returns ARG1, otherwise it returns ARG2. |
| 15 |
|
| 16 |
```go-html-template |
| 17 |
{{ $qty := 42 }} |
| 18 |
{{ cond (le $qty 3) "few" "many" }} → many |
| 19 |
``` |
| 20 |
|
| 21 |
Unlike [ternary operators] in other languages, the `compare.Conditional` function does not perform [short-circuit evaluation]. It evaluates both ARG1 and ARG2 regardless of the CONTROL value. |
| 22 |
|
| 23 |
[short-circuit evaluation]: https://en.wikipedia.org/wiki/Short-circuit_evaluation |
| 24 |
[ternary operators]: https://en.wikipedia.org/wiki/Ternary_conditional_operator |
| 25 |
|
| 26 |
Due to the absence of short-circuit evaluation, these examples throw an error: |
| 27 |
|
| 28 |
```go-html-template |
| 29 |
{{ cond true "true" (div 1 0) }} |
| 30 |
{{ cond false (div 1 0) "false" }} |
| 31 |
``` |