Maps in Golang

A mapping or map represents a reference to a hash table, a data structure where each element represents a key-value pair. At the same time, each element has a unique key that can be used to get The value of the item. 
A mapping is defined as an object of type map[key]value, where key represents the key type and value represents the value type. Moreover, the key type must support the == comparison operation so that the image can match the value to one of the keys and the hash table.

Define a Map

For example, a mapping definition that has a string type as keys and an int type as values:

var authors map[string]int{} // here keys are of type string and values with type int

Setting the map values:

package main

import (
	"fmt")

func main() {
	var authors = map[string]int{
		"Mead":  1,
		"Locas": 2,
		"Marc":  3,
	}
	fmt.Println(authors)
}

As in an array or slicer, elements are placed in curly brackets. Each element represents a key-value pair. First, there is a key followed by a colon. The element definition ends with a comma.

The output will be:

map[Locas:2 Marc:3 Mead:1]

Accessing Map Items

To access the elements, you need to use the following keys (we call a value by adding its related key):

package main

import (
	"fmt"
)

func main() {
	var authors = map[string]int{
		"Mead":  1,
		"Locas": 2,
		"Marc":  3,
	}
	fmt.Println(authors["Mead"])
}

In this example we use the key "Mead" we should get as a result of the value 1.

Checking Existence

In Go, if we try to get a value with a key that is not defined in the map will get 0, and you can check this with the same example by changing "Mead" to any other key that isn't defined and as a result will get 0.

To check for the presence of an element for a specific key, you can use the if statement:

package main

import (
	"fmt"
)

func main() {
	var authors = map[string]int{
		"Mead":  1,
		"Locas": 2,
		"Marc":  3,
	}
	if v, k := authors["Mead"]; k{
		fmt.Println(v)
	}
}

If the value for the specified key is in the map, then the "k" variable will be true, and the "v" variable will store the resulting value. If the "k" variable is false, then there is no value for the key in the map.

In the example above will get the value 1 because the k "Mead" exists in the map.

To iterate over elements, use the for loop:

package main

import (
	"fmt"
)

func main() {
	var authors = map[string]int{
		"Mead":  1,
		"Locas": 2,
		"Marc":  3,
	}
	for k, v := range authors {
		fmt.Println(k, v)
	}
}

As output, we should get:

Mead 1
Locas 2
Marc 3

Add and Remove items

To add elements, you just need to set the value by the new key and the element with this key will be added to the collection:

package main

import (
	"fmt"
)

func main() {
	var authors = map[string]int{"Mead":  1,"Locas": 2,"Marc":  3,}
	authors ["Hanna"] = 200
	fmt.Println(authors["Hanna"])	
}

Here we just add new items with the key "Hanna" and if we run this example we should get the value 200.

To delete, the built-in delete(map, key) function is used, the first parameter of which is the map, and the second is the key by which it is necessary to Delete the item.

package main

import (
	"fmt"
)

func main() {
	var authors = map[string]int{"Mead":  1,"Locas": 2,"Marc":  3,}
	delete(authors, "Locas")
	fmt.Println(authors)	
}

As a result, we will get this output:

map[Marc:3 Mead:1]