Golang program to add items into the hash collection


In Golang, we can use a simple indexing method and slicing method to add items to the hash collections. A hash function is used for retrieving and storing the data. In this article are going to see two different examples where we are going to use the above-mentioned methods to add items to a hash collection in go programming language.

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

  • Import the required packages in the program

  • Create a main function

  • In the main create a hashmap and add items in the hashmap

  • Print the map on the console

Example 1

In this example, we will create a hashmap using make function in Golang and add values to that hashmap using indexing. We will print the hashmap on the console using fmt package.

//Golang program to add items into the hash collection
package main
   
//import fmt package
import "fmt"  
   
//Main function to execute the program
func main() {
   
   // create an empty map to hold key-value pairs
   hashmap := make(map[string]int)
   
   // add key-value pairs to the map
   hashmap["pencil"] = 10
   hashmap["pen"] = 20
   hashmap["scale"] = 15
   
   // print the map to see its contents
   fmt.Println("The map after values are added in it is presented as follows:")
   fmt.Println(hashmap)
}

Output

The map after values are added in it is presented as follows:
map[pen:20 pencil:10 scale:15]

Example 2

In this illustration, we will create a hashmap similar to what we created in last example and additionally we will create a slice using struct keyword from that slice we will add the key value pairs to the map using a for loop. The output will be a map printed using fmt package.

//Golang program to add items into the hash collection
package main
   
//import fmt package
import "fmt"   
   
//Main function to execute the program
func main() {
   
   // create an empty map using the make() function
   hashmap := make(map[string]int)
   
   // create a slice of key-value pairs to add to the map
   keyvaluepairs := []struct {
      key   string
      value int
   }{
      {"pencil", 10},
      {"pen", 20},
      {"scale", 15},
   }
   
   // loop over the slice and add the key-value pairs to the map
   for _, pair := range keyvaluepairs {
      hashmap[pair.key] = pair.value
   }
   
   // print the map to see its contents
   fmt.Println("The hashmap after data is added in it is presented as follows:")
   fmt.Println(hashmap)
}

Output

The hashmap after data is added in it is presented as follows:
map[pen:20 pencil:10 scale:15]

Conclusion

Using two examples, we executed the program of adding items into the hash collection. In the first example we used indexing to add items into the hashmap and then printed it on the console and then in the second example we used a slice to add values to the map.

Updated on: 27-Mar-2023

115 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements