Golang program to iterate hash collection and print only values


In golang we can use hashmap and key functions to iterate hash collection. Hashmap is said to be one of the hash collections. In the hashmap the data is stored in key value pairs as we did below. In this program we will use three methods to iterate the hashmap and print its values.

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.

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 Iteration on Hashmap

In this method, we will create a hashmap using map literal, iterate the hashmap using for loop and print the values on every iteration.

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 map using map literal with keys of type strings of values of type int.

  • Step 3 − In the map create three key value pairs keys as val1, val2, val3 and values as 10,20 and 30.

  • Step 4 − In this step, iterate the loop over the map and print the values on the console.

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

Example

The following example shows Golang program to iterate hash collection and print only values using iteration on hashmap

package main

import "fmt"

func main() {
	
   hashmap := map[string]int{
      "val1": 10,        
      "val2": 20,
      "val3": 30,
   }
   fmt.Println("The hashmap is iterated as follows:")
   for _, value := range hashmap {  
      fmt.Println(value) 
   }
}

The hashmap is iterated as follows:
10
20
30

Using Values of Key

In this method, a hashmap will be created and a new slice named values will be created in which values of keys will be appended from the hashmap and the slice will be iterated to print the values on the console.

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 hashmap as similar to as we created in the first case using a map literal.

  • Step 3 − In this step, create values named slice using make function which is a built-in function in Golang and create it similar to the length of hashmap.

  • Step 4 − Then, Iterate the hashmap and append the values of the hashmap in the new slice values.

  • Step 5 − Then, iterate the slice in which values were appended and print each value on every iteration.

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

Example

The following example illustrates Golang program to iterate hash collection and print only values using values of key

package main

import "fmt"

func main() {
	
   hashmap := map[string]int{
      "val1": 10,
      "val2": 20,
      "val3": 30,
   }	
   values := make([]int, 0, len(hashmap))
   for _, value := range hashmap {
      values = append(values, value)
   }	
   fmt.Println("The hashmap is iterated as follows:")
   for _, value := range values {
      fmt.Println(value)
   }
}

Output

The hashmap is iterated as follows:
10
20
30

Using Keys

In this illustration, we will create a hashmap as we created in the last two examples but the difference here is we will create a keys named slice in which the hashmap keys will be added and then this slice will be iterated to print the values using indexing.

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 hashmap and add key value pairs in it similarly as done in examples above.

  • Step 3 − Create a keys named slice using make function with length similar to the hashmap.

  • Step 4 − Iterate through the hashmap to add keys of it in the keys slice created above.

  • Step 5 − In this step, sort the keys using Strings function from the sort package.

  • Step 6 − As the keys are not sorted, iterate through the slice and print the values of keys using indexing on the hashmap.

  • Step 7 − The Output is printed on the console using fmt.Println() function from the fmt package, where ln refers to new line.

Example

The following example demonstrates Golang program to iterate hash collection and print only values using keys

package main

import (
   "fmt"
   "sort"
)

func main() {
	
   hashmap := map[string]int{
      "val1": 10,
      "val2": 20,
      "val3": 30,
   }

   keys := make([]string, 0, len(hashmap))
   for key := range hashmap {
      keys = append(keys, key)
   }
	
   sort.Strings(keys)

   fmt.Println("The hashmap is iterated as follows:")
   for _, key := range keys {
      fmt.Println(hashmap[key])
   }
}

Output

The hashmap is iterated as follows:
10
20
30

Conclusion

We executed the program of iterating through the hash collection using three examples. In the first example we created a hashmap and iterated it to print the values of keys. In the second example we created values slice and in the third example we created a key slice to print the values.

Updated on: 03-Apr-2023

168 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements