Golang program to store items into the hash collection


In Golang, a Hashmap is a part of hash collection and it stores values in the form of key:value pairs. In this articlewe will store items in the hash collection using two different examples. In the very first example the indexing will be used to store items in the map and in the second example item struct will be used to store items.

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.

Algorithm

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

  • Create a hashmap using map literal and make function with keys of type string and their values be of type int.

  • Add the values to the hashmap using indexing such that pencil=10, pen=20 and scale=15.

  • After the values are added, print the hashmap on the console.

  • In this step, we will manipulate the hashmap by accessing the value of a key using indexing.

  • Then, we will update the value of a key again using indexing.

  • Then, we will delete a particular key from the map using delete function and print the updated map on the console.

  • The print statement is executed using Println() function from fmt package where ln means new line.

Example 1

In this example, we will create a hashmap using make built-in function then add desired values in the map using indexing. Here, we will use three values, and then print them on the terminal using fmt package and manipulate the hashmap using different operations on it. Let’s have a look at the code and the algorithm to see its practical working.

package main
   
import "fmt"
   
func main() {
   
   // Initialize an empty map with string keys and integer values
   hashmap := make(map[string]int)
   
   // Add some items to the map
   hashmap["pencil"] = 10
   hashmap["pen"] = 20
   hashmap["scale"] = 15
   
   // Print the entire map
   fmt.Println("The hashmap created above is: ")
   fmt.Println(hashmap)
   
   // Access a specific item by its key
   fmt.Println("The value of specific element from hashmap is:")
   fmt.Println(hashmap["pencil"])
   
   // Update the value of an existing item
   hashmap["scale"] = 20
   
   // Delete an item from the map
   delete(hashmap, "pencil")
   
   // Print the updated map
   fmt.Println("The hashmap after deleting an item from it is:")
   fmt.Println(hashmap)
}

Output

The hashmap created above is: 
map[pen:20 pencil:10 scale:15]
The value of specific element from hashmap is:
10
The hashmap after deleting an item from it is:
map[pen:20 scale:20]

Example 2

In this example, we will create a hashmap as we did in last example but here we will also create an item struct instance to create key:value pairs and then add those items in the hashmap and manipulate the hashmap. The output will be printed using fmt package. Let’s see the code and the algorithm.

package main

import "fmt"

// Define a struct to represent an item
type Item struct {
   Name     string
   Quantity int 
}

func main() {
   // Initialize an empty map with string keys and Item values
   hashmap := make(map[string]Item)
   
   // Create item instances and add them to hashmap
   pen := Item{Name: "pen", Quantity: 10}
   pencil := Item{Name: "pencil", Quantity: 20}
   registers := Item{Name: "registers", Quantity: 30}
   hashmap[pen.Name] = pen
   hashmap[pencil.Name] = pencil
   hashmap[registers.Name] = registers
   
   // Print the entire map
   fmt.Println("The entire map is presented as follows: ")
   fmt.Println(hashmap)
   
   // Access a specific item by its key
   fmt.Println("The value of the element is accessed as follows:")
   fmt.Println(hashmap["pencil"])
   
   // Update the value of an existing item
   pen.Quantity = 50
   hashmap[pen.Name] = pen
   
   // Delete an item from the map
   delete(hashmap, pencil.Name)
   
   // Print the updated map
   fmt.Println("The updated map after deleting the data item is:")
   fmt.Println(hashmap)
}

Output

The entire map is presented as follows: 
map[pen:{pen 10} pencil:{pencil 20} registers:{registers 30}]
The value of the element is accessed as follows:
{pencil 20}
The updated map after deleting the data item is:
map[pen:{pen 50} registers:{registers 30}]

Conclusion

We executed the program of storing items into the hash collection using two examples. In the first example we used indexing to store items and in the second example we used struct to store items.

Updated on: 27-Mar-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements