2.38. Go language scope (Range)

发布时间 : 2025-10-25 13:32:59 UTC      

Page Views: 9 views

Go in the language range keyword is used for for iterate the array in the loop, slice , channel or collection ( map ). Itreturns the values corresponding to the index of the element in arrays and slices and in the collection key-value .

2.38.1. Example #

package main import "fmt" func main() { //This is how we use range to sum a slice. Using arrays is similar to this nums := []int{2, 3, 4} sum := 0 for \_, num := range nums { sum += num } fmt.Println("sum:", sum) //Using range on an array will pass in two variables: index and value. We did not need to use the ordinal of the element in the above example, so we omitted it with the blank character "_". Sometimes we do need to know its index. for i, num := range nums { if num == 3 { fmt.Println("index:", i) } } //range can also be used on key value pairs in maps. kvs := map[string]string{"a": "apple", "b": "banana"} for k, v := range kvs { fmt.Printf("%s -> %s\\n", k, v) } //Range can also be used to enumerate Unicode strings. The first parameter is the index of the character, and the second parameter is the character (Unicode value) itself. for i, c := range "go" { fmt.Println(i, c) } } 

The output of the above instance is as follows:

sum: 9 index: 1 a -> apple b -> banana 0 103 1 111 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。