# An introduction to Maps in Go (GoLang)

Maps is an unordered collection in Go Language with the key and its associated value. They are good for looking up values quickly.

A few important points about the Maps in Go:

• A map is a data structure that gives you an unordered collection of the key/value pairs
• The Go Maps are used to look up a value by its associated key.
• A map is implemented with a hash table, that is providing quicker lookups on the information element and it is simple to retrieve a value by offering the key.
• Maps are unordered collections, and there is no way to predict the sequence in which the key/value pairs will be returned.
• That is why each iteration over a map could return a different sequence.
• The key type must have the operation == and! = defined, like string, int, float.
• Hence arrays, slices, and structs can’t be utilized as key type, but pointers and port types can.
• Structs can be applied as a key once we provide the Key() or Hash() method, to ensure a unique numeric or string key can be calculated in the struct’s fields.
• A map is a reference type
Further Reading:  Arrays in Go Language
##### How to declare a map

This is how a map can be declared generally:

var mp map[TyepOfKey]TypeOfValue

An example can be:

var empMap map[int]int

Assigning values

The maps are created with curly brackets, and they have keys and values.

var emp_salary = map[string]string{“Haynes”: “\$5000”, “Reema”: “\$5500”}

##### An example of Go map

In this example, we have created a map that stores employee names and their salaries for the demonstration.

```package main

import "fmt"

var emp_salary  = map[string]string{"Haynes": "\$5000", "Reema": "\$5500", "Arnonld": "\$5200"}

func main() {

fmt.Println(emp_salary )

}```

Output:

map[Haynes:\$5000 Reema:\$5500 Arnonld:\$5200]

##### The example of Insert items in the map

This example shows an alternative way of creating a map with key/value pairs. You can also see adding new key/value pairs below:

```package main

import "fmt"

func main() {

emp_salary := make(map[string]string)

fmt.Println("The map before insert\n", emp_salary)

emp_salary["Haynes"] = "\$5000"

emp_salary["Reema"] = "\$5500"

emp_salary["Arnonld"] = "\$5200"

fmt.Println("The map after \n", emp_salary)

}```

Output:

The map before insert

map[]

The map after

map[Haynes:\$5000 Reema:\$5500 Arnonld:\$5200]

Note: Creating a map with the make function is explained below.

Further Reading:  What is a pointer in Go Language?
##### An example of updating a map

Just extended the above example for updating a map:

```package main

import "fmt"

func main() {

emp_salary := make(map[string]string)

fmt.Println("The map before insert\n", emp_salary)

emp_salary["Haynes"] = "\$5000"

emp_salary["Reema"] = "\$5500"

emp_salary["Arnold"] = "\$5200"

fmt.Println("The map insert \n", emp_salary)

emp_salary["Reema"] = "5700"

fmt.Println("The map after update \n", emp_salary)

}```

Output:

The map before insert

map[]

The map insert

map[Arnonld:\$5200 Haynes:\$5000 Reema:\$5500]

The map after update

map[Haynes:\$5000 Reema:5700 Arnold:\$5200]

##### How to loop through a map

This is an important aspect; to iterate over a map using for loop in Go. For that, the range is also used as shown below:

```package main

import "fmt"

func main() {

//A map of employees and thier salary

empSal := map[string] int {"Mike":5000,"Shaggy":3000,"Nana":2000}

/* Displaying map using range*/

for emp := range empSal {

fmt.Println("The Salary of ",emp,"is",empSal[emp])

}

}```

Output:

The Salary of  Mike is 5000

The Salary of  Shaggy is 3000

The Salary of  Nana is 2000

##### An example of deleting items

The delete() function is used to delete an item from a map. It requires the map and the corresponding key that is to be deleted. See the example:

```package main

import "fmt"

func main() {

emp_salary := make(map[string]string)

fmt.Println("The map before insert\n", emp_salary)

emp_salary["Haynes"] = "\$5000"

emp_salary["Reema"] = "\$5500"

emp_salary["Arnold"] = "\$5200"

fmt.Println("The map after insert \n", emp_salary)

delete(emp_salary, "Arnonld")

fmt.Println("The map after delete \n", emp_salary)

}```

Further Reading:  The Variables in Go Programming

Output:

The map before insert

map[]

The map after insert

map[Reema:\$5500 Arnold:\$5200 Haynes:\$5000]

The map after delete

map[Haynes:\$5000 Reema:\$5500]

##### The example of accessing a specific element
```package main

import "fmt"

func main() {

emp_salary := make(map[string]string)

emp_salary["Haynes"] = "\$5000"

emp_salary["Reema"] = "\$5500"

emp_salary["Arnold"] = "\$5200"

fmt.Println("The Salary of Haynes = ", emp_salary["Haynes"])

}```

Output:

The Salary of Haynes =  \$5000

##### How to use make a function for map declaration

As shown in almost all the above examples, we used the make function for creating the maps. The make function takes an argument (which is a map) and it returns an initialized map.

For demos, see the above examples.

Categories Go