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
How to iterate over an Array using for loop?
Initialize values for specific array elements in Go
Declaring an Integer or String Array of Five Elements in Go
Copy an array by value and reference into another array
What is an Array in Go?
How to initialize an Array with an Array Literal in Go?
Golang check if array element exists
Tricks to filter array elements in Go
Initializing an Array with ellipses in Go