0
0
Goprogramming~8 mins

Array length in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Array length
Declare array
Array created with fixed size
Use len(array) to get length
Length returned as integer
Use length in program
This flow shows how an array is declared with a fixed size, then the built-in len function returns its length as an integer.
Execution Sample
Go
package main
import "fmt"
func main() {
  arr := [5]int{1,2,3,4,5}
  fmt.Println(len(arr))
}
This code creates an array of 5 integers and prints its length using len.
Execution Table
StepActionArray Contentlen(arr) ValueOutput
1Declare arr as [5]int{1,2,3,4,5}[1 2 3 4 5]N/AN/A
2Call len(arr)[1 2 3 4 5]5N/A
3Print length[1 2 3 4 5]55
4Program ends[1 2 3 4 5]5N/A
💡 Program ends after printing the length 5.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
arrundefined[1 2 3 4 5][1 2 3 4 5][1 2 3 4 5]
len(arr)N/AN/A55
Key Moments - 2 Insights
Why does len(arr) return 5 even if we only use some elements?
len(arr) returns the fixed size of the array, not how many elements are 'used'. See execution_table step 2 where len(arr) is 5 regardless.
Can the length of an array change after declaration?
No, arrays have fixed length in Go. The length is set when declared, as shown in step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of len(arr) at step 2?
A0
B5
CUndefined
D1
💡 Hint
Check the 'len(arr) Value' column at step 2 in execution_table.
At which step is the array declared with its values?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Action' column to find when arr is declared.
If the array was declared as [3]int{1,2,3}, what would len(arr) be at step 2?
A3
B5
C0
DUndefined
💡 Hint
len returns the declared size of the array, see variable_tracker for len(arr) values.
Concept Snapshot
Array length in Go:
- Arrays have fixed size declared at creation.
- Use len(array) to get the size.
- Length is an int representing total elements.
- Length does NOT change during program.
- Example: arr := [5]int{...}; len(arr) == 5
Full Transcript
This example shows how to get the length of an array in Go. First, an array named arr is declared with 5 integers. The len function returns the fixed size of the array, which is 5. The program prints this length. Arrays in Go have fixed size, so len(arr) always returns the declared size, not how many elements are used. The execution table traces each step: declaration, calling len, printing, and program end. The variable tracker shows arr contents and len(arr) value. Key moments clarify common confusions about fixed size and length meaning. The quiz tests understanding of these steps and values.