Skip to content
Preptima
mediumConceptCodingEntryMidSenior

What happens when you append to a slice, and when do two slices stop sharing memory?

Go slice append either extends the existing backing array or allocates a new one when capacity is exhausted. Bugs appear when two slices share the same array and one append mutates data the other still observes.

5 min readUpdated 2026-08-15

Answer

Short answer

A slice is a view over an array with length and capacity. append may reuse that array or allocate a new one, so code that keeps aliases to related slices must treat append as a possible shared-memory mutation.

Was this answer helpful?

Likely follow-ups

  • Why must you always assign the result of append back to a variable?
  • You pass a slice to a function and it appends to it. Why does the caller sometimes see the change and sometimes not?
  • How would you delete an element from the middle of a slice without leaking the pointer it held?
  • What does copy return, and how does it behave when the two slices overlap?

Related questions

Further reading