Golang program to check a hash collection is empty or not


In this article, we will write a Go language programs to check a hash collection is empty or not. A hash collection is a data structure that contains key-value pairs in Go (Golang) and enables quick lookups, additions, and deletions. The value that corresponds to the key can be accessed by hashing the key into an index.

Hash collections in the form of maps have built-in support in Go. Here, a map is a reference type that is declared by using the map keyword, followed by the key type and value type in the format.

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 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.

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.

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 inside this function create a map named map_create using make function and map literal.

  • Step 3 − In this step, check if the length of the map created is equal to 0.

  • Step 4 − If the condition in step3 is satisfied print the map is empty otherwise print map is not empty use if-else condition.

  • Step 5 − The print statement is executed using fmt.Println() function where ln means new line.

Example 1

In this example, we will use len function to check if a map is empty or not. We will create a map using a make function which a type of built-in function. Then we will use len method over the map to get the Output.

package main

import "fmt"

func main() {
   map_create := make(map[string]int) 
   if len(map_create) == 0 {
      fmt.Println("Map is empty")
   } else {
      fmt.Println("Map is not empty")   
   }
}

Output

Map is empty

Example 2

In this example, we will use an external function which will return a Boolean value indicating whether the map is empty or not. In this case, the if-else condition will be used to see whether map is empty by manipulating the value returned from the function.

package main

import "fmt"

func is_map_empty(map_create map[string]int) bool {
   return len(map_create) == 0
}

func main() {
   map_create := make(map[string]int)

   if is_map_empty(map_create) {
      fmt.Println("Map is empty")
   } else {
      fmt.Println("Map is not empty")
   }
}

Output

Map is empty

Example 3

In this example, we will write a Golang program to check if a hash collection is empty or not using the empty flag where the default value will be set to true.

package main

import "fmt"

func main() {
   hashmap := make(map[string]string)	
   empty := true
   for key, _ := range hashmap {
      if key != "" {
         empty = false
         break
      }
   }

   if empty {
      fmt.Println("Hash is empty")
   } else {
      fmt.Println("Hash is not empty")
   }
   
   hashmap["key1"] = "value1"	
	empty = true
   for key, _ := range hashmap {
      if key != "" {
         empty = false
         break
      }
   }

   if empty {
      fmt.Println("Hash is empty")
   } else {
      fmt.Println("Hash is not empty")
   }
}

Output

Hash is empty
Hash is not empty

Conclusion

We executed the program of checking a hash collection is empty or not using three examples. In the first example we used len function to execute the program, in the second example we used an external function with Boolean as return type and in the third example we used empty flag to execute the program

Updated on: 03-Apr-2023

166 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements