By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
An array [4]event has a fixed length that is part of its type, and copying the array copies every element. Slices are the everyday tool: a slice is a small header — pointer, length, capacity — that describes a window into a backing array. Copying a slice copies the header, so two slices can share the same elements. That aliasing is the source of both the performance and most of the bugs.
len is how many elements the window shows; cap is how far the window could grow before append must allocate a new backing array. make([]event, 0, 4) gives you length zero and room for four appends with no reallocation. When you know the final size — a batch of events, one element per input line — that capacity hint removes repeated copying.
The signature append(s, vs...) returns a slice, and you must assign the result. If the backing array has room, the returned header points at the same array; if not, append allocates a bigger one and copies. Code that ignores the return value compiles only because the compiler allows discarding the result — and then it silently loses the appended elements.
package main
import (
"fmt"
"slices"
)
type event struct {
Service string
Status int
}
func main() {
batch := make([]event, 0, 4)
fmt.Println("len", len(batch), "cap", cap(batch))
batch = append(batch, event{Service: "api", Status: 200})
batch = append(batch, event{Service: "worker", Status: 500})
more := []event{
{Service: "api", Status: 404},
{Service: "worker", Status: 200},
{Service: "billing", Status: 502},
}
batch = append(batch, more...)
fmt.Println("len", len(batch))
statuses := make([]int, 0, len(batch))
for _, e := range batch {
statuses = append(statuses, e.Status)
}
slices.Sort(statuses)
fmt.Println(statuses)
hasFailure := slices.ContainsFunc(batch, func(e event) bool {
return e.Status >= 500
})
fmt.Println("has failure", hasFailure)
head := batch[:2]
head[0].Service = "edge"
fmt.Println("alias", batch[0].Service)
snapshot := slices.Clone(batch)
snapshot[0].Service = "original"
fmt.Println("clone", batch[0].Service, snapshot[0].Service)
}The slices package (Go 1.21+) replaced most hand-written loops for searching, sorting, comparing and deleting. Prefer it to a home-made helper: it is tested, generic and readable. For byte slices, the bytes package mirrors the same operations. Reserve manual index loops for cases where you need the index for something other than element access.
| Need | Use | Trap |
|---|---|---|
| Grow a collection | append(s, v) | Assign the result; it may allocate |
| Reserve capacity | make([]T, 0, n) | Capacity is a hint, not a length |
| Copy a slice | slices.Clone(s) | copy(dst, src) needs a sized destination |
| Search | slices.Contains / slices.Index | It compares values, not pointers |
| Sort | slices.Sort / slices.SortFunc | Sorts in place and returns nothing |
| Remove elements | slices.DeleteFunc | It returns a shorter slice; use it |
Key takeaways
Arrays are fixed-size values; slices are three-word headers over a backing array.
len is the visible window, cap is room to grow before reallocation.
append returns a slice header — always assign it.
Slicing aliases memory; slices.Clone and copy are how you detach.
Reach for the slices package for searching, sorting, comparing and deleting.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.