To access array elements in Go using index, you can use square brackets notation.
In this example,
numbers
with integer values.2
using indexing numbers[1]
.package main
import "fmt"
func main() {
numbers := [5]int{1, 2, 3, 4, 5}
fmt.Println(numbers[1])
}
2
In this example,
fruits
with string values.package main
import "fmt"
func main() {
fruits := [3]string{"Apple", "Banana", "Orange"}
for i := 0; i < len(fruits); i++ {
fmt.Println(fruits[i])
}
}
Apple Banana Orange
In this tutorial, we learned How to Access Array Elements using Index in Go language with well detailed examples.