Home > Article > Backend Development > What is the structure of for range in go language?
"for range" is a unique iteration structure of the Go language, which can be used to traverse arrays, slices, strings, maps and channels. The syntax of "for range" is similar to the foreach structure. The general form is "for key, val := range coll {...}"; the parameter val is always a copy of the value of the corresponding index in the collection, so it is generally only read-only. , any modifications made to it will not affect the original values in the collection.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
The for range structure is a unique iteration structure of the Go language. It is very useful in many situations. for range can traverse arrays, slices, strings, maps and channels. for The range syntax is similar to the foreach statement in other languages. The general form is:
for key, val := range coll { ... }
It should be noted that val is always a copy of the value of the corresponding index in the collection, so it is generally only read-only. Any modifications it makes will not affect the original values in the collection. A string is a collection of Unicode-encoded characters (or runes), so it can also be used to iterate strings:
for pos, char := range str { ... }
Each rune character and index have a one-to-one correspondence in the for range loop , which can automatically identify Unicode-encoded characters according to UTF-8 rules.
The return values traversed through for range have certain rules:
Arrays, slices, and strings return indexes and values.
map returns keys and values.
Channel (channel) only returns the value in the channel.
Traverse arrays and slices - obtain index and value
In the traversal code, key and value represent the subscript and subscript of the slice respectively The value corresponding to the mark. The following code shows how to traverse the slice. The array is also traversed in a similar way:
for key, value := range []int{1, 2, 3, 4} { fmt.Printf("key:%d value:%d\n", key, value) }
The code output is as follows:
Traversal String - Obtain characters
Go language is similar to other languages. You can traverse the string through the combination of for range. During traversal, key and value represent the index and string of the string respectively. every character in .
The following code shows how to traverse a string:
var str = "hello 你好" for key, value := range str { fmt.Printf("key:%d value:0x%x\n", key, value) }
The code output is as follows:
The variable value in the code, the actual The type is rune type, and printing it in hexadecimal is the character encoding.
Traverse the map - obtain the key and value of the map
For the map type, when traversing for range, key and value represent the index key key and value of the map respectively. The values corresponding to the index are generally called key-value pairs of the map, because they appear in pairs. The following code demonstrates how to traverse the map.
m := map[string]int{ "hello": 100, "world": 200, } for key, value := range m { fmt.Println(key, value) }
The code output is as follows:
#Note:
When traversing the map, the key values output by the traversal are unordered. If Ordered key-value pair output is required, and the results need to be sorted.
【Related recommendations: Go video tutorial, Programming teaching】
The above is the detailed content of What is the structure of for range in go language?. For more information, please follow other related articles on the PHP Chinese website!