Golang program to get the size of the hash collection


In Golang we can use various internal functions or packages like len() function or reflect package, to get the size of the hash collection. A Hash collection contains a hashmap which is generally used to create key:value pairs and on the basis of that it shortens the execution time. In this program, we have used two examples to get the size of hash collection.

Syntax

func make ([] type, size, capacity)

The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments.

func range(variable)

The range function is used to iterate over any data type. To use this we first have to write the range keyword followed by the data type to which we want to iterate and as a result the loop will iterate till the last element of the variable.

func len(v Type) int

The len() function is used to get the length of a any parameter. It takes one parameter as the data type variable whose length we wish to find and returns the integer value which is the length of the variable.

Using Len Function

In this method, we will create a hashmap using the make function in Golang and then find the length of the map using len function, The length will be printed on the console using fmt package.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and Output.

  • Step 2 − Create a main function and in this function create a hashmap using make function which is an inbuilt function in Golang.

  • Step 3 − Assign the values to the keys(key1, key2, key3, key4) of hashmap.

  • Step 4 − Then, use len method to calculate the length of the hashmap.

  • Step 5 − Print the length of the map on the console using Println function from fmt package where ln implies new line.

Example

The following example demonstrates Golang program to get the size of the hash collection using len function

package main

import "fmt"

func main() {
   hashmap := make(map[string]int) 
   hashmap["key1"] = 10
   hashmap["key2"] = 20
   hashmap["key3"] = 30
   hashmap["key4"] = 40  

   fmt.Println("Size of map is:", len(hashmap)) 
}

Output

Size of map is: 4

Using the Size Variable

In this method, we will use size variable to calculate the size of hashmap. The hashmap will be created similarly like as it’s created in previous example. Then, it will be iterated and the size variable will be incremented on every iteration.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and Output.

  • Step 2 − in the main function create a hashmap using make function as did in last example.

  • Step 3 − Add the values to the key elements Such that, key1, key2, key3 and key4 are created.

  • Step 4 − Create a variable size which will tell us about the size of the hashmap.

  • Step 5 − Run a loop till the range of hashmap and in every iteration increment the size variable.

  • Step 6 − Finally, when the loop terminates print the size of the map using Println function where ln means new line.

Example

The following example illustrates Golang program to get the size of the hash collection using the size variable

package main

import "fmt"

func main() {
   hashmap := make(map[string]int) 
   hashmap["key1"] = 10
   hashmap["key2"] = 20
   hashmap["key3"] = 30
   hashmap["key4"] = 40

   var size int
   for range hashmap {  
      size++
   }

   fmt.Println("Size of map is:", size) 
}

Output

Size of map: 4

Using the reflect package

In this method, we will write a Golang program to get the size of hash collection using the ValueOf function from the reflect package.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and Output.

  • Step 2 − Create a main function in which further create a hashmap where keys are of type string and values are of type int

  • Step 3 − Then, obtain the size of the hash collection using the ValueOf function from the reflect package and Len method

  • Step 4 − Then, print the size of the hash collection using Println function fmt package where ln means new line

Example

The following example demonstrates Golang program to get the size of the hash collection using the reflect package

package main

import (
   "fmt"
   "reflect"
)

func main() {  
   hashmap := map[string]int{
      "pencil":  10,
      "pen": 20,
      "scale": 30,
   }   
   size := reflect.ValueOf(hash).Len() 
   fmt.Println("The size of hash collection is:", size)
}

Output

Found pen with value 10

Conclusion

We executed this program of getting the size of hash collection using two examples. In the first example we used len method to get the size of the map and in the second example we used a variable size to obtain the size of the map. Both the examples gave desired Output.

Updated on: 03-Apr-2023

215 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements