Chapter 23: Modify Slice

Modify slice

Modifying slices is where most beginners get surprised (and sometimes bitten) because slices behave very differently from arrays and from lists in Python/JavaScript. The key sentence you should remember forever:

Modifying a slice almost always modifies the underlying array — and therefore can affect other slices that share the same backing array.

Today I’ll explain every realistic way to modify a slice, with clear rules, visuals (text diagrams), examples you can run, common bugs + how to avoid them, and when to choose which technique.

1. The Four Main Ways to “Modify” a Slice

# Operation Syntax example Actually changes the slice header? Changes underlying array elements? Most common use-case Danger level
1 Assign to index s[3] = 99 No Yes Change single element Low
2 Append one or more elements s = append(s, 100, 200) Yes (new header possible) Yes (may reallocate) Grow the slice dynamically Medium
3 Reslice (change len/cap window) s = s[:5] or s = s[2:] Yes (new header) No Shrink / move window Low–Medium
4 Copy elements from another slice copy(dst, src) No (only dst elements) Yes (in dst’s backing array) Safe duplication / overwrite Low

2. Visual Understanding – The Slice Header

Every slice looks like this internally (3-word struct):

text

When you do s[3] = 99 → only changes array element When you do append → may change pointer + len + cap When you do s = s[:5] → changes len (and possibly cap)

3. Detailed Examples with Output & Explanation

Create modify_slice.go and run these one by one.

Go

4. The Most Important Rules to Remember

  1. s[i] = value → always modifies the underlying array → affects all slices that can see position i
  2. append(s, …)never do append without assigning back: s = append(s, …) → if cap is enough → modifies underlying array (may affect other slices) → if cap is full → allocates new larger array → old slices remain unchanged
  3. s = s[:n] or s = s[m:] → only changes the slice header (pointer/len/cap) → does not copy or allocate anything → very cheap → the underlying array stays the same
  4. copy(dst, src) → safest way to duplicate or transfer data without shared memory surprises → copies min(len(dst), len(src)) elements → dst must have enough length already (or make it first)

5. Common Bugs & How to Avoid Them

Bug 1: Append to sub-slice overwrites unrelated data

Go

Fixes:

Go

Bug 2: Forgetting to assign back append result

Go

Always: s = append(s, 100)

6. Your Quick Practice Task

Try to:

  1. Create slice [10,20,30,40,50,60]
  2. Make two overlapping views: first 4 and last 4
  3. Change element 3 through first view → see it change in second view
  4. Append 3 numbers to first view → check if original / second view changed
  5. Fix it so append doesn’t affect other views

Which technique did you use to make it safe?

Any part still fuzzy?

  • What exactly happens when append causes reallocation (growth factor)?
  • Slice aliasing bugs in loops?
  • append with … spread operator?
  • Or ready for maps or structs next?

Keep running and breaking these examples on purpose — that’s the fastest way to really understand slices. You’re making excellent progress — slices are the heart of most Go code you’ll write. 💪🇮🇳 Let’s keep going! 🚀

You may also like...

Leave a Reply

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