Stage 1 · Code
Packages, Modules & Modern Collections
The slices and maps Packages
Replace hand-written collection loops with Go 1.26's type-safe standard-library helpers when the helper makes the intent clearer.
Why These Packages Exist
You already know how to search a slice with a loop and copy a map key by key. Those skills still matter. The slices and maps packages do not replace loops; they give common collection operations names, type safety, and implementations the whole Go ecosystem can recognize.
slices.Contains(tags, "urgent") tells a reviewer what the code wants immediately. A loop remains better when you need domain-specific validation, multiple outputs, early error handling, or careful allocation control.
Searching, Sorting, and Transforming Slices
| Operation | Helper | Important behavior |
|---|---|---|
| Find a value | slices.Contains / slices.Index | Index returns -1 when absent |
| Sort values | slices.Sort / slices.SortFunc | Mutates the supplied slice |
| Copy a slice | slices.Clone | Copies elements, not objects referenced by pointer fields |
| Remove a range | slices.Delete | Returns the shortened slice; assign the result |
| Remove adjacent duplicates | slices.Compact | Sort first if equal values are not already adjacent |
Cloning and Iterating Maps
In modern Go, maps.Keys and maps.Values return iterators rather than slices. The slices.Collect bridge makes allocation explicit when you genuinely need a materialized slice—for sorting, stable output, or reuse.
When a Plain Loop Is Better
A helper is not automatically clearer. If task validation must normalize text, reject empty tags, collect every invalid position, and stop on a size limit, one deliberate loop communicates that workflow better than a chain of helpers. Professional Go favors readable control flow over clever compression.
Cloning []*Task copies pointers, not the tasks they point to. Cloning map[string][]string copies slice headers, not each nested backing array. Use an explicit deep-copy function when nested mutable data must be isolated.
Apply It to tasker
Why collect and sort map keys before printing them in a CLI?
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.