OneBite.Dev - Coding blog in a bite size

loop array in Go

Code snippet on how to loop array in Go

  package main

import (
	"fmt"
)

func main() {
	arr := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

	for i := 0; i < len(arr); i++ {
		fmt.Println(arr[i])
	}
}

This code loops through a list of numbers stored in an array—in this case, a list of the integers from 0 to 9. The array is named arr and is composed of 10 elements.

The for loop iterates from the first position of the array (0) to the last (9). It stores the position number in the variable i. Then, it prints the value of arr[i], which is the element in the array corresponding to the number stored in i.

go