Skip to content

nil coalescing ? #1175

Jun 15, 2021 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

(I've moved this to a discussion, as this isn't an issue so much as a question)

@daniel-torok There is a way to accomplish this, though not in a way that's quite so compact. This can, however, be accomplished with an if condition.

One issue is that if you try to reference a missing key, gomplate will error - this is by design as gomplate treats missing keys as errors (see #310 for some discussion on this). To avoid this, you'll want to use has.

Here's an example:

{{ $ds := datasource "ds" }}
foo:
  bar: {{ if has $ds "nonExisting" }}{{ $ds.nonExisting }}{{ else }}1{{end}}

Alternately, if the key does exist, but you want to test whether it's false, you could look at the ternary function. H…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@daniel-torok
Comment options

Answer selected by daniel-torok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1173 on June 17, 2021 12:47.