Expert guidance on Go generics including type parameters, constraints, and type inference. Helps developers understand and implement generic programming patterns in Go.
Expert guidance on Go generics including type parameters, constraints, and type inference based on official Go documentation.
This skill helps developers understand and implement Go generics effectively. It provides guidance on:
When a user asks about Go generics, type parameters, constraints, or related topics:
1. **Assess the Question**
- Identify the specific aspect of generics being asked about (type parameters, constraints, inference, etc.)
- Determine the user's familiarity level with Go and generics
- Note if they have specific code they're working with
2. **Provide Foundational Context**
- Explain that Go added generics in Go 1.18 (March 2022)
- Clarify that generics use type parameters in square brackets `[T any]`
- Note that constraints define what operations are allowed on type parameters
3. **Answer with Concrete Examples**
- Provide working code examples that demonstrate the concept
- Show both the generic implementation and how to call it
- Include type constraint definitions when relevant
- Demonstrate type inference in action
4. **Cover Key Concepts as Needed**
**Type Parameters:**
- Functions, types, and methods can have type parameters
- Syntax: `func Name[T Constraint](param T) { }`
- Multiple type parameters: `[T, U Constraint]`
**Constraints:**
- `any` is an alias for `interface{}` (least restrictive)
- `comparable` for types that support `==` and `!=`
- Custom constraints using interface types
- Constraint literals with type unions: `interface{ ~int | ~float64 }`
**Type Inference:**
- Go can infer type arguments from function arguments
- Type inference works for function calls, not type declarations
- Explicit type arguments use syntax: `Function[int, string](args)`
5. **Address Common Patterns**
- Generic data structures (slices, maps, custom collections)
- Generic algorithms (map, filter, reduce)
- Type-safe wrappers
- Generic method sets
6. **Highlight Best Practices**
- Start simple; add generics when you see duplication
- Use `any` sparingly; prefer specific constraints
- Consider if an interface would be simpler than generics
- Don't force generics where they don't provide value
7. **Provide References**
- Link to official Go generics documentation: https://go.dev/doc/tutorial/generics
- Reference the Go spec on type parameters: https://go.dev/ref/spec#Type_parameter_declarations
- Mention the generics proposal for deep dives: https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md
**User:** "How do I write a generic function to find the minimum value in a slice?"
**Assistant response should:**
1. Explain the need for the `comparable` or ordered constraint
2. Show how to define a constraint for ordered types
3. Provide a complete generic `Min` function
4. Demonstrate calling it with different numeric types
5. Explain how type inference works in the calls
**User:** "What are type constraints in Go?"
**Assistant response should:**
1. Define constraints as interfaces that type parameters must satisfy
2. Explain built-in constraints (`any`, `comparable`)
3. Show how to create custom constraints
4. Demonstrate type unions with `~` for underlying types
5. Give examples of when to use each approach
**User:** "Can you help me make this function generic?" [provides code]
**Assistant response should:**
1. Analyze the existing function for genericizable patterns
2. Identify what type(s) should become parameters
3. Determine appropriate constraints
4. Refactor the code with type parameters
5. Show usage examples with type inference
6. Explain any tradeoffs or limitations
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/go-generics-guide/raw