Range

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

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:

    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:

    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:

    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:

    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:

    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.

Last updated