OneBite.Dev - Coding blog in a bite size

loop object in Go

Code snippet on how to loop object in Go

  for key, value := range myMap {
      fmt.Printf("%s %s\n", key, value)
  }

This code allows looping through an object, in this case a map. A map is a type of data structure that stores key/value pairs.

The first line indicates that we are beginning a for loop and specifies the key (key) and the value (value) associated with the object (myMap) we will be looping through.

The second line contains the code that the loop executes each iteration: it uses the fmt package to print the key (key) and the value (value) stored for the current iteration.

Finally, the loop ends after iterating through all the key/value pairs stored in the map.

go