samber/cc-skills-golang / skills/golang-data-structures
golang-data-structures
VerifiedGolang data structures — slices (internals, capacity growth, preallocation, slices package), maps (internals, hash buckets, maps package), arrays, container/list/heap/ring, strings.Builder vs bytes.Buffer, generic collections, pointers (unsafe.Pointer, weak.Pointer), and copy semantics. Use when choosing or optimizing Go data structures, implementing generic containers, using container/ packages, unsafe or weak pointers, or questioning slice/map internals. Not for applying optimization patterns once profiling has identified a bottleneck (→ See `samber/cc-skills-golang@golang-performance` skill).
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 10.4 KB |
| evals/evals.json | config | 19.6 KB |
| references/containers.md | markdown | 4.0 KB |
| references/generics.md | markdown | 2.5 KB |
| references/map-internals.md | markdown | 2.5 KB |
| references/pointers.md | markdown | 4.2 KB |
| references/slice-internals.md | markdown | 1.9 KB |
references/generics.md
View on GitHub# Writing Generic Data Structures (Go 1.18+)
## Type Constraints
Use the tightest constraint that satisfies your needs:
| Constraint | What It Allows | Use For |
| --- | --- | --- |
| `any` | All types | Containers that only store/retrieve |
| `comparable` | Types supporting `==` and `!=` | Map keys, set membership, dedup |
| `cmp.Ordered` | Numeric types + `string` | Sorting, min/max, binary search |
| Custom interface | Domain-specific operations | Specialized containers |
### Custom Constraints
```go
// Union constraint — restrict to specific types
type Number interface {
~int | ~int64 | ~float64
}
// Method constraint — require specific behavior
type Stringer interface {
comparable
String() string
}
```
The `~` prefix includes all types whose underlying type matches (e.g., `~int` matches `type UserID int`).
## Generic Set Example
```go
type Set[T comparable] map[T]struct{}
func NewSet[T comparable](vals ...T) Set[T] {
s := make(Set[T], len(vals))
for _, v := range vals {
s[v] = struct{}{}
}
return s
}
func (s Set[T]) Add(v T) { s[v] = struct{}{} }
func (s Set[T]) Remove(v T) { delete(s, v) }
func (s Set[T]) Contains(v T) bool { _, ok := s[v]; return ok }
func (s Set[T]) Len() int { return len(s) }
func (s Set[T]) Union(other Set[T]) Set[T] {
result := NewSet[T]()
for v := range s {
result.Add(v)
}
for v := range other {
result.Add(v)
}
return result
}
```
## Generic Sorted Slice
```go
func InsertSorted[T cmp.Ordered](s []T, v T) []T {
i, _ := slices.BinarySearch(s, v)
return slices.Insert(s, i, v)
}
```
## Constraint Composition
Combine multiple constraints with embedded interfaces:
```go
type OrderedStringer interface {
cmp.Ordered
fmt.Stringer
}
```
## When NOT to Use Generics
- **Single concrete type** — generics add complexity for no benefit
- **`any` constraint with type switches** — you're just reimplementing `interface{}` with extra syntax
- **Two or fewer instantiations** — the abstraction overhead isn't justified
- **Complex type relationships** — Go's type system doesn't support higher-kinded types; if the constraints become convoluted, use interfaces instead
Generics shine for data structures (containers, sets, trees), algorithms (sort, search, transform), and utility functions (min, max, clamp) where the logic is identical across types.
→ See `samber/cc-skills-golang@golang-structs-interfaces` skill for generics vs `any` guidance and interface design.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness. | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependencies | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |