Golang program to iterate over a Slice


In this tutorial, we will iterate over a slice using different set of examples. A slice is a dynamic sequence which stores element of similar type. The iterated list will be printed on the console using fmt.Println() function.

Method 1:Using for Loop with Index

In this method,we will iterate over a slice using for loop where both the index and its element will be printed on the console screen. Let’s have a look how to execute this example.

Algorithm

Step 1 − Create a package main and import fmt package in the program.

Step 1 − Create a package main and import fmt package in the program.

Step 3 − Run a loop till the length of slice and print the index along with the element.

Step 4 − The output statement is executed using fmt.Println() function.

Example

Golang program to iterate over a slice using for loop with index

package main
import "fmt"

func main() {
   var slice_string = []string{"Chandigarh ", "Kanpur", "Gujarat", "Goa", "Jaipur"}
   
   fmt.Println("The iterated elements are:")
   for i, item := range slice_string {
      fmt.Println(i, "--", item) 
   }
}

Output

The iterated elements are:
0 -- Chandigarh 
1 -- Kanpur
2 -- Gujarat
3 -- Goa
4 -- Jaipur

Method 2:Using for Loop Without Index

In this method, we will see how to iterate over a slice using for loop without printing index. The output will be printed on the screen using fmt.Println() function.

Algorithm

Step 1 − Create a package main and import fmt package in the program.

Step 2 − Create a function main and further create a slice_string(string) in the function and assign values to it.

Step 3 − Run a loop till the length of slice with a blank identifier and print the element from the slice.

Step 4 − The output statement is executed using fmt.Println() function.

Example

Golang program to iterate over a slice using for loop without index

package main
import "fmt"

func main() {
   var slice_string = []string{"Chandigarh ", "Kanpur", "Gujarat", "Goa", "Jaipur"}
   
   fmt.Println("The iterated elements are:")
   for _, element := range slice_string {
      fmt.Println(element)
   }
}

Output

The iterated elements are:
Chandigarh 
Kanpur
Gujarat
Goa
Jaipur

Method 3: Using for Loop with Variable

In this method, we will see how to iterate over a slice using for loop without printing index. The output will be printed on the screen using fmt.Println() function.

Algorithm

Step 1 − Create a package main and import fmt package in the program.

Step 2 − Create a function main and further create a slice_string(string) in the function and assign values to it.

Step 3 − Create a variable j=0 and run a loop till the length of slice and print the element on every iteration.

Step 4 − Do j++ on every iteration until the loop is terminated.

Step 5 − The output is printed using fmt.Println() function.

Example

Golang program to iterate over a slice using for loop with variable

package main
import "fmt"

func main() {
   var slice_string = []string{"Chandigarh ", "Kanpur", "Gujarat", "Goa", "Jaipur"}
   fmt.Println("The iterated elements are:")
   j := 0
  
  for range slice_string {
      fmt.Println(slice_string[j])
      j++
   }
}

Output

The iterated elements are:
Chandigarh 
Kanpur
Gujarat
Goa
Jaipur

Conclusion

We executed the program of iterating the loop using three methods. In all the methods we used for loop differently, in the first case for loop is used with index, in the second case blank identifier is used, in the second case variable is used with the loop. Similar outputs are obtained in both cases. Hence program executed successfully.

Updated on: 13-Feb-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements