Generic Stdlib: slices, maps and cmp
Go 1.21 brought generic helper packages that replace a lot of hand-written loops. You'll use slices (Contains, Sort, SortFunc, BinarySearch, Clone, Equal, Max/Min), maps (Keys, Values, Clone, Equal), and cmp (Compare, Or, Ordered) — and write your own generic function with a constraint.
Learn Generic Stdlib: slices, maps and cmp in our free Go course — an interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Go course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn in This Lesson
1️⃣ Searching and sorting with slices
The slices package gives you Contains , Index , Sort , BinarySearch , and Max / Min for any element type. Note BinarySearch requires a sorted slice.
2️⃣ Custom ordering with SortFunc and cmp
To sort structs or use multiple keys, slices.SortFunc takes a comparison returning <0 , 0 , or >0 . Build it from cmp.Compare and chain tiebreaks with cmp.Or .
3️⃣ The maps package
maps.Keys and maps.Values give you iterators over a map; collect them with slices.Collect and sort for deterministic output. maps.Clone and maps.Equal round it out.
4️⃣ Writing your own generic function
When the stdlib doesn't have what you need, write a generic function with a type parameter and a constraint . cmp.Ordered allows any type that supports < and > .
🎯 Your Turn
Use the slices package. Fill in the two blanks marked ___ to test membership and sort the words.
❌ Calling BinarySearch on an unsorted slice — wrong result.
✅ slices.Sort first, or use slices.Contains for unsorted data.
❌ Returning a bool from a SortFunc — it wants an int.
❌ Forgetting map order is random when printing keys.
✅ slices.Sort the collected keys for stable output.
❌ Mutating a slice you handed out — the caller's view changes too.
✅ Store or return a slices.Clone for an independent copy.
-1. It returns -1, 0, or +1 for less/equal/greater — exactly what SortFunc expects.
cmp.Ordered . Use comparable only when you need == / != rather than < / > .
Sort scores by points descending, breaking ties by player name ascending, using slices.SortFunc with cmp.Compare and cmp.Or .
Practice quiz
Which standard library package gained generic helpers in Go 1.21?
- slices
- container/list
- sort (only)
- reflect
Answer: slices. Go 1.21 promoted slices and maps (and added cmp) to the standard library as generic helper packages.
What does slices.Contains(s, v) return?
- the index of v
- a sorted copy
- a bool: whether v is present in s
- the length of s
Answer: a bool: whether v is present in s. slices.Contains reports whether the value v appears in the slice; slices.Index returns its position (or -1).
How do you sort a slice of ints in ascending order with the slices package?
- slices.Clone(s)
- slices.Sort(s)
- slices.Order(s)
- slices.Reverse(s)
Answer: slices.Sort(s). slices.Sort sorts a slice of an ordered type in place; slices.SortFunc takes a custom comparison.
What does slices.SortFunc require its comparison function to return?
- a bool
- the sorted slice
- an error
- a negative, zero, or positive int for less/equal/greater
Answer: a negative, zero, or positive int for less/equal/greater. SortFunc uses a cmp-style function returning <0, 0, or >0; cmp.Compare is the usual building block.
Which function requires the slice to already be sorted to work correctly?
- slices.BinarySearch
- slices.Clone
- slices.Equal
- slices.Contains
Answer: slices.BinarySearch. slices.BinarySearch assumes a sorted slice and returns the position plus a found bool in O(log n).
What does maps.Keys return in Go 1.23+?
In Go 1.23 maps.Keys/Values return iterators (iter.Seq); collect them with slices.Collect if you need a slice.
What does cmp.Or(a, b, c) do?
- adds the values
- returns the first non-zero argument (or the zero value)
- compares two values
- sorts the arguments
Answer: returns the first non-zero argument (or the zero value). cmp.Or returns its first argument that is not the zero value, handy for fallback/default chains.
What does cmp.Compare(a, b) return when a < b?
- a positive number
- true
- 0
- a negative number
Answer: a negative number. cmp.Compare returns -1 if a<b, 0 if equal, +1 if a>b — exactly what SortFunc and BinarySearchFunc expect.
Which constraint lets a generic function accept any orderable type?
- error
- any
- cmp.Ordered
- comparable
Answer: cmp.Ordered. cmp.Ordered constrains to types supporting < <= >= > (ints, floats, strings); comparable only allows == and !=.
What do slices.Equal and maps.Equal compare?
- memory addresses
- whether the elements (or key/value pairs) are equal
- the capacity
- only the lengths
Answer: whether the elements (or key/value pairs) are equal. slices.Equal compares element by element; maps.Equal compares the same set of keys mapped to equal values.