To iterate over an array in Go, you can use a for
loop or range-based iteration.
In this example,
numbers
with some values.for
loop to iterate over each element of the array and print the elements.package main
import "fmt"
func main() {
numbers := []int{1, 2, 3, 4, 5}
for i := 0; i < len(numbers); i++ {
fmt.Println(numbers[i])
}
}
1 2 3 4 5
In this example,
fruits
with some values.package main
import "fmt"
func main() {
fruits := []string{"apple", "banana", "cherry"}
for _, fruit := range fruits {
fmt.Println(fruit)
}
}
apple banana cherry
In this tutorial, we learned How to Iterate Over an Array in Go language with well detailed examples.