This is a creation in Article, where the information may have evolved or changed.
Use of the Go language map
The Go language map is similar to the functionality of the C + + language, but there are many differences. Map is often used in the development of large concurrent business of Go language, such as the broadcast of user information. Here's a look at the relevant usage code
Map traversal and deletion, adding p Ackage Main
Import(
"FMT"
)
Funcdelete_msg (name_agemap[string]int,key string) {
Delete(name_age,key)
}
FuncSearch (name_agemap[string]int){
forkey,value: =rangename_age {
FMT. Printf ("%s-%d\n",key,value)
}
}
FuncMain (){
name_age: =map[string]int{}
name_age["Bojie"]=
name_age["csdn"]=Ten
name_age["HG"]=
name_age["HK"]=
Search (name_age)
delete_msg (name_age,"Bojie")
FMT. Printf ("Delete Bojie \ n")
Search (name_age)
delete_msg (name_age,"Bojie")
FMT. Printf ("Remove Bojie again \ n")
Search (name_age)
FMT. Printf ("add HG keyword after \ n")
name_age["HG"]=101
Search (name_age)
}
The output is the following first click on the compiled record
bojie-21csdn-10hg-100hk-18 Delete Bojie after csdn-10hg-100hk-18 delete Bojie again after csdn-10hg-100hk-18 add HG keyword csdn-10hg-101hk-18
Click again to run the record after recompiling
hk-18bojie-21csdn-10hg-100 Delete Bojie after csdn-10hg-100hk-18 delete Bojie again after hk-18csdn-10hg-100 add HG keyword csdn-10hg-101hk-18
Found through the results 1.map Deleting a non-existent keyword does not produce an error. 2.map Insert Existing keyword will replace the original existing value, this and C + + STL map, Stl::map cannot insert, can insert the same is the Stl::set container. The 3.map traversal is traversed through the form of for key, value: = Range name_age. Deletion of 4.map deleted by delete 5. Yes, you will find that the order of each recompile open traversal is different. 6. In the delete function, we pass the map variable directly to delete operation, but found that the output of the result is correct, can be found that the map is as cheap as slice is passed.
* As for performance and Stl::map compared to how, can self-test, but you have to note that stl::map with the template, here also affect some speed.