Go Program to Add Key-Value Pairs to a Map

Introduction

In Go, maps are collections of key-value pairs where each key is unique. You can dynamically add key-value pairs to a map, even after it has been created and initialized. This guide will demonstrate how to add key-value pairs to a map in Go.

Problem Statement

Create a Go program that:

  • Declares and initializes an empty map.
  • Adds key-value pairs to the map.
  • Displays the map after adding the key-value pairs.

Example:

  • Input: Key-Value pairs to add: {"Apple": 5, "Banana": 3, "Cherry": 7}
  • Output: Map content: {"Apple": 5, "Banana": 3, "Cherry": 7}

Solution Steps

  1. Import the fmt Package: Use import "fmt" for formatted I/O operations.
  2. Write the Main Function: Define the main function, which is the entry point of every Go program.
  3. Create and Initialize an Empty Map: Use the make function to create an empty map.
  4. Add Key-Value Pairs to the Map: Use the syntax map[key] = value to add key-value pairs to the map.
  5. Display the Map: Use fmt.Println to display the map after adding the key-value pairs.

Go Program

package main

import "fmt"

/**
 * Go Program to Add Key-Value Pairs to a Map
 * Author: https://www.javaguides.net/
 */
func main() {
    // Step 1: Create and initialize an empty map
    fruitMap := make(map[string]int)

    // Step 2: Add key-value pairs to the map
    fruitMap["Apple"] = 5
    fruitMap["Banana"] = 3
    fruitMap["Cherry"] = 7

    // Step 3: Display the map after adding the key-value pairs
    fmt.Println("Map content:", fruitMap)
}

Explanation

Step 1: Create and Initialize an Empty Map

  • The make function is used to create an empty map. For example, fruitMap := make(map[string]int) creates an empty map where the keys are strings representing fruit names, and the values are integers representing quantities.

Step 2: Add Key-Value Pairs to the Map

  • Key-value pairs are added to the map using the syntax map[key] = value. For example, fruitMap["Apple"] = 5 adds the key "Apple" with the value 5 to the map.

Step 3: Display the Map

  • The program prints the map using fmt.Println to display its contents after the key-value pairs have been added.

Output Example

Example 1:

Map content: map[Apple:5 Banana:3 Cherry:7]

Example 2:

Adding more key-value pairs:

fruitMap["Mango"] = 10
fruitMap["Orange"] = 8
fmt.Println("Map content:", fruitMap)

Output:

Map content: map[Apple:5 Banana:3 Cherry:7 Mango:10 Orange:8]

Example 3:

Starting with an empty map and adding a single key-value pair:

fruitMap := make(map[string]int)
fruitMap["Grape"] = 12
fmt.Println("Map content:", fruitMap)

Output:

Map content: map[Grape:12]

Conclusion

This Go program demonstrates how to add key-value pairs to a map dynamically using the map[key] = value syntax. It covers basic programming concepts such as map creation, initialization, and modification in Go. This example is useful for beginners learning Go programming and understanding how to manage and manipulate maps effectively.

Leave a Comment

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

Scroll to Top