Bird
Raised Fist0
Goprogramming~10 mins

Slice and array relationship in Go - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Slice and array relationship
Declare array with fixed size
↓
Create slice referencing array
↓
Modify slice element
↓
Array element changes too
↓
Modify array element
↓
Slice element changes too
↓
Slice length and capacity tracked
↓
Slice can be resliced within capacity
↓
End
This flow shows how a slice points to an underlying array, so changes in one reflect in the other, and how slice length and capacity control access.
Execution Sample
Go
arr := [5]int{10, 20, 30, 40, 50}
slice := arr[1:4]
slice[0] = 25
arr[2] = 35
Create an array and a slice from it, then modify elements via slice and array to see shared data.
Execution Table
StepOperationArray StateSlice State (elements)LengthCapacityNotes
1Declare array[10 20 30 40 50]N/AN/AN/AArray created with 5 elements
2Create slice arr[1:4][10 20 30 40 50][20 30 40]34Slice points to array elements 1 to 3
3Modify slice[0] = 25[10 25 30 40 50][25 30 40]34Change via slice updates array at index 1
4Modify arr[2] = 35[10 25 35 40 50][25 35 40]34Change via array updates slice element at index 1
5Reslice slice = slice[1:3][10 25 35 40 50][35 40]23Slice resliced within capacity
6Modify slice[1] = 45[10 25 35 45 50][35 45]23Change via slice updates array at index 3
7End[10 25 35 45 50][35 45]23Final states
💡 Execution ends after slice and array modifications demonstrating shared underlying data.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6Final
arr[10 20 30 40 50][10 20 30 40 50][10 25 30 40 50][10 25 35 40 50][10 25 35 40 50][10 25 35 45 50][10 25 35 45 50]
sliceN/A[20 30 40][25 30 40][25 35 40][35 40][35 45][35 45]
len(slice)N/A333222
cap(slice)N/A444333
Key Moments - 3 Insights
Why does changing slice[0] also change arr[1]?
Because the slice points to the same underlying array starting at arr[1], so modifying slice[0] changes arr[1] as shown in step 3 of the execution_table.
What happens if we reslice beyond the capacity?
You get a runtime panic. In step 5, we resliced within capacity (3), so it's safe. Trying beyond capacity would cause an error.
Does changing the array affect the slice?
Yes, because the slice references the array's memory. Step 4 shows arr[2] changed, and slice[1] reflects that change.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of arr after modifying slice[0]?
A[10 25 35 40 50]
B[10 25 30 40 50]
C[10 20 30 40 50]
D[25 20 30 40 50]
💡 Hint
Check the 'Array State' column at step 3 in execution_table.
At which step does the slice length change from 3 to 2?
AStep 6
BStep 4
CStep 5
DStep 7
💡 Hint
Look at the 'Length' column in execution_table between steps 4 and 6.
If we modify arr[3] after step 6, what slice element changes?
Aslice[1]
Bslice[0]
Cslice[2]
DNo slice element changes
💡 Hint
Refer to the slice's underlying array indices and capacity in variable_tracker.
Concept Snapshot
Array is fixed size; slice is a view into array.
Slice has length and capacity.
Changing slice elements changes array and vice versa.
Slice can be resliced within capacity.
Slices share underlying array memory.
Full Transcript
We start by declaring an array with 5 integers. Then we create a slice that references part of this array. When we change an element in the slice, the corresponding array element changes too because they share the same memory. Similarly, changing the array updates the slice. The slice has a length and capacity that control how much of the array it can access. We can reslice within the capacity to change the slice's view. This shows the close relationship between slices and arrays in Go.

Practice

(1/5)
1. In Go, what is the relationship between a slice and the underlying array it references?
easy
A. A slice is a completely independent copy of the array.
B. A slice is a view into an array and shares the same data.
C. A slice stores data separately and does not affect the array.
D. A slice can only reference arrays of fixed size 10.

Solution

  1. Step 1: Understand slice basics

    A slice in Go points to an underlying array and does not copy data.
  2. Step 2: Relationship between slice and array

    Changes to the slice affect the array and vice versa because they share the same data.
  3. Final Answer:

    A slice is a view into an array and shares the same data. -> Option B
  4. Quick Check:

    Slice shares array data = B [OK]
Hint: Remember: slices share data with arrays, not copy it [OK]
Common Mistakes:
  • Thinking slices copy array data
  • Believing slices are independent arrays
  • Assuming slices have fixed size
2. Which of the following is the correct way to create a slice from an array arr := [5]int{1,2,3,4,5} that includes elements 2, 3, and 4?
easy
A. slice := arr[1:4]
B. slice := arr[2:5]
C. slice := arr[0:3]
D. slice := arr[3:6]

Solution

  1. Step 1: Identify slice indices

    Slice syntax is arr[start:end], including start but excluding end index.
  2. Step 2: Select correct indices for elements 2,3,4

    Elements 2,3,4 are at indices 1,2,3, so slice is arr[1:4].
  3. Final Answer:

    slice := arr[1:4] -> Option A
  4. Quick Check:

    Slice indices 1 to 4 exclude 4 = D [OK]
Hint: Slice includes start index, excludes end index [OK]
Common Mistakes:
  • Using wrong start or end index
  • Trying to slice beyond array length
  • Confusing inclusive/exclusive slice bounds
3. What will be the output of the following Go code?
arr := [3]int{10, 20, 30}
slice := arr[:]
slice[1] = 99
fmt.Println(arr)
medium
A. [99 20 30]
B. [10 20 30]
C. [10 99 30]
D. [10 20 99]

Solution

  1. Step 1: Understand slice referencing

    slice := arr[:] creates a slice referencing the entire array.
  2. Step 2: Modify slice element

    slice[1] = 99 changes the second element of the slice and underlying array.
  3. Step 3: Print array

    Printing arr shows the updated array: [10 99 30].
  4. Final Answer:

    [10 99 30] -> Option C
  5. Quick Check:

    Slice change affects array = A [OK]
Hint: Changing slice changes array too [OK]
Common Mistakes:
  • Assuming array stays unchanged
  • Confusing slice copy with reference
  • Misreading slice syntax
4. Identify the error in the following Go code snippet:
arr := [4]int{1, 2, 3, 4}
slice := arr[1:3]
slice[2] = 10
fmt.Println(arr)
medium
A. No error, output is [1 2 10 4]
B. Syntax error in slice declaration
C. Array length mismatch error
D. Index out of range error when assigning to slice[2]

Solution

  1. Step 1: Check slice length

    slice := arr[1:3] includes elements at indices 1 and 2, length 2.
  2. Step 2: Accessing slice[2]

    slice[2] tries to access third element (index 2), which is out of range.
  3. Final Answer:

    Index out of range error when assigning to slice[2] -> Option D
  4. Quick Check:

    Slice length 2, index 2 invalid = C [OK]
Hint: Slice length limits valid indices from 0 to len-1 [OK]
Common Mistakes:
  • Assuming slice length equals array length
  • Using invalid slice indices
  • Ignoring runtime panics
5. Given the array arr := [5]int{1, 2, 3, 4, 5}, you create two slices:
slice1 := arr[1:4]
slice2 := arr[2:5]
If you set slice1[1] = 99, what will be the content of slice2?
hard
A. [99 4 5]
B. [3 99 5]
C. [3 4 5]
D. [2 99 5]

Solution

  1. Step 1: Identify slice1 and slice2 elements

    slice1 = arr[1:4] = elements at indices 1,2,3 -> [2,3,4]
    slice2 = arr[2:5] = elements at indices 2,3,4 -> [3,4,5]
  2. Step 2: Modify slice1[1]

    slice1[1] is element at arr index 2 (value 3), changed to 99.
    So arr becomes [1,2,99,4,5].
  3. Step 3: Check slice2 after modification

    slice2 references arr[2], arr[3], arr[4] -> [99,4,5].
  4. Final Answer:

    [99 4 5] -> Option A
  5. Quick Check:

    Shared array data means slice2 reflects change = B [OK]
Hint: Slices share array; changing one affects overlapping slices [OK]
Common Mistakes:
  • Mixing up slice indices and values
  • Forgetting slices share underlying array
  • Assuming slices are independent copies
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