2.39. Go language Map (Collection)

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

Page Views: 9 views

Map is a collection of unordered key-value pairs. The most important point of Map is passing the key to retrieve data quickly key , similar to an index, points to the value of the data.

Map is a collection, so we can iterate over it like arrays and slices. But, Map is unordered, and we cannot determine the order in which it isreturned, because Map is to use the hash table to achieve.

2.39.1. Define Map #

You can use built-in functions make , you can also use the map keyword to define Map :

/* Declare variables, default map is nil */ var map_variable map[key_data_type]value_data_type /* Using the make function */ map_variable := make(map[key_data_type]value_data_type) 

If you don’t initialize map then a nil map . nil map cannot be used to store key-value pairs

2.39.2. Example #

The following example demonstrates the creation and use of map :

Example #

package main import "fmt" func main() { var countryCapitalMap map[string]string /*Create Collection */ countryCapitalMap = make(map[string]string) /* Insert key value pairs into the map, corresponding to the capital cities of each country */ countryCapitalMap [ "France" ] = "Paris" countryCapitalMap [ "Italy" ] = "Rome" countryCapitalMap [ "Japan" ] = "Tokyo" countryCapitalMap [ "India " ] = "New Delhi" /*Use the key to output map values */ for country := range countryCapitalMap { fmt.Println(country, "The capital is", countryCapitalMap [country]) } /*Check if the element exists in the collection */ capital, ok := countryCapitalMap [ "American" ] /*If it is determined to be true, then it exists; otherwise, it does not exist */ /*fmt.Println(capital) */ /*fmt.Println(ok) */ if (ok) { fmt.Println("The capital of America is", capital) } else { fmt.Println("The capital city of America does not exist") } } 

The running result of the above instance is as follows:

The capital of France is Paris Italy's capital is Rome The capital of Japan is Tokyo India's capital is New Delhi The capital city of America does not exist 

2.39.3. delete() function #

delete() function is used to delete the elements of the collection, and the argument is map and its corresponding key . Examples are as follows:

Example #

package main import "fmt" func main() { /* Create a map */ countryCapitalMap := map[string]string{"France": "Paris", "Italy": "Rome", "Japan": "Tokyo", "India": "New delhi"} fmt.Println("Original Map") /* Print Map */ for country := range countryCapitalMap { fmt.Println(country, "The capital is", countryCapitalMap [ country ]) } /*Delete Element*/ delete(countryCapitalMap, "France") fmt.Println("French entry deleted") fmt.Println("Map after deleting elements") /*Print Map*/ for country := range countryCapitalMap { fmt.Println(country, "The capital is", countryCapitalMap [ country ]) } } 

The running result of the above instance is as follows:

Original map The capital of India is New delhi The capital of France is Paris Italy's capital is Rome Japan's capital is Tokyo French entry deleted Map after deleting elements Italy's capital is Rome Japan's capital is Tokyo The capital of India is New delhi 
《地理信息系统原理、技术与方法》  97

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