go-generics
Skill9 starsChanged 4 months agoMITView on GitHub
Use when deciding whether to introduce Go generics, writing generic functions or types, composing type constraints, or choosing between type aliases and type definitions. Apply proactively when a user is writing a utility function that could conceivably work with multiple types, even if they didn't mention generics. Does not cover interface-only designs (see go-interfaces).
1 other project uses a version of this, 1 of them edited.
Adopted by
Most recently taken up by
- cxuu/golang-skillsedited, first seen Jun 20, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 1
- cxuu/golang-skills★ 16513% same
Wrote their own version · 0
None found.
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository26 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- go-clean-architectureSkill
- go-code-reviewSkill
- go-code-styleSkill
- go-concurrencySkill
- go-contextSkill
- go-control-flowSkill
- go-databaseSkill
- go-data-structuresSkill
- go-declarationsSkill
- go-defensiveSkill
- go-documentationSkill
- go-error-handlingSkill
- go-functional-optionsSkill
- go-functionsSkill
- go-graphqlSkill
- go-grpcSkill
- go-interfacesSkill
- go-lintingSkill
- go-loggingSkill
- go-namingSkill
- go-observabilitySkill
- go-packagesSkill
- go-performanceSkill
- go-swaggerSkill
- go-testingSkill
The file
We split this skill into the rules it gives an agent
1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 92
Prefer cmp.Ordered (Go 1.21+) over rolling your own.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Go Generics
Generics are a powerful but easy-to-misuse feature. The Go answer is pragmatic: write concrete code first, then generalize only when you have a real second caller.
Core Rules
- Write concrete first. Reach for generics only when a second type actually needs the same logic.
- If an interface already models the behavior, use the interface. Don't pile type parameters on top.
- Prefer standard constraints (
comparable,cmp.Ordered,any) over hand-rolled unions. - Don't over-constrain.
comparableis usually enough; the narrower the constraint, the fewer callers benefit. - Name type parameters with a single uppercase letter (
T,K,V,E) unless a longer name genuinely helps. - Don't use generics for interface satisfaction.
func F[T io.Reader](r T)is justfunc F(r io.Reader). - Don't wrap stdlib containers "for generic convenience" unless you eliminate real duplication.
Decision Flow
Multiple types need the same logic?
├─ No → concrete type
├─ Yes → do they share a useful interface?
│ ├─ Yes → use the interface
│ └─ No → use genericsWhen NOT to Use Generics
// Premature: only ever called with int
func Sum[T constraints.Integer | constraints.Float](xs []T) T {
var t T
for _, x := range xs { t += x }
return t
}
// Better
func SumInts(xs []int) int {
var t int
for _, x := range xs { t += x }
return t
}"Write code, don't design types." — Griesemer & Taylor
When Generics Pay Off
- A library function the standard library would have written generically:
slices.Index,maps.Keys,slices.SortFunc. - Concurrent-safe data structures (typed sets, ordered maps) where boxing into
anywould be both ugly and slow. - Map/Reduce-style helpers that genuinely apply to many element types.
Type Parameter Naming
| Name | Typical use |
T | General element / first type |
K | Map key |
V | Map value |
E | Element of a collection |
R | Result of a transform |
Multi-letter names are reserved for constraints where the meaning is non-obvious:
func Marshal[Opts encoding.MarshalOptions](v any, opts Opts) ([]byte, error)Constraint Composition
type Numeric interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~float32 | ~float64
}
func Sum[T Numeric](xs []T) T {
var t T
for _, x := range xs { t += x }
return t
}~intmeans "anything whose underlying type isint" — coverstype Celsius int.|unions widen the set.- Prefer
cmp.Ordered(Go 1.21+) over rolling your own.
Read references/constraints.md for the constraint catalogue, when
~matters, and how type inference interacts with constraints.
Common Pitfalls
Don't Wrap Stdlib Types Generically
// Adds complexity, eliminates no duplication
type Set[T comparable] struct {
m map[T]struct{}
}
// Use the builtin
seen := map[string]struct{}{}
seen["a"] = struct{}{}A generic wrapper around map[T]struct{} is only worth it if you keep it for many call sites and provide methods that pay for the indirection (e.g., Union, Intersect).
Don't Use Generics for Interface Satisfaction
// Pointless type parameter
func Process[T io.Reader](r T) error { ... }
// Just use the interface
func Process(r io.Reader) error { ... }Don't Over-Constrain
// Restrictive without reason
func Contains[T interface{ ~int | ~string }](xs []T, t T) bool { ... }
// comparable is enough
func Contains[T comparable](xs []T, t T) bool { ... }Read references/generics-vs-interfaces.md when interfaces and generics both seem to fit, and you have to choose.
Type Aliases vs Definitions
type Old = pkg.New // alias: same type, alternate name
type Old pkg.New // definition: new type, fresh method setType aliases (=) are for package migrations and gradual API moves. For new types, use a definition.
Anti-Patterns
| Anti-pattern | Why it hurts | Do this instead |
| Generic for a single instantiation | Indirection without payoff | Concrete code |
| Generic where an interface fits | Type parameter is just io.Reader in disguise | Accept the interface |
interface{ ~int } when comparable suffices | Restricts callers, no benefit | Loosen the constraint |
Custom Numeric constraint | cmp.Ordered exists | Standard constraint |
Set[T] wrapper around map[T]struct{} | Two-line struct, no methods | Use the map directly |
| Generic function with two type params, neither used | The compiler can infer nothing | Drop one or both |
Verification Checklist
- At least two real, current call sites benefit from the type parameter
- An interface would not be a simpler model
- Constraint is the loosest one that compiles (
any,comparable,cmp.Orderedpreferred) - Type parameter names are conventional letters unless clarity demands more
- No
Texists only to satisfy an interface — accept the interface instead - No generic wrapper added without methods that justify it
- Doc comment explains what the type parameter must support
References
- references/constraints.md — constraint catalogue,
~and|,cmp.Ordered, type inference - references/generics-vs-interfaces.md — picking between a generic and an interface
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

