Looping with Golang’s Maps

We’ve so far discussed how you can declare and initialize maps in Golang. In this blog post, we discuss how you can use loops to implement your Golang maps.
 
We use loops in maps to initialize a block of several values where we want to simplify our Golang code. Have a look at the following sample code where the map has been initialized:

n: = make (map [int] int)
n [1] = 200
n [2] = 300
n [3] = 350
n [4] = 450
n [5] = 600

If we were to print the individual elements of such a map, we can either print each of elements or use for loop. Here’s how you can use the print fmt function to print the individual elements:

 
func main () {
n: = make (map [int] int)
n [1] = 200
n [2] = 300
n [3] = 350
n [4] = 450
n [5] = 600
fmt.Println (“the First element is:”, n[1])
fmt.Println (“Second element is:”, n[2])
fmt.Println (“Third element is:”, n[3])
fmt.Println (“the First element is:”, n[4])
fmt.Println (“Fifth element is:”, n[5])
}

Now, such a code is too bulky. You need for the loop to help you simplify it.
Here’s the syntax of for loop:

for [condition |  ( init; condition; increment ) | range]
{
   Golang statement(s);
}

What’s the significance of this syntax? Well, here’s what you should know about for loop syntax that you want to use with Golang maps:
If the condition is available, then the loop will execute as long as the condition remains true. If the for clause that is (init; condition; increment) here’s how the loop will be executed:

    • The init step will be executed first, and only once. You can declare and initialize any of your loop control variables. Program statements are placed in this section as long as the semicolon exists.
    • Next, the condition will be evaluated. If the condition is true, then the body of for loop will be executed. On the other hand, if the condition is false, the body of the loop will not run and flow of the program will jump to the next program statement just after the loop.
    • After the body of for loop has executed the flow of program will jump back up to the increment statement.
    • The condition of your loop will now be evaluated again. If it’s true, then the loop will execute, and the process repeats itself until the condition becomes false, after which for loop is terminated.
    • If the range is specified, then for loop will execute for each data item that’s in the range. Otherwise, if it’s not specified then your loop will execute in the normal way.

So, how can you modify the above code to use for loops? Here’s how your code should look like if you wish to use for loops in Golang maps:
Here’s an example of how you can use loops for your arrays in Golang:

func main () {
n: = make (map [int] int)
n [1] = 200
n [2] = 300
n [3] = 350
n [4] = 450
n [5] = 600
for count := 0; count < 5; count++ {
fmt.Println (“The element of the map is:”, n [count])
}

Here’s another example that illustrates how you can use loops in your maps:

import "sort"
var a map[int]string
var index []int
for i := range a {
index = append(index, i)
}
sort.ints (index)
for _, i: = range index {
fmt.Println ("The key that you wish to find is:", i, "and the value that you are looking for is :", a[i])
}

There you have it—you can now begin using loops to implement Golang’s maps. Keep on practicing to be perfect.

Online Technology Courses Stone River eLearning

GET YOUR FREE Golang EBOOK!

Comments