8.8 nil Slices vs Empty Slices

Right, let’s settle this. You’ve probably seen both nil and empty slices in the wild and maybe even used them interchangeably. That works… until it spectacularly doesn’t. The difference is one of the most beautifully subtle, yet profoundly important, distinctions in Go. It’s the difference between having absolutely nothing (nil), and having something that happens to contain nothing (an empty slice). Think of it this way: a nil slice is like having a blank check. You haven’t committed to any specific bank account (backing array), and the check’s “amount” field (length and capacity) is zero. An empty slice is like writing a check for $0.00 from your very real, but currently empty, checking account. The effect of trying to spend that money is the same (you get nothing), but the underlying financial reality is different.

8.7 Slice Gotchas: Sharing Backing Arrays and Unexpected Mutation

Right, let’s talk about the moment you accidentally become the villain in your own story. You change a slice, and suddenly, a completely different variable you have in another part of your code has also changed. You stare at the screen, convinced Go is broken, or perhaps reality itself. It’s not. It’s just slices being slices, and you’ve just been introduced to their shared-backing-array party trick. The root of all this chaos is simple: a slice is a descriptor. It’s a fancy data structure (a struct, under the hood) with a pointer to an underlying array, a length, and a capacity. The key word there is pointer. When you create a new slice from an existing one using a simple slice expression like sliceB := sliceA[1:4], you are not creating a new array. You are creating a new descriptor that points to the exact same block of memory that sliceA points to.

8.6 Three-Index Slicing: a[low:high:max] and Capacity Control

Alright, let’s talk about the three-index slice. You’ve probably been happily slicing away with a[low:high] and thinking that’s all there is to it. But Go, in its infinite wisdom (or perhaps its obsession with giving you just enough rope to hang yourself with, elegantly), offers a third index. It looks like this: a[low:high:max]. This isn’t just for show. It’s the scalpel to the [low:high] machete. It gives you precise, surgical control over the resulting slice’s capacity.

8.5 copy(): Moving Data Between Slices

Now, let’s talk about copy(), the workhorse function for moving data between slices when a simple assignment just won’t cut it. You use copy() for one simple reason: you want two separate, independent slices with the same underlying data. An assignment like slice2 := slice1 doesn’t do that; it just creates a new header pointing to the exact same array. Change an element in slice2, and boom, you’ve changed it in slice1 too. It’s a recipe for spooky action at a distance, and we don’t like that.

8.4 append(): Growing a Slice and the Backing Array

Alright, let’s get our hands dirty with append(). This is where the rubber meets the road and where most new Go developers get their first, confusing flat tire. The name makes it sound so simple: “just add this to the end.” And it is… until it isn’t. The magic—and the occasional horror—happens under the hood with the backing array. Think of a slice not as the data itself, but as a fancy struct with a pointer to an array, a length (how much of the array it’s using), and a capacity (how much of the array it could use). When you append, you’re asking to add an element to the end of the used portion. If there’s room in the capacity (len(s) < cap(s)), append just drops the new value in the next available slot, bumps the length, and hands you back the same slice, now with a new length. It’s fast and cheap.

8.3 Creating Slices: Literals, make(), and Slicing Arrays

Right, let’s get our hands dirty with the three main ways you conjure a slice into existence. This isn’t just about syntax; it’s about understanding what you’re actually asking the runtime to do for you under the hood. Each method has its own personality and its own performance implications. Slice Literals: The Quick and Easy This is the most straightforward way. You just declare what you want, and Go does the work.

8.2 Slice Header: Pointer, Length, and Capacity

Alright, let’s pull back the curtain on what a slice actually is. Because if you think it’s just a list of values, you’re in for a rude awakening the first time you modify a slice and some other, seemingly unrelated slice magically changes too. That’s not a bug; it’s you not understanding the slice header. A slice isn’t the data itself. It’s a glorified, three-field data structure that describes a contiguous section of an underlying array. I like to call this data structure the slice header. It’s the manager, not the worker. It contains:

8.1 Arrays: Fixed-Length, Value-Type Sequences

Let’s start with the humble array. It’s the fundamental building block, the simplest collection type Go has, and frankly, it’s a bit of a diva. It demands to know its exact size at compile time and throws a fit if you even think about changing it. This rigidity is its greatest strength and its most annoying weakness. An array isn’t just a reference to a sequence of values; it is the entire sequence. Think of it not as a pointer to a house, but as the entire, physical house itself. This has a crucial implication: assignment and passing to a function creates a full, deep copy of the entire data structure. This isn’t a “oh, I’ll just point to your data” situation. This is a “I’m renting a truck, moving every single one of your bricks to a new lot, and building an identical house” situation.

— joke —

...