HUGO

Inverting boolean not working

Inverting boolean not working
I’m new to the go language and after looking up i found the way to reverse a boolean but the result is not what I expected.

Output:
true, true, true, true

Expected output:
true, false, true, false

Why is the not operator not working?

See https://golang.org/pkg/text/template/#hdr-Variables

A variable’s scope extends to the “end” action of the control structure (“if”, “with”, or “range”) in which it is declared, or to the end of the template if there is no such control structure.

Example 1

{{ $var := "foo" }}
{{ range seq 4}}
  {{ $var }}
  {{ $var := "bar" }}
{{ end }}

foo foo foo foo

Example 2

{{ $var := "foo" }}
{{ range seq 4}}
  {{ $var }}
  {{ $var = "bar" }}
{{ end }}

foo bar bar bar

Thanks for the quick reply!
The question now is: how can I acclompish a true, false, true, false?
I want to loop over all my components and if it’s true i will give in the dict of my partial a background color.

Expected result:
background color
no background color
background color
no background color

See example 2 above.

{{ $var := true }}
{{ range seq 4}}
  {{ $var }}
  {{ $var = not $var}}
{{ end }}

true false true false

Your solution was right but in my example I had to switch line 5 with line 4.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.