To access array elements in Go, you can use the indexing syntax with square brackets.
In this example,
numbers
with integer values.2
using the indexing syntax numbers[2]
.package main
import "fmt"
func main() {
numbers := []int{1, 2, 3, 4, 5}
element := numbers[2]
fmt.Printf("Element at index 2: %d\n", element)
}
Element at index 2: 3
In this example,
fruits
with string values.package main
import "fmt"
func main() {
fruits := []string{"Apple", "Banana", "Orange"}
for _, fruit := range fruits {
fmt.Println(fruit)
}
}
Apple Banana Orange
In this tutorial, we learned How to Access Array Elements in Go language with well detailed examples.