0
0
Goprogramming~10 mins

Accessing array elements in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Accessing array elements
Start
Define array
Choose index
Check index in range?
NoError: index out of range
Yes
Access element at index
Use element (print, assign, etc.)
End
This flow shows how to access an element in an array by choosing an index, checking if it's valid, and then using the element.
Execution Sample
Go
package main
import "fmt"
func main() {
  arr := [3]int{10, 20, 30}
  fmt.Println(arr[1])
}
This code defines an array of 3 integers and prints the element at index 1.
Execution Table
StepActionIndexIndex Valid?Element AccessedOutput
1Define array---[10 20 30]
2Choose index1---
3Check index in range1Yes--
4Access element at index1Yes20-
5Print element1Yes2020
6End----
💡 Index 1 is within array bounds (0 to 2), so element 20 is accessed and printed.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
arrundefined[10 20 30][10 20 30][10 20 30][10 20 30]
indexundefinedundefined111
elementundefinedundefinedundefined2020
Key Moments - 3 Insights
Why do we get an error if the index is out of range?
Because arrays have fixed size, accessing an index outside 0 to length-1 is invalid. The execution_table row 3 shows the index check that must be 'Yes' to proceed.
Is the first element at index 0 or 1?
The first element is at index 0. The example accesses index 1 to get the second element, as shown in execution_table rows 2 and 4.
What happens if we try to access arr[3] in this example?
Since the array length is 3, valid indices are 0,1,2. Accessing arr[3] would fail the index check in row 3 and cause a runtime error.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'element' after step 4?
A10
B30
C20
Dundefined
💡 Hint
Check the 'Element Accessed' column at step 4 in the execution_table.
At which step does the program check if the index is valid?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column in execution_table for the index range check.
If the index was 3 instead of 1, what would happen according to the execution flow?
ARuntime error due to index out of range
BProgram would print 0
CElement 30 would be accessed
DProgram would print element at index 2
💡 Hint
Refer to the concept_flow and execution_table exit_note about index validity.
Concept Snapshot
Access array elements using arr[index]
Index must be between 0 and length-1
Accessing invalid index causes runtime error
Arrays have fixed size in Go
Use fmt.Println to display element
Full Transcript
This example shows how to access elements in a Go array. First, an array of three integers is defined. Then, an index is chosen. The program checks if the index is within the valid range. If yes, it accesses the element at that index and prints it. If the index is out of range, the program will cause a runtime error. The first element is at index 0, so arr[1] accesses the second element. This process is shown step-by-step in the execution table and variable tracker.