Bird
Raised Fist0
Goprogramming~10 mins

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

Choose your learning style10 modes available
Concept Flow - Accessing array elements
Start
↓
Define array
↓
Choose index
↓
Check index in range?
No→Error: 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.

Practice

(1/5)
1. What is the index of the first element in a Go array?
easy
A. 1
B. Depends on the array size
C. -1
D. 0

Solution

  1. Step 1: Understand Go array indexing

    In Go, arrays start indexing from 0, meaning the first element is at index 0.
  2. Step 2: Confirm the first element's index

    The first element is always accessed with index 0, not 1 or any other number.
  3. Final Answer:

    0 -> Option D
  4. Quick Check:

    First element index = 0 [OK]
Hint: Remember, Go arrays start counting at zero [OK]
Common Mistakes:
  • Thinking indexing starts at 1
  • Assuming negative indexes are valid
  • Believing index depends on array size
2. Which of the following is the correct way to access the third element of an array named arr in Go?
easy
A. arr[3]
B. arr[2]
C. arr(2)
D. arr{2}

Solution

  1. Step 1: Recall Go array indexing syntax

    Go uses square brackets with zero-based indexing to access elements, so the third element is at index 2.
  2. Step 2: Identify correct syntax

    arr[2] accesses the third element; arr[3] accesses the fourth, arr(2) and arr{2} are invalid syntax.
  3. Final Answer:

    arr[2] -> Option B
  4. Quick Check:

    Third element = arr[2] [OK]
Hint: Use square brackets and zero-based index [OK]
Common Mistakes:
  • Using parentheses instead of brackets
  • Using 1-based index
  • Using curly braces for access
3. What will be the output of this Go code?
arr := [4]int{10, 20, 30, 40}
fmt.Println(arr[1])
medium
A. 10
B. 40
C. 20
D. 30

Solution

  1. Step 1: Identify the array elements and their indexes

    The array is [10, 20, 30, 40], indexed 0 to 3. Index 1 corresponds to the second element, 20.
  2. Step 2: Determine the printed value

    arr[1] accesses the element 20, so fmt.Println prints 20.
  3. Final Answer:

    20 -> Option C
  4. Quick Check:

    arr[1] = 20 [OK]
Hint: Remember index 1 is second element [OK]
Common Mistakes:
  • Printing arr[0] instead
  • Confusing index 1 with 2
  • Expecting output as 30 or 40
4. What is wrong with this Go code snippet?
arr := [3]int{1, 2, 3}
fmt.Println(arr[3])
medium
A. Index 3 is out of bounds for the array
B. Array declaration syntax is incorrect
C. fmt.Println cannot print array elements
D. The array size should be 4

Solution

  1. Step 1: Check array size and valid indexes

    The array has size 3, so valid indexes are 0, 1, and 2 only.
  2. Step 2: Identify the error in accessing arr[3]

    Accessing arr[3] tries to get the fourth element, which does not exist, causing an out-of-bounds runtime error.
  3. Final Answer:

    Index 3 is out of bounds for the array -> Option A
  4. Quick Check:

    Valid indexes: 0 to 2 only [OK]
Hint: Array indexes go from 0 to length-1 [OK]
Common Mistakes:
  • Using index equal to array length
  • Thinking fmt.Println can't print elements
  • Assuming array size auto-expands
5. Given the array arr := [5]int{5, 10, 15, 20, 25}, which code snippet correctly sums the first three elements?
hard
A. sum := arr[0] + arr[1] + arr[2]
B. sum := arr[1] + arr[2] + arr[3]
C. sum := arr[0] + arr[2] + arr[3]
D. sum := arr[2] + arr[3] + arr[4]

Solution

  1. Step 1: Identify the first three elements

    The first three elements are at indexes 0, 1, and 2, which are 5, 10, and 15 respectively.
  2. Step 2: Check which option sums these elements

    sum := arr[0] + arr[1] + arr[2] sums arr[0], arr[1], and arr[2], which is correct. Other options include elements beyond the first three.
  3. Final Answer:

    sum := arr[0] + arr[1] + arr[2] -> Option A
  4. Quick Check:

    Sum first three = arr[0]+arr[1]+arr[2] [OK]
Hint: Sum elements at indexes 0, 1, and 2 for first three [OK]
Common Mistakes:
  • Starting sum at index 1
  • Skipping elements
  • Including elements beyond first three
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard