Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

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.

PreviousMapNextFunctions

Last updated 1 year ago

Was this helpful?