> For the complete documentation index, see [llms.txt](https://mavolin.gitbook.io/corgi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mavolin.gitbook.io/corgi/learning-corgi/for.md).

# 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 `{`.

{% hint style="info" %}
Unlike Go templates, when writing a for-range loop the index goes first, just like in regular Go.
{% endhint %}

{% tabs %}
{% tab title="Corgi" %}

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


```

{% endtab %}

{% tab title="Go" %}

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

```

{% endtab %}

{% tab title="HTML" %}

```markup
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
```

{% endtab %}
{% endtabs %}

## 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.

{% tabs %}
{% tab title="Corgi" %}

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

```

{% endtab %}

{% tab title="HTML" %}

```markup


  <p>a: foo</p>
  <p>b: bar</p>
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://mavolin.gitbook.io/corgi/learning-corgi/for.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
