0Pricing
Go Academy · Lesson

Arrays: Fixed-Length Collections

Array literals, indexing, and length

Arrays: Fixed-Length Collections is a free Go Academy lesson on CoddyKit. This is lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, and your progress syncs across the web and the CoddyKit app. The Go Academy course includes 4 lessons in total.

What Is an Array?

An array in Go is a fixed-length sequence of elements of the same type. The length is part of the type: [3]int and [4]int are different types.

  • Arrays are value types — assigning copies the entire array
  • Length is always known at compile time
  • Rarely used directly; slices are preferred for most tasks

Array Literals

Declare and initialize arrays with a literal:

package main

func main() {
    primes := [5]int{2, 3, 5, 7, 11}
    names  := [3]string{"Alice", "Bob", "Carol"}
    flags  := [4]bool{true, false, true, false}
    _ = primes; _ = names; _ = flags
}

Array Length and Indexing

Use len() for the length and zero-based indexing to read/write elements:

package main
import "fmt"

func main() {
    scores := [3]int{90, 85, 92}
    fmt.Println(len(scores)) // 3
    fmt.Println(scores[0])   // 90
    scores[1] = 100
    fmt.Println(scores)      // [90 100 92]
}

Ellipsis in Array Literals

Use [...] to let the compiler count the elements for you:

package main
import "fmt"

func main() {
    days := [...]string{"Mon","Tue","Wed","Thu","Fri"}
    fmt.Println(len(days)) // 5
}

Arrays Are Value Types

When you assign or pass an array, Go copies the whole thing. Modifying the copy does not affect the original:

package main
import "fmt"

func main() {
    a := [3]int{1, 2, 3}
    b := a     // full copy
    b[0] = 99
    fmt.Println(a) // [1 2 3] — unchanged
    fmt.Println(b) // [99 2 3]
}

Comparing Arrays

Arrays of the same type and length can be compared with == and !=:

package main
import "fmt"

func main() {
    x := [3]int{1, 2, 3}
    y := [3]int{1, 2, 3}
    z := [3]int{1, 2, 4}
    fmt.Println(x == y) // true
    fmt.Println(x == z) // false
}

2D Arrays

Arrays can be nested to form grids or matrices:

package main
import "fmt"

func main() {
    grid := [2][3]int{
        {1, 2, 3},
        {4, 5, 6},
    }
    fmt.Println(grid[1][2]) // 6
}

Iterating an Array

Use a classic for loop or range to iterate:

package main
import "fmt"

func main() {
    nums := [4]int{10, 20, 30, 40}
    for i, v := range nums {
        fmt.Printf("[%d] = %d\n", i, v)
    }
}

Arrays vs Slices

The key differences:

  • Array: fixed size, value type, size is part of type
  • Slice: dynamic size, reference type, backed by an array
  • In practice, use slices unless you need a fixed compile-time size (e.g., SHA-256 hash [32]byte)

Common Pitfalls

Watch out for:

  • Index out of range — Go panics at runtime if i >= len(arr)
  • Passing large arrays to functions causes expensive copies — use pointers or slices instead
  • Forgetting that [3]int and [4]int are incompatible types

Quick Check

What is the length of [...]string{"a","b","c"}?

Recap: Arrays

Key takeaways:

  • Arrays have a fixed, compile-time length that is part of the type
  • They are value types — assignment copies all elements
  • Use len() and zero-based indexing
  • Prefer slices for most collection needs

Practice Prompt

Declare an array of 5 integers, assign the value 100 to the third element, then print the array using range.

Frequently Asked Questions

Is the “Arrays: Fixed-Length Collections” lesson free?

Yes — the full text of “Arrays: Fixed-Length Collections” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO. The Go Academy course includes 4 lessons in total.

What will I learn in “Arrays: Fixed-Length Collections”?

Array literals, indexing, and length You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Arrays: Fixed-Length Collections” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Arrays: Fixed-Length Collections
  2. Slices: Dynamic Lists
  3. Maps: Key-Value Stores
  4. Iterating Collections with range
← Back to Go Academy