Go language Map (collection)
Map is a collection of unordered key-value pairs. The most important point of MAP is to quickly retrieve the data by key, which is similar to the index, which points to the value of the data.
Map is a collection, so we can iterate over it like an iterative algebraic group and a slice. However, map is unordered, and we cannot determine the order in which it is returned because the map is implemented using a hash table.
Define Map
You can use the built-in function make or you can use the Map keyword to define a map:
Declare variable, default map is nil
var map_name = Map[type]type
Another way to create with make
Map_name: = Make (Map[type]type)
If you do not initialize the map, a nil map is created. Nil map cannot be used to store key value pairs
The following example demonstrates creating and using a map:
1 Package Main2 3 Import (4 "FMT"5 )6 7 Func Main () {8 //Create a map9Countrycapitalmap: = Make (map[string]string)Ten One //Insert the corresponding capital of each country Acountrycapitalmap["France"] ="Paris" -countrycapitalmap["Italy"] ="Rome" -countrycapitalmap["Japan"] ="Tokyo" thecountrycapitalmap["India"] ="New Delhi" - - //use key to output map values -Fmt. Println ("the first type of output") + forCountry: =Range countrycapitalmap{ -Fmt. Println (" Capital of", Country," is", Countrycapitalmap[country]) + } A at //Direct output of map key and value -Fmt. Println ("the second type of output") - forK,v: =Range countrycapitalmap{ -Fmt. Println (" Capital of"K" is", V) - } - in //To see if the element is in the map, the variable OK returns TRUE or false, and when the key of the query returns true in the map and captial gets the value in the map -Fmt. Println ("***************************************") toCaptial, OK: = countrycapitalmap["states"] + ifok{ -Fmt. Println (" Capital of", Captial," is", countrycapitalmap[captial]) the}Else{ *Fmt. Println ("Capital of states are not present") $ }Panax Notoginseng}
The above code executes the returned result:
first output way capital of France is Pariscapital of Italy is Romecapital of Japan is Tokyocapital of India is New Delhi the second output way capital of France is Pariscapital of Italy is Romecapital of Japan is Tokyocapital of India is New Delhi *************************************** capital of the states is not present
Delete () function
The delete () function is used to delete the elements of the collection, with the parameter map and its corresponding key. Examples are as follows:
Package Mainimport ("FMT") Func main () {//Create a mapCountrycapitalmap: = map[string]string{"France":"Paris","Italy":"Rome","Japan":"Tokyo","India":"New Delhi"} fmt. Println ("the original map") forK,v: =Range countrycapitalmap{FMT. Println (" Capital of"K" is", V)} //remove key and value from France in mapDelete (Countrycapitalmap,"France") fmt. Println ("the map after deletion") forK,v: =Range countrycapitalmap{FMT. Println (" Capital of"K" is", V)}}
The above code executes the returned result:
is are is is is Rome
Because of the characteristics of the map, the order is unordered, but I want to go to the wrong order of the map key to print how to do, look down!
1 Package Main2 3 Import (4 "FMT"5 )6 7 Func Main () {8 //Create a map9Countrycapitalmap: = map[string]string{"France":"Paris","Italy":"Rome","Japan":"Tokyo","India":"New Delhi"}TenCountry_array: = []string{"France","Italy","Japan","India"} One A //prints the information in the map in an orderly fashion based on the order of the custom array - for_,country: =Range country_array{ -Fmt. Printf ("Capital%v of are %v \ n", Country,countrycapitalmap[country]) the } -}
The above code executes the returned result:
Capital France of are Paris Capital Italy of are Rome capital Japan is Tokyo Capital India of is
Use of Go language map