Go Program to Find the Length of a Map

Introduction

In Go, maps are collections of key-value pairs where each key is unique. You can find the length of a map, which is the number of key-value pairs it contains, using the len function. This guide will demonstrate how to find the length of a map in Go.

Problem Statement

Create a Go program that:

  • Declares and initializes a map with some key-value pairs.
  • Finds and displays the length of the map.

Example:

  • Input: Map: {"Apple": 5, "Banana": 3, "Cherry": 7}
  • Output: Length of the map: 3

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 the Map: Use a map literal to create and initialize a map with some key-value pairs.
  4. Find the Length of the Map: Use the len function to find the number of key-value pairs in the map.
  5. Display the Length of the Map: Use fmt.Println to display the length of the map.

Go Program

package main

import "fmt"

/**
 * Go Program to Find the Length of a Map
 * Author: https://www.javaguides.net/
 */
func main() {
    // Step 1: Create and initialize the map
    fruitMap := map[string]int{
        "Apple":  5,
        "Banana": 3,
        "Cherry": 7,
    }

    // Step 2: Find the length of the map using the len function
    length := len(fruitMap)

    // Step 3: Display the length of the map
    fmt.Printf("Length of the map: %d\n", length)
}

Explanation

Step 1: Create and Initialize the Map

  • The map is created and initialized using a map literal. For example, fruitMap := map[string]int{"Apple": 5, "Banana": 3, "Cherry": 7} creates a map with three key-value pairs.

Step 2: Find the Length of the Map

  • The len function is used to find the number of key-value pairs in the map. For example, length := len(fruitMap) stores the length of the map in the length variable.

Step 3: Display the Length of the Map

  • The program prints the length of the map using fmt.Printf to display the result in a formatted manner.

Output Example

Example 1:

Length of the map: 3

Example 2:

If the map is empty:

fruitMap := map[string]int{}
length := len(fruitMap)
fmt.Printf("Length of the map: %d\n", length)

Output:

Length of the map: 0

Example 3:

With a larger map:

fruitMap := map[string]int{
    "Apple":  5,
    "Banana": 3,
    "Cherry": 7,
    "Mango":  10,
    "Orange": 8,
}
length := len(fruitMap)
fmt.Printf("Length of the map: %d\n", length)

Output:

Length of the map: 5

Conclusion

This Go program demonstrates how to find the length of a map using the len function. It covers basic programming concepts such as map creation, initialization, and length determination in Go. This example is useful for beginners learning Go programming and understanding how to manage and analyze the contents of a map.

Leave a Comment

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

Scroll to Top