How to assign and access array element values in Go?


You access or assign the array elements by referring to the index number. The index is specified in square brackets.

Example

package main

import "fmt"

func main() {
	var theArray [3]string
	theArray[0] = "India"  // Assign a value to the first element
	theArray[1] = "Canada" // Assign a value to the second element
	theArray[2] = "Japan"  // Assign a value to the third element

	fmt.Println(theArray[0]) // Access the first element value
	fmt.Println(theArray[1]) // Access the second element valu
	fmt.Println(theArray[2]) // Access the third element valu
}

Output

India
Canada
Japan
Most Helpful This Week