๐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 {.
for _, elem := range []int{1, 2, 3, 4}
p #{elem}
for _, elem := range []int{1, 2, 3, 4} {
// p #{elem}
}
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>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}
<p>a: foo</p>
<p>b: bar</p>Last updated
Was this helpful?