Maps (also called hashes or dicts in other languages) are one of Go’s built-in associative data types. A map maps keys to values. In that sense, a map stores key-value pairs.
The map data structure is used for fast lookups, retrieval, and deletion of data based on keys. It is one of the most widely-used data structures in computer science.
A map is declared using the following syntax:
var m map[KeyType]ValueType
m
: the name of the map variable.KeyType
(Optional): the data type of the keys in the map. This data type can also be declared at the time of initialization.ValueType
: the data type of the value in the key-value pairs in a map.Map types are reference types like pointers or slices; so, the value of the above m is nil as it doesn’t point to an initialized map. Any attempt to write to a nil map will cause a runtime panic.
var m = make(map[KeyType]ValueType)
The following line of code declares a map with String
keys that map to Integer
values.
var m map[string]int
To initialize a map, the following built-in make
function is required.
m = make(map[string]int)
package mainimport "fmt"func main() {// declaring a mapvar mymap map[string]int// initializing the mapmymap = make(map[string]int)if mymap == nil {fmt.Println("mymap is nil")} else {fmt.Println("mymap is not nil")}}
Since each key can map to, at most, one value, a map cannot contain identical keys.
Values can be inserted in the map using the following syntax:
mymap["Key"] = Value
This can be further understood using the code:
package mainimport "fmt"func main() {// declaring a mapvar mymap map[string]int// initializing the mapmymap = make(map[string]int)mymap["Pakistan"] = 92mymap["India"] = 91fmt.Println(mymap)}
Individual values in a map can be accessed using the following syntax:
val = mymap["Key"]
This can be further understood using the code:
package mainimport "fmt"func main() {// declaring a mapvar mymap map[string]int// initializing the mapmymap = make(map[string]int)mymap["Pakistan"] = 92mymap["India"] = 91// Accessing a specific value in the mapvar stored_value = mymap["Pakistan"]fmt.Println(stored_value)}
To delete values from a map, use the following syntax:
delete(map, key)
map
is the name of the map variable.key
is the value of the key-value pair we want to delete from the map.This can be further understood using the code:
package mainimport "fmt"func main() {// declaring a mapvar mymap map[string]int// initializing the mapmymap = make(map[string]int)mymap["Pakistan"] = 92mymap["India"] = 91fmt.Println("Before deletion, mymap:", mymap)delete(mymap, "Pakistan")fmt.Println("After deletion, mymap:", mymap)}
We can also iterate over a map using the range form of for-loop
. The range form gives you the key and value pair in every iteration:
package mainimport "fmt"func main() {// declaring a mapvar mymap map[string]int// initializing the mapmymap = make(map[string]int)mymap["Pakistan"] = 92mymap["India"] = 91mymap["USA"] = 1for country, code := range mymap {fmt.Println(country, code)}}
A map is an unordered collection; therefore, the iteration order of a map is not guaranteed to be the same every time you iterate over it.
Free Resources