Use of the Go language map

Source: Internet
Author: User
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.








Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.