
Description
I am trying to do a very simple task in a Go Template which amounts to "if something in a range is found that fulfills a certain condition then output A else output B".
However, this is really trivial task appears to be absolutely impossible to do:
{{ $foundAnAddress := 0 }}
{{ range $address := $container.Addresses }}
{{ if eq $address.Port "8000" }}
{{ $foundAnAddress := 1 }}
server backend_{{ $container.Name }} {{ $address.IP }}:{{ $address.Port }}
{{ end }}
{{ end }}
{{ if eq $foundAnAddress 0 }}
# server is unreachable !! -> set maintenance host
server maintenance localhost:8000
{{ end }}
Why is it impossible? Because it appears to be not doable to pass any information from the inner loop outside again since it immediately expires in the scope, so the {{ if .. }} at the end never works as intended.
This stackoverflow answer http://stackoverflow.com/questions/28674199/in-a-go-template-range-loop-are-variables-declared-outside-the-loop-reset-on-ea describes workarounds involving modifying application code.
However, this is completely unrealistic and impractical because in the real world, usually the person writing a template is an end-user of the application and not a developer that would fork it and modify the application internals just to do such a simple task.
Therefore, please stop intentionally crippling the Go Template variable handling and allow people to write functional templates where at least some very simple logical tasks that aren't absolutely trivial can be done without immediately being required to fork and rewrite the entire application. (yes I get it, logic is always supposed to reside in the application. But sometimes that's just not a practical solution if you need a template that works right now and not in 10 years when the application was finally patched with the specific functionality required in your template.)
PS: I'm not very proficient in Go. My sincere apologies if the task above is actually possible without modifying application code. However, I read a lot of docs on Go Template by now and I haven't found a single way of doing it that doesn't require me to patch the host application..