In this example, we will show you how to iterate over an array in Go with an example.
With for loops, we can iterate over array elements in Go.
Go - Array Iteration Example
The example uses three for loop forms to iterate over an array of programming languages.
package main
import "fmt"
func main() {
progLangs := []string{ "C", "C++", "Java", "Python", "Go" }
for i := 0; i < len(progLangs); i++ {
fmt.Println(progLangs[i])
}
for idx, e := range progLangs {
fmt.Println(idx, "=>", e)
}
j := 0
for range progLangs {
fmt.Println(progLangs[j])
j++
}
}
Output:
C C++ Java Python Go 0 => C 1 => C++ 2 => Java 3 => Python 4 => Go C C++ Java Python Go
Free Spring Boot Tutorial - 5 Hours Full Course
Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course