Stage 1 · Code
Generics
Generics in Practice
Put the ideas together by building a small but complete generic set library, then apply it to real sample data and trace exactly what each operation produces.
Why a Set Is a Good Example
A set is a collection where each value either exists or does not exist. No duplicates matter. That makes it great for jobs like tags, feature flags, permissions, service names, or supported regions.
It is also a perfect generic example because the behavior stays the same no matter what you store. A set of strings and a set of integers need the same operations: add, remove, check membership, combine, and find overlap.
The Complete Set Type
Let's build the full thing, not a half-example. This small library is enough to use in real programs.
Why comparable Is Required
A set needs to check whether a value is already present and needs to use values as map keys. In Go, map keys must be comparable. That is why the set is Set[T comparable], not Set[T any].
This is a great example of a constraint matching the real job. We are not using comparable because it looks official. We are using it because the data structure literally depends on comparability to function.
A good constraint is not random decoration. It tells the reader why the generic code is allowed to do what it does. Here, comparable explains both map-key validity and membership checks.
Using the Set with Real Data
Let's treat these sets as technology tags on two engineering skill clusters. One cluster is backend-focused. The other is reliability-focused. We want to know what they share and what the combined toolkit looks like.
| Set | Values | Meaning |
|---|---|---|
backend | go, postgres, kubernetes | Core backend platform skills |
reliability | go, prometheus, kubernetes | Core reliability and observability skills |
| Intersection | go, kubernetes | Skills both groups share |
| Union | go, kubernetes, postgres, prometheus | Combined skill inventory without duplicates |
This is the kind of thing generics are excellent at: building one small, solid tool you can reuse with different real data sets without rewriting the data-structure logic every time.
Tracing Union and Intersection
Quiz
Why is `Set[T comparable]` a better fit than `Set[T any]`?
Practice
Add a method `Difference` to the generic set type. It should return a new set containing values that exist in the receiver but not in the `other` set.
Summary
- A generic set is a strong real-world example because the structure and operations stay the same across many value types.
comparableis required because sets depend on map keys and equality checks.- Methods like
Add,Remove,Contains,Union, andIntersectionform a small but genuinely useful utility library. - Extra helpers can use stronger constraints only when needed, like
SortedValues[T cmp.Ordered]. - Generics are at their best when they remove repetition from a stable abstraction without hiding the underlying logic.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.