The pit of the map in Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
<pre style= "margin-top:0px; margin-bottom:0px; " ><span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " ></span><pre name= "code" class= "plain" >package mainimport ("FMT") type Vertex struct {x, y int}var m map[st Ring]vertex = map[string]vertex{"Key1": Vertex{1, 2},//<---------note the comma "Key2" here: vertex{5, 6},//<--------- Again pay special attention to the comma here}func main () {//m["Key3"] = vertex{3, 4}//here in fact should make a judgment so modify it to this look if _, exists: = m["Key3"]; exists {//Determine the existence of this key in the map So is the modification operation m["Key3"] = Vertex{3, 4}} else {//To determine that the key does not exist in the map so it is added operation m["Key3"] = vertex{3, 4}}fmt. PRINTLN (m)//Output Map[key1:{1 2} key2:{5 6} key3:{3 4}]}
This map and Java compared to the wonderful, when the assignment, please pay attention to the two damned comma, at the same time to the map to append the key value pairs, directly write on it, although the understanding is very simple, but people are very uncomfortable ah, the ghost know this is inserted or modified, do not make it wrong, judge the insurance

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.