SoFunction
Updated on 2025-03-05

GO range specific use

What can GO language for…range  do?
for…range  How to use?
What are the returns of for…range ? Can it be used for any data structure?
How can I deal with the return value of for…range  if it is not needed?
How is the data of for…range  passed?
Will there be any questions above when you just learned golang? It's actually very simple, let's share and practice it one by one

What can GO language for…range  do?

golang's for…range   is the syntax of go itself, which can be used to traverse data structures. There are the following data structures that can traverse

  • Slice slice
  • array array
  • map hash table
  • channel

for…range  How to use?

Let's take a look at how they can be used separately. For...range  is equivalent to an iterator that can traverse the keys/index and values ​​of the data structure.

array array

Initialize an array

Use for…range to traverse, which corresponds to index and value

func main() {
 myArray := [5]int{1, 2, 3, 4, 5}

 for i, v := range myArray {
  ("%d -- %d -- %p\n", i, v, &v)
 }
}

Slice slice

Initialize a slice
Use for…range to traverse, which corresponds to index and value

mySlice := []int{1, 2, 3, 4, 5}
 
for i, v := range mySlice {
    ("%d -- %d -- %p\n", i, v, &v)
}

map hash table

  • Initialize a map hash table
  • Use for…range to traverse, map corresponding key-value pairs
myMap := map[string]string{
    "name":  "xmt",
    "hobby": "program",
    "addr":  "mengli",
}
for k, v := range myMap {
    ("%s -- %s -- %p\n", k, v, &v)
}

channel

  • Create a channel that can buffer 10 int type data
  • Create a coroutine to write data into the channel specifically
  • The main coroutine traversal channel and reads data
package main

import "fmt"

var myCh = make(chan int, 10)

func writeCh() {
 for i := 0; i < 5; i++ {
  myCh <- i
 }
 close(myCh)

}
func main() {
 go writeCh()
 for {
  for data := range myCh {
   (data)
  }
  break
 }

}

What are the returns of for…range ? Can it be used for any data structure?

Not all data structures can use for…range. This method can be used in the following structures.

Return value 1 Return value 2 Data delivery
String index The value corresponding to the index Value pass
Array or slice index The value corresponding to the index Array: Value Passing
Slice: Reference pass
Hash table key The value corresponding to the key pointer
aisle Data in the channel pointer

How can I deal with the return value of for…range  if it is not needed?

I believe that all xdm who have written golang knows that go can use - for return values ​​that we don't need, so for...range. Of course, this can be done.

For example:

myMap := map[string]string{
    "name":  "xmt",
    "hobby": "program",
    "addr":  "mengli",
}
for _, v := range myMap {
    ("%s -- %p\n", v, &v)
}

How is the data of for…range  passed?
The data are all passed through copying, that is, they are all passed by values. However, due to the different data structures, there are still some differences between arrays and slices here.
In the previous article, we mentioned that slices correspond to data structures with 3 elements, one is cap, one is len, and the other is ptr, which points to an underlying array.

Slices are passed by reference, but when passing data, the variable slices are passed by value, but the underlying array it actually points to will not change.

Let's write a demo to check it out:

Our thinking is like this:

Iterate through an array/slice. When traversing the current value, modify the corresponding values ​​of the subsequent index. After all, check whether the actual data will be modified in the result. If it is modified, it is the slice that is passed by reference. If it is not modified, it is the array that is passed by value.

Effects of arrays

myArray := [5]int{1, 2, 3, 4, 5}
(myArray)

for i, v := range myArray {
    if  i == 0{
        myArray[2] = 888
    }
    ("%d -- %d -- %p\n", i, v, &myArray[i])
}

The effects are as follows:

go run
[1 2 3 4 5]
0 -- 1 -- 0x1189c120
1 -- 2 -- 0x1189c124
2 -- 3 -- 0x1189c128
3 -- 4 -- 0x1189c12c
4 -- 5 -- 0x1189c130

The effect of slices

mySlice := []int{1, 2, 3, 4, 5}
(mySlice)
for i, v := range mySlice {
    if i == 0{
        mySlice[2] = 888
    }
    ("%d -- %d -- %p\n", i, v, &mySlice[i])
}

The effects are as follows:

go run
[1 2 3 4 5]
0 -- 1 -- 0x1140e340
1 -- 2 -- 0x1140e344
2 -- 888 -- 0x1140e348
3 -- 4 -- 0x1140e34c
4 -- 5 -- 0x1140e350

Through the above cases, I believe I have a little clue in my heart

This is the end of this article about the specific use of GO range. For more related GO range content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!