Golang Program to Create a Simple Map


Maps in Go are powerful data structures that allow us to store and retrieve key−value pairs. They are useful for various applications, such as storing and manipulating data, implementing dictionaries, and performing efficient lookups. In this article, we will write a program to create a simple map in golanguage using two methods that include the literal method and the make() function method.

Algorithm

  • Declare a map variable using the desired key and value types.

  • Initialise the map using the literal notation by enclosing key−value pairs in curly braces {}.

  • Assign values to the respective keys using the colon ':' syntax.

  • Optionally, repeat steps 2 and 3 for additional key−value pairs.

  • The map is now created and ready for use.

Syntax

mapName := map[keyType]valueType{key1: value1, key2: value2, ...}

The syntax represents creating a map using the literal notation. We declare a map variable named "mapName" and specify the key and value types within square brackets. Inside the curly braces, we provide the key−value pairs separated by colons.

make(map[keyType]valueType)

The syntax creates a map using the make() function. We declare a map variable named "mapName" and assign the result of the make() function, specifying the key and value types as arguments. This method creates an empty map that we can later populate with key−value pairs.

Example

In this example we are going to create a simple map in golanguage, to store the names of fruits and their corresponding quantities. Using the literal method, we can declare and initialise a map named 'fruitMap' with the key type as string and the value type as int. We then populate the map with key−value pairs representing different fruits and their quantities. Finally, we can access and print the values from the map.

package main

import "fmt"

func main() {
	fruitMap := map[string]int{
		"apple":  5,
		"banana": 3,
		"orange": 2,
	}

	fmt.Println("Fruit Map:", fruitMap)
	fmt.Println("Quantity of Apples:", fruitMap["apple"])
	fmt.Println("Quantity of Bananas:", fruitMap["banana"])
}

Output

Fruit Map: map[apple:5 banana:3 orange:2]
Quantity of Apples: 5
Quantity of Bananas: 3

Example

In this example we are going to create a simple map in golanguage, to store the names of programming languages and their corresponding popularity ratings. Using the make() function method, we can declare and initialise a map named 'langMap' with the key type as string and the value type as float64. We then populate the map with key−value pairs representing different programming languages and their popularity ratings. Finally, we can access and print the values from the map.

package main

import "fmt"

func main() {
	langMap := make(map[string]float64)

	langMap["Go"] = 9.2
	langMap["Python"] = 9.8
	langMap["JavaScript"] = 10.0

	fmt.Println("Language Map:", langMap)
	fmt.Println("Popularity of Go:", langMap["Go"])
	fmt.Println("Popularity of Python:", langMap["Python"])
}

Output

Language Map: map[Go:9.2 JavaScript:10 Python:9.8]
Popularity of Go: 9.2
Popularity of Python: 9.8

Real Life Implementation

Geolocation Services

Maps are extensively used in geolocation services like Google Maps, Waze, and Apple Maps. These applications use maps to display locations, provide navigation, calculate distances, and offer real−time traffic updates. Maps help users find their way, discover nearby places, and plan routes efficiently.

User Authentication

In web development, maps can be used to store user authentication data. For example, a map can associate user IDs with their corresponding authentication tokens or session data. This allows websites to quickly verify user credentials during logins and manage user sessions securely.

Conclusion

Maps in go language are used to store and retrieve values based on keys that are unique. In this article we have looked at how we can create a simple map in golanguage, the literal method and the make() function method. These methods allow us to declare, initialise, and populate maps with key−value pairs efficiently. These methods can be very helpful in google maps, offer real time traffic on maps, as well as storing the user authentication data.

Updated on: 07-Sep-2023

76 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements