Introduction to Maps in Go Programming

Maps In Go programming

Introduction

In this tutorial, you’ll understand map implementation of a hash table in Golang. So, a map is exactly a hash table. using the make() function to create a map.

So, we have the following example here.

var MapVal map[string][int]
MapVal = make(map[string]int)

We declaring MapVal as a variable, a map variable. So, var MapVal and it’s going to be a map into afterward the keyword map, we have in brackets the word string, that’s the type of the key, and then after that, we have another type and that’s the type of the value. So, the key and value can be different types.

Then, the line after that, first we’re declaring MapVal to be a variable then actually assigning it. pointing it to a map, making an actual map and I call to make for that. So, MapVal equals make and then parentheses map string int.

Now, another way to define a map is you can create a map with a map literal. So, we can initialize it with values, with key-value pairs rather.

 MapVal := map[string][int] { "John" 452}

So, here list a bunch of key-value pairs and values separated by colons. So, in this case, there is one pair but you can have a comma-separated list of key-value pairs.

Accessing maps

The way you would access an array except for the index that you use. that you put between the square brackets, that’s actually the key. So, if we want to read the value associated with the key John.

fmt.Println(MapVal["John"])

We can also add a key-value pair into the map, or change an existing one if it’s there.

MapVal["John"] = 258

Note, that would also change a key-value pair. So, let’s say there was already a key-value pair in there, John mapped to 258. Also, we can delete a key-value pair from the map without overwriting it. You can delete it completely by calling the delete function.

here, we call delete function and pass it, the first argument is the name of the map, so MapVal. The second argument is the key that you want to delete.

delete(MapVal, "John")

In this case, John and it will eliminate that key-value pair John and whatever its value is. It will eliminate that from the maps.

Two value assignment

In maps, we can have a two value assignment for a map.

id, S : MapVal["John"]

In this case, the second value, id comma S that S is going to be a Boolean. It’s going to be true when the key is present in the maps.

Iterating values with Maps

Basically, one thing which is very common to do is to iterate through an entire map, like iterating through an array, very common thing. You want to iterate through every key-value pair. Actually, the two value assignment with the range keyword. So, it’s a for a loop.

for key, val := range MapVal 
{
     fmt.Println(key, val)
}

In these examples, every pass, every iteration through the for-loop, will use one key and one value pair. So, key and value will be bound to a different key-value pair for every iteration to the for loop and you’ll iterate through them for a loop until you’ve gone through every key-value pair inside the MapVal.

Leave a Reply

Your email address will not be published. Required fields are marked *