Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money

GO for… What can range do?

The for… How to use range?

The for… What is the case with the return value of range, and can it be used with any data structure?

The for… What can be done with the return value of range if it is not needed?

The for… How is the range data passed?

Will the XDM who just learned Golang have the above question? In fact, it is very simple, we will share and practice one by one

The GOThe for... rangeWhat can be done?

Golang the for… Range is the syntax of GO itself, which can be used to traverse data structures. The following data structures can be traversed

  • Slice slice
  • An array of array
  • Map a hash table
  • The channel tunnel

The for... rangeHow to use it?

Let’s see how they can be used separately. Range is an iterator that iterates over the keys/indexes and values of a data structure

An array of array

  • Initialize an array
  • useThe for... rangeTraversal, corresponding to the index and value
func main(a) {
	myArray := [5]int{1.2.3.4.5}

	for i, v := range myArray {
		fmt.Printf("%d -- %d -- %p\n", i, v, &v)
	}
}
Copy the code

Slice slice

  • Initialize a slice
  • useThe for... rangeTraversal, corresponding to the index and value
mySlice := []int{1.2.3.4.5}
	
for i, v := range mySlice {
    fmt.Printf("%d -- %d -- %p\n", i, v, &v)
}
Copy the code

Map a hash table

  • Initialize a map hash table
  • useThe for... rangeTraversal, map corresponding key-value pairs
myMap := map[string]string{
    "name":  "xmt"."hobby": "program"."addr":  "mengli",}for k, v := range myMap {
    fmt.Printf("%s -- %s -- %p\n", k, v, &v)
}
Copy the code

The channel tunnel

  • Create a channel that can buffer up to 10 ints
  • Create a coroutine specifically to write data to a channel
  • The main coroutine traverses the channel, reading the data

package main

import "fmt"

var myCh = make(chan int.10)

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

}
func main(a) {
	go writeCh()
	for {
		for data := range myCh {
			fmt.Println(data)
		}
		break}}Copy the code

The for... rangeWhat is the case with the return value of, and can it be used with any data structure?

Not all data structures can use for… Range, the following structure can use this method

The return value of 1 The return value of 2 The data transfer
string The index The value corresponding to the index Value passed
Array or slice The index The value corresponding to the index Array: value passing

Slice: pass by reference
Hash table key The value of the key Pointer to the
channel Data in channel Pointer to the

The for... rangeIf not, what can I do about it?

Golang (XDM) : golang (XDM) : golang (XDM) : golang (XDM) : golang (XDM) : golang (XDM) : golang (XDM) Range can do the same, of course

Such as:

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

The for... rangeHow is the data transferred?

All of the data is passed by copy, so it’s all passed by value, but there are some differences between arrays and slicing because the data structure is different

In the previous article, we talked about slicing, which corresponds to a data structure with three elements, cap, len, and PTR, pointing to an underlying array

Slice is passed by reference, but when passing data, slice is passed by value, but the underlying array it actually points to remains the same

Let’s write a demo to check it out:

Here’s the idea:

Iterate over an array/slice, while iterate over the current value, modify the corresponding value of the index behind, after all traversal, check the result, whether the actual data will be modified, if modified, it is slice is passed reference, if not, it is array is passed value

Array effects

myArray := [5]int{1.2.3.4.5}
fmt.Println(myArray)

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

The effect is as follows:

go run main.go [1 2 3 4 5] 0 -- 1 -- 0x1189c120 1 -- 2 -- 0x1189c124 2 -- 3 -- 0x1189c128 3 -- 4 -- 0x1189c12c 4 -- 5 --  0x1189c130Copy the code

Effect of slice

mySlice := []int{1.2.3.4.5}
fmt.Println(mySlice)
for i, v := range mySlice {
    if i == 0{
        mySlice[2] = 888
    }
    fmt.Printf("%d -- %d -- %p\n", i, v, &mySlice[i])
}
Copy the code

The effect is as follows:

go run main.go
[1 2 3 4 5]
0 -- 1 -- 0x1140e340
1 -- 2 -- 0x1140e344
2 -- 888 -- 0x1140e348
3 -- 4 -- 0x1140e34c
4 -- 5 -- 0x1140e350
Copy the code

Through the above cases, I believe that the heart has a bit spectrum

Welcome to like, follow and favorites

Friends, your support and encouragement, I insist on sharing, improve the quality of the power

All right, that’s it for this time

Technology is open, our mentality, should be more open. Embrace change, live in the sun, and strive to move forward.

I am Nezha, welcome to like, see you next time ~