Golang program to convert the hash collection into the array


In Go programming language, a hash collection contains a hashmap which holds values in form of key:value pairs. Here, in this particular program we will convert the map into array which is of fixed size and can be accessed via indexing. We will use two examples to execute the program. In the first example, we will use an index variable to add the values in the array and in the second example we will use an append method to add the values in the array.

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

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

  • Create a hashmap using map literal with the keys and values both of the type string.

  • In this step, create an array using make function (a built-in function in Golang) similar to the length of hashmap.

  • Create an I variable and initialize it with 0 then run a loop on the hashmap and in every iteration assign the array indexes the values from the hashmap.

  • Increment the ith variable in every iteration and after the loop is terminated print the array on the console.

  • The print statement is executed using Println() function from fmt package where ln means new line.

Example 1

In this example we will create a hashmap using map literal where both the keys and values are strings. Then an empty is array is created in which the values from the hashmap will be added using an index variable and then the array will be printed on the console using fmt package.

package main

import "fmt"

//Main function to execute the program
func main() {
   
   // create a hash collection
   hashmap := map[string]string{
      "item1": "value1",
      "item2": "value2",
      "item3": "value3",
   }
   
   // create an array to add the values from map
   array := make([]string, len(hashmap))
   
   // iterate over the keys of the hash collection and store the corresponding values in the array
   i := 0
   for _, value := range hashmap {
      array[i] = value
      i++
   }
   
   // print the array on the terminal
   fmt.Println("The hash collection conversion into array is shown as:")
   fmt.Println(array)
}

Output

The hash collection conversion into array is shown as:
[value1 value2 value3]

Example 2

In this illustration, create a hashmap as we did in last method and an array of type string to hold the map’s values, then iterate the map and add the values in the array using append method which is a built-in method in Golang.

package main

import "fmt"

//Main function to execute the program
func main() {
   
   // create a hash collection
   hashmap := map[string]string{
      "item1": "value1",
      "item2": "value2",
      "item3": "value3",
   }
   
   // create an empty array in which values will be added from hash collection
   array := []string{}
   
   // iterate over the keys of the hash collection and append the corresponding values to the array
   for key := range hashmap {
      value := hashmap[key]
      array = append(array, value)
   }
   
   // print the resulting array
   fmt.Println("The conversion of the hash collection into array is shown like:")
   fmt.Println(array)
}

Output

The conversion of the hash collection into array is shown like:
[value1 value2 value3]

Conclusion

We executed the program of converting the hash collection into the array using two examples. In both the examples we created empty array to hold the map’s values but in the first example we used an i variable and indexing to add values and in the second example we used the append method to add values in the array. Both the examples returned similar output.

Updated on: 27-Mar-2023

89 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements