Beginner Guide for Golang Maps by Example!

A map is a built-in data structure in Golang that stores a collection of key-value pairs. In this article, we will explore how to create, initialize, and manipulate maps in Go with examples.

Creating a Map

To create a map in Go, use the make() function with the map keyword and the types of key and value pairs. For example, to create a map that maps strings to integers, use the following code:

m := make(map[string]int)

This creates an empty map that maps strings to integers.

Initializing a Map

Maps can also be initialized with key-value pairs using a map literal. A map literal is a comma-separated list of key-value pairs enclosed in curly braces. For example, to initialize a map that maps strings to integers with some initial values, use the following code:

m := map[string]int{
    "apple":  1,
    "banana": 2,
    "orange": 3,
}

This creates a map with three key-value pairs.

Accessing Values in a Map

To access a value in a map, use the key as the index. For example, to get the value associated with the key "apple", use the following code:

value := m["apple"]

If the key is not present in the map, the value returned will be the zero value of the value type. For example, if we try to get the value associated with the key "mango", which is not present in the map, the value returned will be 0.

value := m["mango"]
fmt.Println(value) // Output: 0

To check if a key is present in a map, use the following code:

value, ok := m["apple"]
if ok {
    fmt.Println(value) // Output: 1
} else {
    fmt.Println("Key not found")
}

In this example, the variable ok is used to check if the key "apple" is present in the map. If the key is present, ok will be true, and the value associated with the key will be stored in the variable value.

Updating a Map

To update the value associated with a key in a map, simply assign a new value to the key. For example, to update the value associated with the key "apple" to 5, use the following code:

m["apple"] = 5

If the key is not present in the map, a new key-value pair will be added to the map.

Deleting a Key-Value Pair from a Map

To delete a key-value pair from a map, use the delete() function with the key as the argument. For example, to delete the key-value pair associated with the key "banana", use the following code:

delete(m, "banana")

Iterating Over a Map

To iterate over a map, use the range keyword with a for loop. The range keyword returns two values: the key and the value associated with the key. For example, to print all the key-value pairs in the map m, use the following code:

for key, value := range m {
    fmt.Printf("%s: %d\n", key, value)
}

In this example, we are using the Printf() function from the fmt package to print the key and value associated with each key-value pair in the map.

Conclusion

Maps are an essential part of the Go programming language. They are used to store key-value pairs and enable developers to build efficient data structures. In this article, we explored what maps are, how to create and use them, and some of their basic operations. With this knowledge, you can start using maps in your Go programs to store and retrieve data efficiently.

I hope this helps, you!!

More such articles:

https://medium.com/techwasti

https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA

https://www.techwasti.com/

\==========================**=========================

If this article adds any value to you then please clap and comment.

Let’s connect on Stackoverflow, LinkedIn, & Twitter.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!