<\/span><\/h2>\n\n\n\nOne of the most common way of creating a map is using the map literal:<\/p>\n\n\n\n
map[key_type]value_type{}<\/code><\/pre>\n\n\n\nAn example of above where key type is string and value type is integer<\/p>\n\n\n\n
employeeSalary := map[string]int{}<\/code><\/pre>\n\n\n\nA map can also be created with some key values initialized<\/p>\n\n\n\n
employeeSalary := map[string]int{\n\"John\": 1000\n\"Sam\": 2000\n}<\/code><\/pre>\n\n\n\nA new key-value pair can also be added to the map<\/p>\n\n\n\n
employeeSalary[\"Tom\"] = 2000<\/code><\/pre>\n\n\n\nLet’s see a program<\/p>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n \/\/Declare\n employeeSalary := map[string]int{}\n fmt.Println(employeeSalary)\n \n \/\/Intialize using map lieteral\n employeeSalary = map[string]int{\n \"John\": 1000,\n \"Sam\": 1200,\n }\n\n \/\/Adding a key value\n employeeSalary[\"Tom\"] = 2000\n fmt.Println(employeeSalary)\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nmap[]\nmap[John:1000 Sam:1200 Tom:2000]<\/code><\/pre>\n\n\n\nIn the above program, we created a map literal intialized with some values. Then we added another key-value pair in it. Then we printed it using fmt.Println which prints all the key-value pairs in format map[key:value key:value]<\/strong><\/p>\n\n\n\nA map can also be declared with var keyword, but it creates a nil map as default zero value of map is nil. Adding any key value pair to that map will cause a panic. Let’s see an example for this<\/p>\n\n\n\n
package main\n\nfunc main() {\n var employeeSalary map[string]int\n employeeSalary[\"Tom\"] = 2000\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\npanic: assignment to entry in nil map<\/code><\/pre>\n\n\n\nAbove program run into panic as the map is nil.<\/p>\n\n\n\n
One use case of having a map declared with var <\/strong>keyword is when an already existing map needs to be assigned to it or when we want to assign the result of a function.<\/p>\n\n\n\n<\/span>Using Make <\/strong><\/span><\/h2>\n\n\n\nThis <\/strong>is another way of creating the map. The builtin function make<\/strong> can be used to create a map. It returns an initialized map. Hence key-value pairs can be added to it.<\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n \/\/Declare\n employeeSalary := make(map[string]int)\n \/\/Adding a key value\n employeeSalary[\"Tom\"] = 2000\n fmt.Println(employeeSalary)\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nmap[Tom:2000]<\/code><\/pre>\n\n\n\nIn above program we created a map using make function. Then we added a key value pair in it. Then we printed it using fmt.Println<\/strong> which prints all the key value pairs.<\/p>\n\n\n\n<\/span>Map Operations<\/strong><\/span><\/h1>\n\n\n\nThe below operations are applicable for map<\/p>\n\n\n\n
- Add a key-value pair<\/li>
- Update a key<\/li>
- Get the value corresponding to a key<\/li>
- Delete a key-value pair<\/li>
- Check if a key exists<\/li><\/ul>\n\n\n\n
<\/span>Add a key value pair<\/strong><\/span><\/h2>\n\n\n\nBelow is the format for adding a key value pair to a map<\/p>\n\n\n\n
mapName[key] = value<\/code><\/pre>\n\n\n\nLet’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 employeeSalary[\"Tom\"] = 2000\n fmt.Pr<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nmap[Tom:2000]<\/code><\/pre>\n\n\n\nAlso note that adding to a nil map will cause a panic.<\/p>\n\n\n\n
<\/span>Update a key-value pair<\/strong><\/span><\/h2>\n\n\n\nWhen 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 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\n\n\n<\/span>Get the value corresponding to a key<\/strong><\/span><\/h2>\n\n\n\nBelow is the format for retrieving a value corresponding to a key<\/p>\n\n\n\n
val := mapName[key]<\/code><\/pre>\n\n\n\nLet’s see a program<\/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 employeeSalary[\"Tom\"] = 2000\n \n \/\/Retrieve a value \n salary := employeeSalary[\"Tom\"]\n fmt.Printf(\"Salary: %d\", salary)\n}<\/code><\/pre>\n\n\n\n<\/span>Delete a key value pair<\/strong><\/span><\/h2>\n\n\n\nBelow is the format for delete a value corresponding to a key<\/p>\n\n\n\n
delete(map_name, key)<\/code><\/pre>\n\n\n\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n \/\/Declare\n employeeSalary := make(map[string]int)\n\n \/\/Adding a key value\n fmt.Println(\"Adding key\")\n employeeSalary[\"Tom\"] = 2000\n fmt.Println(employeeSalary)\n\n fmt.Println(\"\\nDeleting key\")\n delete(employeeSalary, \"Tom\")\n fmt.Println(employeeSalary)\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nAdding key\nmap[Tom:2000]\n\nDeleting key\nmap[]<\/code><\/pre>\n\n\n\nIn above program we delete the key and when we print the map again, the key is not there.<\/p>\n\n\n\n
<\/span>Check if a key exists<\/strong><\/span><\/h2>\n\n\n\nBelow is the format to check if a key exist in the map<\/p>\n\n\n\n
val, ok := mapName[key]<\/code><\/pre>\n\n\n\nThere are two cases<\/p>\n\n\n\n
- If the key exists val <\/strong>variable be the value of the key in the map and ok <\/strong>variable will be true<\/li><\/ul>\n\n\n\n
- If the key doesn’t exist val<\/strong> variable will be default zero value of value type and ok <\/strong>variable will be false<\/li><\/ul>\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 employeeSalary[\"Tom\"] = 2000\n fmt.Println(\"Key exists case\")\n val, ok := employeeSalary[\"Tom\"]\n fmt.Printf(\"Val: %d, ok: %t\\n\", val, ok)\n fmt.Println(\"Key doesn't exists case\")\n\n val, ok = employeeSalary[\"Sam\"]\n fmt.Printf(\"Val: %d, ok: %t\\n\", val, ok)\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nKey exists case\nVal: 2000, ok: true\nKey doesn't exists case\nVal: 0, ok: false<\/code><\/pre>\n\n\n\nIn the above program when key exists then val variable is set to the actual value which is 2000 here and ok variable is true. When the key<\/strong> doesn’t exist, the val<\/strong> variable is set to 0 which is the default zero value of int and ok<\/strong> variable is false. This ok<\/strong> variable is the best way to check if the key exists in a map or not<\/p>\n\n\n\nIn case we only want to check if a key is present and val is not needed, then blank identifier i.e “_” can be used in place of val.<\/p>\n\n\n\n
_, ok = employeeSalary[\"Sam\"]<\/code><\/pre>\n\n\n\n<\/span>Functions on Maps<\/strong><\/span><\/h2>\n\n\n\nBelow is the builtin function which can be used on a map<\/p>\n\n\n\n
- len() function<\/li><\/ul>\n\n\n\n
len() function<\/strong><\/p>\n\n\n\nThe len()<\/strong> function can be used to get the length of the map which is number of key value pair present in the map. Below is the format for using this function on map.<\/p>\n\n\n\nlen(mapName)<\/code><\/pre>\n\n\n\nLet’s see a program<\/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 employeeSalary[\"Tom\"] = 2000\n employeeSalary[\"Sam\"] = 1200\n\n lenOfMap := len(employeeSalary)\n fmt.Println(lenOfMap)\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\n2<\/code><\/pre>\n\n\n\n<\/span>Zero Value<\/strong><\/span><\/h1>\n\n\n\nzero value of a map is nil. This is also proved when we declare a map using the var<\/strong> keyword. See below program.<\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n var employeeSalary map[string]int\n if employeeSalary == nil {\n fmt.Println(\"employeeSalary map is nil\")\n }\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nemployeeSalary map is nil<\/code><\/pre>\n\n\n\n<\/span>Maps are referenced data types<\/strong><\/span><\/h1>\n\n\n\nMap are reference data types. So on assigning one map to a new variable, then both variable refers to the same map. Any change in one of the map would reflect in other and vice versa.<\/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 employeeSalary[\"Tom\"] = 2000\n employeeSalary[\"Sam\"] = 1200\n\n eS := employeeSalary\n\n \/\/Change employeeSalary\n employeeSalary[\"John\"] = 3000\n fmt.Println(\"Changing employeeSalary Map\")\n fmt.Printf(\"employeeSalary: %v\\n\", employeeSalary)\n fmt.Printf(\"eS: %v\\n\", eS)\n\n \/\/Change eS\n employeeSalary[\"John\"] = 4000\n fmt.Println(\"\\nChanging eS Map\")\n fmt.Printf(\"employeeSalary: %v\\n\", employeeSalary)\n fmt.Printf(\"eS: %v\\n\", eS)\n}<\/code><\/pre>\n\n\n\nIn the <\/strong>above program, eS is a new map variable to which we assign the existing employeeSalary<\/strong> map.<\/p>\n\n\n\n- First, we add a new key in employeeSalary<\/strong> map. The change reflects both in employeeSalary<\/strong> and eS<\/strong> map<\/li><\/ul>\n\n\n\n