Golang program to get the hash collection values as an array


In Go programming language, a hash collection contains a hashmap which stores the values as key:value pairs for the efficient execution of the programs. In this article we will use two examples to get the hash collection values as an array. In the first example, we will create a slice and append the strings of map in that slice, in the second example we will hash the string and store it inside the map. In this way we will print 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 both the keys and values are of type string.

  • Make an empty array similar to the length of hashmap using make function of Golang.

  • Iterate the hashmap till its range and append the values of the map using append method.

  • Print the array values on the console using Println function from fmt package where ln means new line.

Example 1

In this example, we will create a hashmap using map literal and then array values in which the values of the hashmap will be appended using make function which is a built-in function in Golang. The values array will be printed on the console using fmt package. Let’s see the example to understand the process of working.

package main

import (
   "fmt"
)
   
func main() {
   hashmap := map[string]string{ //create a map 
      "item1": "value1",
      "item2": "value2",
      "item3": "value3",
   }
   
   values := make([]string, 0, len(hashmap))
   
   for _, v := range hashmap {
      values = append(values, v) //append the map strings in the slice
   }
   fmt.Println("The hash collection value as an array is printed as:")
   fmt.Println(values) //print the slice
}

Output

The hash collection value as an array is printed as:
[value1 value2 value3]

Example 2

In this example, we will create a slice of strings, then we will iterate those strings hash them and store them inside the new map. The map values will be converted to an array and printed on the console using the fmt package. Let’s see through the code and the algorithm to understand the process of execution.

package main

import (
   "crypto/sha256"
   "fmt"
)

func main() {
   
   // Create a slice of strings to hash
   data_val := []string{"a", "b", "c", "d"}
   
   // Create a map to store the hash values
   hashes := make(map[string][32]byte)
   
   //hash the string and store it inside the map
   for _, str := range data_val {
      hash := sha256.Sum256([]byte(str))
      hashes[str] = hash
   }
   
   // Convert the map values to an array and print it
   var arr [][32]byte
   for _, value := range hashes {
      arr = append(arr, value)
   }
   fmt.Println(arr)
}

Output

[[202 151 129 18 202 27 189 202 250 194 49 179 154 35 220 77 167 134 239 248 20 124 78 114 185 128 119 133 175 238 72 187] [62 35 232 22 0 57 89 74 51 137 79 101 100 225 177 52 139 189 122 0 136 212 44 74 203 115 238 174 213 156 0 157] [46 125 44 3 169 80 122 226 101 236 245 181 53 104 133 165 51 147 162 2 157 36 19 148 153 114 101 161 162 90 239 198] [24 172 62 115 67 240 22 137 12 81 14 147 249 53 38 17 105 217 227 245 101 67 100 41 131 15 175 9 52 244 248 228]]

Conclusion

We executed and compiled the program of getting the hash collection values as an array using the two examples. In the first example we created a slice and added the strings of the map inside the slice and in the second example we created a map, hashed the string and added those strings in the map then map values were converted to array. Hence, the program executed successfully.

Updated on: 27-Mar-2023

503 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements