I was just wondering if it is possible to conditionally skip an iteration inside a range.
Range conditionals are possible, but I am not sure what you need. Searching this forum for “range conditional” here is one example:
I’m looking for a way to skip an unnecessary iteration in range. For example, in Java, we can do so.
for(int i=0; i<3; i++) {
if(i==2)
continue;
System.out.print(i+" ");
}
This example would output 0 1 3
. Here continue
skips the i=2
iteration. I’m looking for an analog for Hugo.
Your example can be rewritten as:
for(int i=0; i<3; i++) {
if(i!=2)
System.out.print(i+" ");
}
(IF i IS NOT 2) and does not require a continue
then.
I myself know that. But there are some situations where I need continue
. Does Hugo have something similar?
Hi @UtkarshVerma. I think you’ll have better luck if you give us a concrete example of what you’d like to do.
To my knowledge and the Go Template doc there is nothing equivalent to continue
or break
in ranges
I agree, it’s not possible. Our own Bep did make a feature request for it here, which got accepted by the Go team.
In the past this feature was added, but then got reverted back because of issues.
Currently break
and continue
are unplanned. So it looks it may take a while. (If they were in an upcoming Go version, they move from unplanned to planned.)
use range with index
and the math functions
with if eq all is done
I myself have a workaround for that. It is exactly what @davidsneighbour had suggested. Therefore what I was looking for wasn’t a solution. It was just me asking if such a feature was present in Go or not.
Oh. Anyhow, thanks for the information.