🔁For

Just like with if and switch, corgi also provides a built-in for-loop.

You can write anything behind the for that you could also write behind a Go for. Just don't add a {.

Unlike Go templates, when writing a for-range loop the index goes first, just like in regular Go.

for _, elem := range []int{1, 2, 3, 4}
  p #{elem}

Ordered Map Iteration

When iterating over maps the order is non-deterministic, meaning the elements will appear in "random" order.

If you always want to print elements in the same (sorted) order, you can use corgi's ordered range keyword, which allows you to range over maps with comparable keys (as defined by the comparable constraint) in ascending order.

- m := map[string]string{"b": "bar", "a": "foo"}
for k, v := ordered range m
  p #{k}: #{v}

Last updated