In a Go program, you're iterating over a collection of items, and you want to execute a block of code for each item. Which loop would you use for this purpose?

  • do-while
  • for
  • range
  • while
The range loop in Go is specifically designed for iterating over elements in a collection, such as arrays, slices, maps, or channels. It simplifies the syntax and improves readability by abstracting away the complexity of managing loop variables and indices. By using the range keyword, you can iterate over each item in the collection directly, without worrying about index management or termination conditions. Therefore, when iterating over a collection of items in a Go program, the range loop is the most suitable choice.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *