ELEVATE YOUR BUSINESS WITH

Limitless customization options & Elementor compatibility let anyone create a beautiful website with Valiance.

Range in GoLang

SELECT * FROM `itio_tutorial_master` WHERE `tutorial_menu`='1' AND `tutorial_submenu`='21' AND `tutorial_status`=1 LIMIT 1

Range in GoLang

Range in GoLang

In GoLang, the range keyword is used in loops to iterate over elements in various data structures such as arrays, slices, strings, maps, and channels. The range operator returns two values when iterating over these structures: the index (or key) and the value of the element at that position.

Basic Syntax of range:

The syntax for range depends on the type of data structure you are iterating over.

package main

import "fmt"func main() { Index: 1 Planet: MarsIndex: 2 Planet: Venus

2. Using range with a Map:

package main

import "fmt"func main() { Country: USA Capital: Washington D.C.

Country: France Capital: ParisCountry: India Capital: New Delhi

3. Using range with a String:

In the case of strings, range will iterate over the Unicode code points (runes) in the string.

package main

import "fmt"func main() { Index: 0, Char: H

Index: 1, Char: eIndex: 2, Char: lIndex: 3, Char: lIndex: 4, Char: o

4. Using range with a Channel:

package main

import "fmt"func main() { // Creating a channel ch := make(chan int, 3) ch <- 1 ch <- 2 ch <- 3 // Iterating over the channel using range for value := range ch { fmt.Println("Received:", value) }}

Note: Channels are typically closed before iteration to signal when all values have been sent. However, in the example above, for simplicity, we use an unbuffered channel and do not explicitly close it.

Special Cases and Considerations:

  1. Ignoring Values:

    • If you don’t need the index or the value, you can use the blank identifier (_) to ignore them.
    • For example, you might only care about the keys in a map:

    for key := range capitals {

    fmt.Println("Country:", key)}

  2. No Need for Both Index and Value:

    • If you only need the value (not the index), you can omit the first variable:

    for _, planet := range planets {

    fmt.Println("Planet:", planet)}

  3. Looping Over a Slice or Array:

    • range gives you the index and the value, but you can use the index if you want to modify the value directly.

    for index, value := range planets {

    planets[index] = value + " (updated)"}

Summary:

  • range in Go is a powerful construct for iterating over data structures like arrays, slices, maps, strings, and channels.
  • Arrays/Slices: Iterates over index-value pairs.
  • Maps: Iterates over key-value pairs.
  • Strings: Iterates over Unicode code points.
  • Channels: Iterates over values received from a channel.
  • Efficient Iteration: Use the blank identifier (_) when you don't need both index and value.

By using range, you can write clean and efficient loops in Go, making it easier to handle data structures and perform operations on their elements.

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql