{"id":2328,"date":"2020-06-20T22:32:35","date_gmt":"2020-06-20T17:02:35","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=2328"},"modified":"2020-06-20T22:33:37","modified_gmt":"2020-06-20T17:03:37","slug":"update-key-map-golang","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/update-key-map-golang\/","title":{"rendered":"Update a key in the map in Go (Golang)"},"content":{"rendered":"\n
When trying to add a key to the map which already exists, the new value will override the old value. This is analogous to updating a key in the map. <\/p>\n\n\n\n
Let’s see an example<\/p>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n \/\/Declare\n employeeSalary := make(map[string]int)\n\n \/\/Adding a key value\n fmt.Println(\"Before update\")\n employeeSalary[\"Tom\"] = 2000\n fmt.Println(employeeSalary)\n\n fmt.Println(\"After update\")\n employeeSalary[\"Tom\"] = 3000\n fmt.Println(employeeSalary)\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nBefore update\nmap[Tom:2000]\nAfter update\nmap[Tom:3000]<\/code><\/pre>\n\n\n\nIn the above program after writing the same key “Tom” <\/strong>with a new value of 3000 <\/strong>it overwrites the existing value of 2000. <\/strong>When we print the map again the value printed is 3000<\/p>\n","protected":false},"excerpt":{"rendered":"When trying to add a key to the map which already exists, the new value will override the old value. This is analogous to updating a key in the map. Let’s see…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[3,4],"class_list":["post-2328","post","type-post","status-publish","format-standard","hentry","category-tech","tag-go","tag-golang"],"yoast_head":"\n
Update a key in the map in Go (Golang) - Welcome To Golang By Example<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n