# Range

The provided Go code demonstrates the usage of the `range` keyword in different contexts.

```go
package main

import "fmt"

func main() {
	// Summing elements in a slice
	nums := []int{2, 3, 4}
	sum := 0
	for _, num := range nums {
		sum += num
	}
	fmt.Println("sum:", sum)

	// Finding the index of a specific value in a slice
	for i, num := range nums {
		if num == 3 {
			fmt.Println("index:", i)
		}
	}

	// Iterating over key-value pairs in a map
	kvs := map[string]string{"a": "apple", "b": "banana"}
	for k, v := range kvs {
		fmt.Printf("%s -> %s\n", k, v)
	}

	// Iterating over keys in a map
	for k := range kvs {
		fmt.Println("key:", k)
	}

	// Iterating over Unicode characters in a string
	for i, c := range "go" {
		fmt.Println(i, c)
	}
}
```

#### Output

```
sum: 9
index: 1
b -> banana
a -> apple
key: a
key: b
0 103
1 111
```

Let's break down each part:

1. **Summing elements in a slice:**

   ```go
   nums := []int{2, 3, 4}
   sum := 0
   for _, num := range nums {
       sum += num
   }
   fmt.Println("sum:", sum)
   ```

   This part calculates the sum of all elements in the `nums` slice and prints the result.
2. **Finding the index of a specific value in a slice:**

   ```go
   for i, num := range nums {
       if num == 3 {
           fmt.Println("index:", i)
       }
   }
   ```

   This part iterates over the `nums` slice and prints the index when the value is equal to 3.
3. **Iterating over key-value pairs in a map:**

   ```go
   kvs := map[string]string{"a": "apple", "b": "banana"}
   for k, v := range kvs {
       fmt.Printf("%s -> %s\n", k, v)
   }
   ```

   This section iterates over the key-value pairs in the `kvs` map and prints each pair.
4. **Iterating over keys in a map:**

   ```go
   for k := range kvs {
       fmt.Println("key:", k)
   }
   ```

   This part iterates over the keys of the `kvs` map and prints each key.
5. **Iterating over Unicode characters in a string:**

   ```go
   for i, c := range "go" {
       fmt.Println(i, c)
   }
   ```

   This loop iterates over the Unicode characters in the string "go" and prints the index and Unicode code point of each character.

When you run the entire program, you'll get the following output:

```
sum: 9
index: 1
a -> apple
b -> banana
key: a
key: b
0 103
1 111
```

This output corresponds to the execution of each part of the code.


---

# Agent Instructions: 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:

```
GET https://ineelhere.gitbook.io/code-with-go/11-range.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
