This is a creation in Article, where the information may have evolved or changed.
The range function is a magical and interesting built-in function that you can use to iterate through arrays, slices, and dictionaries.
When used to iterate over arrays and slices, the range function returns indexes and elements;
When used to traverse a dictionary, the range function returns the key and value of the dictionary.
<textarea wrap= "soft" class= "Crayon-plain print-no" data-settings= "DblClick" readonly style= "-MOZ-TAB-SIZE:4; -o-tab-size:4; -webkit-tab-size:4; Tab-size:4; FONT-SIZE:12PX!important; line-height:15px!important; " >package mainimport ("FMT") func main () {//Here we use range to calculate all the elements of a slice and//This method applies to the array also nums: = []int{2, 3, 4} Sum: = 0 for _, num: = range nums {sum + = num} fmt. Println ("sum:", sum)//range returns index and element values when iterating through arrays and slices//If we do not care about the index can use an underscore (_) to ignore this return value//Of course we sometimes need this index for I, Num: = range nums {if num = = 3 {fmt. Println ("Index:", I)}}//When using range to traverse the dictionary, return the key-value pairs. KVS: = map[string]string{"A": "Apple", "B": "Banana"} for K, V: = range KVS {fmt. Printf ("%s-%s\n", K, V)}//The range function is used to iterate through a string, returning a Unicode code point. The first return value is the index of the starting byte of each character, the second is the character code point,//Because the string of go is made up of bytes, and multiple bytes form a rune type character. For I, C: = Range "Go" {fmt. Println (i, c)}} </textarea>
123456789101112131415161718192021222324252627282930313233343536373839404142 |
Package MainImport ( "FMT")funcMain() { //Here we use range to calculate all the elements of a slice and //This method is also applicable to arrays Nums := []int{2, 3, 4} sum := 0 for _, Num := Range Nums { sum += Num } FMT.Println("sum:", sum) //Range returns index and element values when iterating through arrays and slices //If we don't care the index can use an underscore (_) to ignore this return value //Of course we sometimes need this index for I, Num := Range Nums { if Num == 3 { FMT.Println("Index:", I) } } returns a key-value pair when using range to traverse the dictionary. KVS := Map[string]string{"a": "Apple", "B": "Banana"} for k, v := Range KVS { FMT.Printf("%s-%s\n", k, v) } the//Range function returns a Unicode code point when it is used to traverse a string. The first return value is the index of the starting byte of each character, and the second is the character code point . //Because the string of go is made up of bytes, multiple bytes form a rune type character. for I, C := Range "Go" { FMT.Println(I, C) }} |
The output is:
<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">banana0 1031, Appleb,/usr/local/go/bin/go run/users/xiequan/mygo/src/golangdemo/main.gosum:9index:1a 111</textarea>
1234567 |
/usr/Local/Go/bin/GoRun /Users/Xiequan/Mygo/src/Golangdemo/Main.Gosum: 9Index: 1a - Appleb - Banana0 1031 111 |
Go Range Function Usage