Golang program to create a hash collection


In this article, we will write a Golang programs to create hash collections with the help of make function and map function. Hashmap stores key value pairs that can be accessed via index. In this program we will create a hash collection with the help of two examples.

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.

Using Make Function

In this method, we will create a hashmap with the help of a make function and then add key value pairs to that hashmap. We will manipulate the hashmap in many ways, like printing the key value pairs or printing the entire map.

Algorithm

  • Step 1 − 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.

  • Step 2 − Create a main function and in that function create a hashmap using built-in make function in Golang.

  • Step 3 − In this step, add the values to the keys(pen,pencil and marker) in the hashmap.

  • Step 4 − Print the hashmap on the console using Println function from the fmt package.

  • Step 5 − Then, print the value of a key chosen from the key-value pairs created above similarly as done in step4.

  • Step 6 − Then, check if a particular key exists in the map or not using if-else condition print the key value pair if it exists else print no information is available for that key.

Example

Following example demonstrates golang program to create a hash collection using make function

package main

import "fmt"

func main() {
	
   hashmap := make(map[string]int)
   hashmap["pen"] = 26
   hashmap["pencil"] = 30
   hashmap["marker"] = 34

   fmt.Println(hashmap)	
   fmt.Println("The price of pencil is:", hashmap["pencil"])
	
   value, exists := hashmap["marker"]
   if exists {
      fmt.Println("The price of marker is:", value)
   } else {
      fmt.Println("There is no information about marker")
   }
}

Output

map[marker:34 pen:26 pencil:30]
The price of pencil is: 30
The price of marker is: 34

Using Map Literal

In this method, we will create the hashmap using map literal instead of make function. The other functions will be similarly executed as we did in last example.

Algorithm

  • Step 1 − 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.

  • Step 2 − Create a main function and in this function create a hashmap using the map literal and assign the values to the keys in the map.

  • Step 3 − In this step, print the map on the console using fmt.Println() function, here ln suggests new line.

  • Step 4 − Here, print a particular key value pair in order to manipulate the hashmap. In this case we have used pencil’s value to be printed on the console similar to step3.

  • Step 5 − In this case, check if the value of key exists in the map and use if-else condition to print the value if key exists in the map and print no information if no value exists.

Example

The following example demonstrates the golang program to create hash collection using map literal

package main

import "fmt"

func main() {
	
   hashmap := map[string]int{
      "pen":    24,
      "pencil": 30,
      "marker": 36,
   }
   fmt.Println(hashmap)	 
   fmt.Println("The price of pencil is:", hashmap["pencil"])

   value, exists := hashmap["marker"]
   if exists {
      fmt.Println("The price of marker is:", value)
   } else {
      fmt.Println("There is no information about marker")
   }
}

Output

map[marker:36 pen:24 pencil:30]
The price of pencil is: 30
The price of marker is: 36

Conclusion

We executed the program of creating a hash collection using two examples. Both the examples execute similarly the basic difference between both of them is in the first case the hashmap is created with the help of make function which is a built-in function in Golang and in the second case hashmap is created with the help of map literal.

Updated on: 03-Apr-2023

116 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements