0
0
Goprogramming~10 mins

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

Choose your learning style9 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.