Contributor : Sagar Singh
Map is a very important data structure in Golang used to store ,add and delete data in your program. Go provides a built-in type map that implements a hash table.
- Provides fast lookup
- Store key value pairs and does not allow duplicate key values
-
var map1 map[<key type>]<valuetype>
here map1 is the variable name that represents the memory adress where the map is stored.
eg: var map1 map[string]int
here the "key" type is string and the "value" type is integer. Map types are reference types like pointers and slice , so the above value of map1 is nil , It does not point to an initialized map. And writing attempt to a nil map will cause a runtime Panic, so don't do that.
Here is an example :
-
To initialize a map we use make function
map1 = make(map[string]int, 100)
Here , 100 is capacity of the map that the user has to set in order to initialize the map. But the initial capacity does not bound it's size , i.e. when more than a hundered elements will be added to the map, it will expand accordingly. This means that the map grows to accomodate the size of the items stored in it, with an exception of nil map because items can't be added in it.
To initialize a map with some data
map1 := map[string]int{ "yash": 1, }
To add a value in an initialized map
map1["sagar"] = 2
To retrieve data from a map we use keys to access the values , here the key "sagar" has the value 2 which is now stored in the variable "i"
i := map1["sagar"]
To delete data from a map we use the inbuilt delete function , here we want to remove the key value pair " sagar: 2 " from map1
delete(map1, "sagar")
To interate over the contents of a map, we use range keyword:
for key, value := range map1{
fmt.Println("key:", key,"value:",value)
}
if we don't want the key or the value we swap it with underscore or else we get error : declared but not used
for _, value := range map1{
fmt.Println("value:",value)
}
Here "i" is the variable that stores the value of the key "yash" , "ok " is of type bool that has value true if the key exists
i, ok := map1["yash"]
If we don't want to store the value we use an underscore(_)
_, ok := map1["yash"]
-
if a requested key is not present then we get the "value" type's zero value
j := map1["kunal"] //j == 0
kunal key does not exist so, we get zero value of type integer that is "0"
-
To find out the numer of elements in the map we use len function
k := len(map1) fmt.Println(k)
output
1
there is only one key value pair in map1 "yash: 1" , so we get k = 1