Arrays

Arrays are the fixed-size foundation that slices (your next lesson) are built on. You'll learn to declare them, index them, and — crucially — understand that Go arrays are values : copying or passing one copies every element. That single fact explains a lot of Go behaviour.

Learn Arrays in our free Go course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick recall.

Part of the free Go course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn in This Lesson

1️⃣ Declaring & Indexing

An array's length is part of its type : [5]int and [3]int are different types that can't be mixed. Declared without values, every element starts at the element type's zero value. You index with square brackets, starting at 0 , and len() tells you the length (which never changes).

2️⃣ The [...] Form & Iterating

Typing the count by hand is error-prone, so Go lets you write [...] and it counts the literal for you. You can also set elements by index inside a literal (handy for sparse data), and you iterate exactly like any collection — with range .

3️⃣ Arrays Are Values

This is the single most important thing about Go arrays. Assigning an array to another variable, or passing it to a function, copies every element . The original is untouched by changes to the copy. (Slices, next lesson, behave differently — they share underlying storage.) Because they're plain values, two arrays of the same type can be compared with == .

Your turn — declare a three-string array and fill the podium.

These lines declare an array and set three slots, but they're scrambled. Put them in order so the program prints [10 50 99] (assume a func main wraps them).

Why: the array must exist ( var a [3]int ) before any index can be assigned. The three assignments fill positions 0, 1 and 2 — they can be in any order among themselves, but all must precede the Println . The result is [10 50 99] .

Predict the output before you reveal the answer.

[0 0 0] — a fresh array fills with the element type's zero value, which is 0 for int.

{'a := [2]int ; b := a; b[0] = 9; fmt.Println(a[0])'}

1 — b := a copies the whole array, so changing b[0] leaves a untouched.

3 — [...] tells Go to count the three literal elements, so the length is 3.

Slices, almost always — they're flexible and resizable. Use a true array only when the length is genuinely fixed and known at compile time, or when you need value semantics and comparability.

Arrays are copied when passed, so the function edited a copy. Pass a slice of the array ( arr[:] ) or a pointer ( *[N]T ) to mutate the original.

No — an array's length is fixed forever. If you need to add elements, that's exactly what a slice is for (next lesson).

Loop through the readings and track the largest value you've seen. Start max at the first element. Write it yourself and match the example output.

Practice quiz

What is printed by: var scores [5]int; fmt.Println(scores)?

A declared array fills every element with the element type's zero value, which is 0 for int.

Given a := [3]int{1,2,3}; b := a; b[0] = 99 — what is a[0]?

  • 1
  • 99
  • 0
  • undefined

Answer: 1. Arrays are values: b := a copies every element, so changing b leaves a untouched. a[0] is still 1.

What does len([...]string{"a","b","c"}) return?

  • 0
  • 2
  • It is a compile error
  • 3

Answer: 3. The [...] form tells Go to count the literal's elements, so the length is 3.

Are [3]int and [5]int the same type?

  • Yes, both are int arrays
  • No — the length is part of the type
  • Only if they hold equal values
  • Only inside a function

Answer: No — the length is part of the type. An array's length is part of its type, so [3]int and [5]int are different, incompatible types.

What is the valid index range for a length-5 array?

  • 0 to 4
  • 1 to 5
  • 0 to 5
  • 1 to 4

Answer: 0 to 4. Indexes are 0-based, so a length-5 array runs from index 0 to 4. Index 5 is out of range.

What does [10]int{0: 100, 9: 55} produce?

Indexed literals set positions 0 and 9; the gaps fill with the zero value 0.

If you pass an array to a function and it modifies elements, what does the caller see afterward?

  • The changes — arrays are shared
  • No changes — the array was copied
  • A panic
  • Only the first element changes

Answer: No changes — the array was copied. Passing an array copies it, so the function mutates a local copy and the original is unchanged.

Can two arrays of the same type be compared with ==?

  • No, never
  • Only pointers can
  • Only with reflect
  • Yes, if same type and length

Answer: Yes, if same type and length. Arrays of the same type and length are comparable with ==, comparing element by element.

After summing [...]int{4,8,15,16,23,42} with range, what is the total?

  • 100
  • 108
  • 96
  • 120

Answer: 108. 4+8+15+16+23+42 = 108.

Why can a fixed array be used as a map key but a slice cannot?

  • Arrays are smaller
  • Maps require pointers
  • Arrays are comparable; slices are not
  • Slices are immutable

Answer: Arrays are comparable; slices are not. Map keys must be comparable. Arrays are comparable with ==, but slices are not, so slices can't be keys.