Home > other >  Through two examples to introduce Golang For Range circulation principle (turn)
Through two examples to introduce Golang For Range circulation principle (turn)

Time:11-23

The following code is infinite loop?
Syntactic sugar
What's the problem with the following code?
Slice the For Range principle
Other syntactic sugar
The map
The channel
An array of
String
The following code is infinite loop?

Func main () {
V: int []={1, 2, 3}
For I: range v={
V=append (v, I)
}
}

Content of the above code to initialize a slice for 1, 2, 3, and then traverse the slice, then give the slice additional elements, as the traversal of the arrays v has also been gradually increase, so the for loop is an infinite loop?

Whether the answer, it will only through three times, the result of the v is [0, 1, 2], is not infinite loop, because for range to achieve with the syntactic sugar,

Syntactic sugar

Syntactic sugar (Syntactic sugar), also translated as icing syntax, was invented by the British computer scientist Peter redding, a term that refers to add some kind of computer language syntax, this syntax has no effect on the function of the language, but more convenient programmers use, Syntactic sugar to make the program more concise, more readable,
For slice for range, its underlying code is:

//for_temp:=range
//len_temp:=len (for_temp)
//for index_temp=0; Index_temp & lt; Len_temp; Index_temp + + {
//value_temp=for_temp [index_temp]
//index=index_temp
//value=https://bbs.csdn.net/topics/value_temp
//the original body
//}

As you can see, before the traversal access to the length of the section len_temp:=len (for_temp), traversing the number of times will not change with the change of the section, the code above nature won't be dead cycle,

What's the problem with the following code?

Slice: int []={0, 1, 2, 3}
MyMap:=make (map [int] * int)

For the index, value:=range slice {
FMT. Println (& amp; The index, & amp; Value)
MyMap [index]=& amp; The value
}
FMT. Println ("=====new map=====")
For k, v:=range myMap {
FMT. Printf (" % d=& gt; % d \ n ", k, v)
}

This is likely to encounter problems in the actual coding, circular section, the index and value address after been assigned at the beginning, or behind the address, save to slice value address myMap, such operation result is:

=====new map=====
0=& gt; 3
1=& gt; 3
2=& gt; 3
3=& gt; 3

Results exactly the same, is the value of the final traversal through the bottom of the code above to see, after the traversal of the value assigned to the value, and in our case, will keep the address of the value to the value of myMap, this value is a (global variables), so after assigning the value myMap inside all the values are the value, so the structure is the same and it is the last value,

Note, here must be kept pointer will have a problem, if the save is value directly and copy for Golang is values, so the value will copy again to save again, in this case the result is correct,

Slice the For Range principle

To summarize, through the For Range traversal section, first of all, the calculation of times past slice (length); Each traversal, the current traverse to the value of the deposit to a global variable index,

Other syntactic sugar

In addition, For the Range not only support section, other syntactic sugar of the underlying code,

The map

//the Lower a for range over a map.
//The loop we generate:
//var hiter map_iteration_struct
//for mapiterinit (type, range, & amp; Hiter); Hiter key!=nil; Mapiternext (& amp; Hiter) {
//index_temp=* hiter. Key
//value_temp=* hiter. Val
//index=index_temp
//value=https://bbs.csdn.net/topics/value_temp
//the original body
//}

The channel

//the Lower a for range over a channel.
//The loop we generate:
//for {
//index_temp, ok_temp=& lt; - range
//if! Ok_temp {
//break
//}
//index=index_temp
//the original body
//}

An array of

//the Lower a for range over an array.
//The loop we generate:
//len_temp:=len (range)
//range_temp:=range
//for index_temp=0; Index_temp & lt; Len_temp; Index_temp + + {
//value_temp=range_temp [index_temp]
//index=index_temp
//value=https://bbs.csdn.net/topics/value_temp
//the original body
//}

String


//the Lower a for range over a string.
//The loop we generate:
//len_temp:=len (range)
//var next_index_temp int
//for index_temp=0; Index_temp & lt; Len_temp; Index_temp next_index_temp={
//value_temp=rune (range [index_temp])
//if value_temp & lt; Utf8. RuneSelf {
//next_index_temp=index_temp + 1
//} else {
//value_temp next_index_temp=decoderune (range, index_temp)
//}
//index=index_temp
//value=https://bbs.csdn.net/topics/value_temp
////the original body
//}
  • Related