Chapter 20: Go Arrays

Arrays in Go are very different from what most beginners expect. They are almost never the first choice when you want a “list of things” — that role belongs to slices.

Let’s go through everything about Go arrays like we’re sitting together with VS Code open, typing and running code live.

1. Core Facts About Arrays in Go

Property Explanation Consequence / Gotcha
Fixed size The size is part of the type — [5]int is different type from [6]int Cannot grow or shrink after creation
Zero value All elements get their zero value (0, “”, false, nil, etc.) var a [10]int → all zeros
Value type Arrays are copied when assigned or passed to functions (not reference like slices) Big arrays = expensive copy
Contiguous memory Elements are stored in continuous memory blocks Very fast access (O(1))
Length len(a) is compile-time constant (fixed) No dynamic resizing
Most common usage Rare in everyday code — used mostly when size is known & fixed forever Statistics, fixed-size buffers, crypto keys, coordinates

Golden rule most people learn after 1–2 weeks:

In real Go code (2024–2026), you will use arrays directly < 5% of the time. 95%+ of the time when you want “a list”, you use slices ([]int, []string, etc.).

2. Declaration Styles (4 Main Ways)

Go

3. Very Important: Arrays Are Value Types (Copy on Assignment)

Go

Slices behave differently (reference type) — that’s why slices are used much more often.

4. Arrays as Function Parameters (Copy vs Pointer)

Go

To modify the original array → pass pointer:

Go

But again: most people just use slices instead — no need to pass pointers or worry about size.

5. Real-World (Rare) Use Cases for Arrays in 2026

Use case Why array instead of slice? Example type
Fixed-size cryptographic key / hash Size must be exactly 32 or 64 bytes [32]byte
RGB / RGBA color Always exactly 3 or 4 components [4]uint8
3D point / vector Always x,y,z (or x,y,z,w) [3]float64
Small fixed lookup table Performance + compile-time size [256]byte (ASCII map)
Matrix / small fixed grid Known dimensions at compile time [3][3]int
Interop with C code (cgo) C arrays have fixed size [64]byte

6. Quick Comparison: Array vs Slice (Very Important Table)

Feature Array [n]T Slice []T Winner in practice
Size in type Yes — part of type No Slice
Can grow / shrink No Yes (append) Slice
Zero value Zeroed elements nil Slice (more useful)
Copy on assignment Full copy Copy of descriptor (cheap) Slice
Pass to function Copy (expensive for large n) Cheap (pointer-like) Slice
len() at compile time? Yes No (runtime) Array (rare benefit)
Most idiomatic usage 2026 <5% >95% Slice

7. Your Quick Practice Exercise

Create arrays.go and try these:

Go

Now change it to slice ([]int) and see the difference.

Questions now?

  • Multidimensional arrays ([3][4]int)?
  • Why len(array) is compile-time constant?
  • How to convert array ↔ slice?
  • Or ready to move to slices next (the real star)?

Keep typing and running these — once you really feel the difference between array & slice, a huge part of Go will suddenly make sense. You’re doing great! 💪🇮🇳 Let’s keep going! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *