Chapter 21: Go Slices

slices.

After we talked about arrays (fixed-size, rarely used directly), now we come to slices — the dynamic, flexible, everyday “list” type that almost everyone confuses with arrays at first… but once you understand the difference, slices become your best friend in 95%+ of list-related code.

Let’s go through everything about Go slices like we’re pair-programming together — theory, internals, declaration patterns, common operations, gotchas, real patterns, and many examples you can copy-paste right now.

1. Core Facts About Slices in Go

Property Explanation Consequence / Why it matters
Type notation []T (no size inside brackets) [5]int is array, []int is slice
Zero value nil Very useful — var s []int is nil
Reference type Slice is a descriptor (pointer + len + cap) — cheap to copy, shares underlying array Assigning slice = cheap, modifying affects original
Dynamic size Can grow with append, shrink with slicing Most flexible collection
Backed by array Every slice references some portion of an underlying array Understanding this prevents many bugs
len & cap len(s) = visible elements, cap(s) = underlying capacity until reallocation Key to performance
Most used collection Used for lists, queues, stacks, buffers, JSON arrays, HTTP params, database rows, etc. You will write []string, []int, []byte every day

Golden sentence to remember forever:

A slice is a window (view) into an underlying array. It has three fields internally:

  • pointer to the start of the window
  • length (how many elements you can see)
  • capacity (how far the window can grow without reallocation)

2. How to Create Slices (5 Most Common Ways)

Go

Typical output:

text

3. The Three Most Important Operations

a) append() — grows the slice (most used function)

Go

Important rules:

  • append may return a new slice header → always do s = append(s, …)
  • If cap is enough → no reallocation (fast)
  • If cap is full → new larger underlying array (usually ~double size)

b) Slicing — create new views (very cheap)

Go

Full slice expression [low:high:max] → controls new capacity (very useful to prevent accidental modification of tail).

c) copy() — copy elements between slices

Go

4. Classic Gotchas & Safety Tips

  1. Appending to slice from slice can share memory
Go

Fix: use full slice expression or copy

Go
  1. nil vs empty slice
Go

Both behave same in most operations (append, len, range), but nil is preferred when you mean “no value”.

5. Your Quick Practice Exercise (Try Now)

Go

Play with it:

  • Append more elements — watch cap grow
  • Slice with [low:high:high] syntax
  • Try append on sub-slice → see shared memory bug

Questions now?

  • Deep dive on slice internals (3-word structure)?
  • append growth strategy / amortized complexity?
  • 2D slices ([][]int)?
  • strings.Split, bytes.Split, JSON arrays?
  • Or next: structs or maps?

Slices are the most important composite type in everyday Go — once you feel comfortable with append, slicing, make, copy, and nil vs empty, you’ll write much safer & idiomatic code.

Keep running examples — you’re doing fantastic! 💪🇮🇳 Let’s keep going! 🚀

You may also like...

Leave a Reply

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