Golang program to search an item in the hash collection


In this article, we will write Golang programs to search an item in the hash collection. A hashmap is part of hash collections. Here we are building a hashmap as set of key-alue pairs using Golang’s map which is constructed using map literal. We will execute this program using two examples.

Syntax

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.

Using Exists Keyword

In this method, we will use exists to check whether a particular item is present in the map or not. Here, we will check the key4 using indexing in hashmap that whether any value of it exists in the map or not.

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 that function create a hashmap using map literal where the keys are of type string and the values are of the type int.

  • Step 3 − Assign value to each key in the map.

  • Step 4 − Then, search any particular item in the hashmap using exists keyword, here we are searching for key4.

  • Step 5 − If the key exist in the map, its value will be printed on the console using Println() function from the fmt package.

  • Step 6 − If no key exists in the map the "The item does not exist in the hash collection." Will be printed similarly like in step5.

Example

The following example demonstrates Golang program to search an item in the hash collection using exists keyword

package main

import "fmt"

func main() {
   hashmap := map[string]int{  
      "key1": 10,
      "key2": 20,
      "key3": 30,
      "key4": 40,  
   }	
   value, exists := hashmap["key4"]  
   if exists {
      fmt.Println("The value of 'key4' is:", value)
   } else {
      fmt.Println("The item does not exist in the hash collection.")
   }
}

Output

The value of 'key4' is: 40

Using Found Flag

In this illustration, we will create hashmap similar to how we created in the last example, but here we will use a found flag and set its default value as false.

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 particular function create a hashmap similar to last example and assign values to the keys.

  • Step 3 − In this step, create a variable searchkey and store the particular key in the variable whose value is to be searched. In this case we have used key4.

  • Step 4 − Create a found named flag and assign it initial value as false.

  • Step 5 − Create another variable value of type int to store the value of the key.

  • Step 6 − Then, run a loop on the hashmap and on every iteration check if the searchkey and the key are equal to each other or not.

  • Step 7 − If they are equal to each other set the found as true, and the key’s original value to value variable.

  • Step 8 − Break the loop if the key is found.

  • Step 9 − After the loop is terminated check the value of found, if found is true, print the value of key on the console using Println function from fmt package.

  • Step 10 − If found=false, print a statement that the key is not found in hash collection.

Example

The following example illustrates Golang program to search an item in the hash collection using found flag

package main

import "fmt"

func main() {
   hashmap := map[string]int{ 
      "key1": 10,    
      "key2": 20,
      "key3": 30,
      "key4": 40,
   }
   searchKey := "key4"
   found := false            
   var value int                
   for key, val := range hashmap {
      if key == searchKey {
         found = true
         value = val
         break    
      }
   }

   if found {
      fmt.Println("The value of", searchKey, "is", value)
   } else {
      fmt.Println(searchKey, "not found in the hash collection.")
   }
}

Output

The value of key4 is 40

Using Formatting Identifier

In this method, we will write a Go program to search an item in the hash collection using formatting identifier.

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 create a map where keys are of type string and values are of type int

  • Step 3 − Set the key equal to a key in the map to search whether it exists in the map or not

  • Step 4 − Check the key in the map using indexing and also use a found flag with it

  • Step 5 − Then, use if-else conditional statement to check if found is true this implies that the key element is present in the map

  • Step 6 − But, if found is false the key is not present in the map

  • Step 7 − The key and its value are printed on the console using formatting identifiers.

Example

The following example example how the develop Golang program to search an item in the hash collection using formatting identifier

package main

import "fmt"

func main() {

   values := map[string]int{
      "pen":    10,
      "pencil": 20,
      "scale":  30,
   }

   key := "pen"
   value, found := values[key]

   if found {
      fmt.Printf("Found %s with value %d\n", key, value)
   } else {
      fmt.Printf("%s not found\n", key)
   }
}

Output

Found pen with value 10

Conclusion

We executed the program of searching an item in hash collection using three different examples. In the first example we have used exists keyword to search an item in the hashmap, in the second example we have used found flag to perform the execution and in the third example, we used formatting identifiers to search the key in the map. Both the examples gave desired Output.

Updated on: 03-Apr-2023

157 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements