Golang program to get keys from a hash collection


A hash collection is a data structure that contains key-value pairs in Go 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. In tis example, we will use two methods to get keys from a hash collection, in the first example we will use append method to get keys from the hashmap and in the second method we will make use of index variable. Let’s dive into the examples to see how we can do it.

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 append(slice, element_1, element_2…, element_N) []T

The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.

Algorithm

  • Import the required packages in the program

  • Create a main function

  • In the main with the help of internal functions obtain the key from the hash collections

  • Print the map’s keys on the terminal using fmt package

Example 1

In this example, we will create a hashmap and a keys slice. The hashmap will be iterated to add the keys to the keys slice and those keys will be printed on the console using fmt package’s Println() function. Let’s have a look at the code and the algorithm to see how the execution is taking place.

//Golang program to get keys from a hash collection
package main

import "fmt"

//Main function to execute the program
func main() {
   hashmap := map[string]int{  //create a hashmap using map literal
      "apple":  10,
      "mango":  20,
      "banana": 30, //assign the values to the key
   }
   
   keys := make([]string, 0, len(hashmap))  //create a keys slice similar to  the length of the hashmap
   
   for key := range hashmap {
      keys = append(keys, key)  //append the key from hashmap to keys
   }
   fmt.Println("The keys obtained here from hash collection are:")
   fmt.Println(keys) //print the slice on the console
}

Output

The keys obtained here from hash collection are:
[mango banana apple]

Example 2

In this method we will create a hashmap and obtain its keys in the keys slice using an additional index variable. The hashmap will be iterated and on each iteration the key in the hashmap will be added to the keys slice and it will be printed on the console using fmt package. Let’s go through the code and the algorithm to understand the concept.

//Golang program to get keys from a hash collection
package main

import "fmt"

//Main function to execute the program
func main() {
   hashmap := map[string]int{
      "apple":  10,
      "mango":  20,
      "banana": 30,
   }
   
   keys := make([]string, len(hashmap)) //create keys slice to store the keys of hashmap
   
   i := 0
   for key := range hashmap {
      keys[i] = key  //in the keys slice add the key on every iteration
      i++
   }
   fmt.Println("The keys obtained from the hash collection is:")
   fmt.Println(keys)  //print the keys on the console
}

Output

The keys obtained from the hash collection is:
[apple mango banana]

Conclusion

We executed the program of getting the keys from a hash collection using two examples. In the first example we used the append function which is a built-in function in Golang to add the keys from hashmap in keys named slice and in the second example to perform similar operation we used an index variable. Both the examples give as expected output.

Updated on: 27-Mar-2023

369 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements