[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M7BQCZX3RZ3VFTMW1Q6PJ7SB":3,"skill-same:01M7BQCZX3RZ3VFTMW1Q6PJ7SB":51},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":16,"categories":17,"keywords":18,"publishedAt":19,"linkOnly":20,"markdownContent":21,"markdownBody":22,"frontMatter":23,"refName":24,"commitSha":25,"skillDirectory":26,"forksCount":27,"repositoryHtmlUrl":28,"repositoryHomepageUrl":6,"ownerHtmlUrl":29,"ownerBlogUrl":6,"ownerDescription":6,"updatedAt":30,"install":31,"resources":44},"01M7BQCZX3RZ3VFTMW1Q6PJ7SB","go-generics",null,"Use when choosing or writing Go generics, constraints, type aliases versus definitions, or utilities for multiple types. Non-generic interface design belongs to go-interfaces.","h0rn3t\u002Fgolang-skills","h0rn3t","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F1213719?v=4","01M7BQBDJ8D4P5W7EQHBD464BX","Eugene Shershen","01M7BQBDJXMTCKNCEDKVEYDRGA",7,"Apache-2.0","https:\u002F\u002Fgithub.com\u002Fh0rn3t\u002Fgolang-skills\u002Fblob\u002Fmain\u002Fskills\u002Fgo-generics\u002FSKILL.md",[],[],"2026-11-15 14:19:17",false,"---\nname: go-generics\ndescription: Use when choosing or writing Go generics, constraints, type aliases versus definitions, or utilities for multiple types. Non-generic interface design belongs to go-interfaces.\n---\n\n# Go Generics and Type Parameters\n\n> Compatibility: Baseline Go 1.27 (see [COMPATIBILITY.md](..\u002Fgo-style-core\u002Freferences\u002FCOMPATIBILITY.md)). Generic **methods**\n> require Go 1.27+, and the compiler gates them by the `go` directive: at\n> `go 1.26` it rejects one with `generic method requires go1.27 or later`.\n> Inference in function-type conversions and composite-literal elements\n> (Go 1.27+) and self-referential constraints (Go 1.26+) are not gated — they\n> compile on a 1.27 toolchain at an older directive and fail on the older\n> toolchain itself, so verify them on the CI toolchain. Generic type aliases\n> Go 1.24+; generics themselves, Go 1.18+.\n\n## Resource Routing\n\n- `references\u002FCONSTRAINTS.md` - Read when composing constraints or supplying type arguments explicitly.\n\n## When to Use Generics\n\nStart with concrete types. Generalize only when a second type appears.\n\n```\nDo multiple types share identical logic?\n├─ No  → concrete types\n└─ Yes → Do they share a useful interface?\n         ├─ Yes → interface\n         └─ No  → generics\n```\n\n**Prefer generics when**: multiple types share identical logic (sort, filter,\nmap\u002Freduce); the alternative is `any` plus type switching; you are building a\nreusable container.\n\n**Avoid generics when**: only one type is ever instantiated; an interface\nalready models the shared behavior; the generic version is harder to read.\n\n---\n\n## Generic Methods (Go 1.27+)\n\nMethods may declare their own type parameters, independent of the receiver's.\nThis removes the old workaround of a package-level generic function taking the\nreceiver as its first argument.\n\n```go\ntype Store struct{ data map[string]any }\n\n\u002F\u002F Go 1.27: the type parameter belongs to the method\nfunc (s *Store) Get[T any](key string) (T, bool) {\n    v, ok := s.data[key].(T)\n    return v, ok\n}\n\nn, ok := store.Get[int](\"count\")\n```\n\nConstraints:\n\n- A generic method cannot satisfy an interface; [go-interfaces](..\u002Fgo-interfaces\u002FSKILL.md#generic-methods-cannot-satisfy-interfaces)\n  owns that rule and the assertion that catches it.\n- The receiver's own type parameters stay on the receiver; do not redeclare them.\n- Reflection does not see a generic method (`MethodByName`, `NumMethod`): a\n  method that `text\u002Ftemplate`, `html\u002Ftemplate`, or RPC calls by name stays\n  non-generic, or the template fails at run time.\n- Same restraint as generic functions: add the parameter when a second type\n  actually appears, not in anticipation.\n\nGo 1.27 extends function type inference to all assignments and conversions to\nmatching function types, composite-literal elements included (`S{f: id}`,\n`[]func(int) int{id}`). Assignment to a typed variable already supported\ninference in Go 1.21. Prefer inference when the call site remains clear.\n\n---\n\n## Constraints\n\nPrefer standard-library constraints over hand-written ones:\n\n| Need | Use |\n|---|---|\n| `\u003C`, `>` ordering | `cmp.Ordered` (Go 1.21+) — not `constraints.Ordered` |\n| Numeric union | Write a local union; the `constraints` module is still `x\u002Fexp` |\n\n```go\ntype Numeric interface {\n    ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~float32 | ~float64\n}\n```\n\nUse `~` for underlying types so named types (`type Celsius float64`) satisfy the\nconstraint, and `|` for unions.\n\n### Self-referential constraints (Go 1.26+)\n\nA type parameter may appear in its own constraint, so a constraint can require\nthat a type combines with, compares to, or produces its own kind:\n\n```go\ntype Adder[A Adder[A]] interface{ Add(A) A }\n\nfunc Sum[A Adder[A]](zero A, xs ...A) A {\n    for _, x := range xs {\n        zero = zero.Add(x)\n    }\n    return zero\n}\n```\n\nReach for it only when the method genuinely takes or returns the implementing\ntype; `comparable` or `cmp.Ordered` covers equality and ordering. Go 1.25\nrejects the declaration as an invalid recursive type, and the `go` directive\ndoes not gate it (see the Compatibility note).\n\n### Hashing generic keys\n\n`hash\u002Fmaphash.Hasher[K]` (Go 1.27+) is the seam between a hash-based container\nand its keys: one hash function plus the matching equivalence relation. It buys\nwhat the built-in map cannot do — keys that are not `comparable`, or an equality\nother than `==` — so constrain the container to `any`, not `comparable`.\n`maphash.ComparableHasher[K]{}` is the ready-made implementation for ordinary\nkeys; a custom `Hasher` is a two-method type.\n\n```go\ntype table[K, V any] struct {\n    hasher maphash.Hasher[K] \u002F\u002F maphash.ComparableHasher[string]{} at an ordinary call site\n    seed   maphash.Seed      \u002F\u002F maphash.MakeSeed() per table; a zero Seed panics in SetSeed\n}\n\nfunc (t *table[K, V]) hash(k K) uint64 {\n    var h maphash.Hash\n    h.SetSeed(t.seed)\n    t.hasher.Hash(&h, k)\n    return h.Sum64()\n}\n```\n\nA `Hasher` must be logically stateless, and `Equal(x, y)` must imply an equal\nhash — the container relies on both. `ComparableHasher` is itself constrained to\n`comparable`, so it can only be named where the key type is concrete: inside a\n`K any` container the field stays the `Hasher[K]` interface.\n\n---\n\n## Type Aliases vs Type Definitions\n\nType aliases (`type Old = new.Name`) are rare — use for package migration or\ngradual API refactoring. Generic type aliases (Go 1.24+) are legal\n(`type Set[T comparable] = map[T]struct{}`) and carry the same caution: an alias\nadds a name, not a type, so it buys nothing but a migration path.\n\n---\n\n## Common Pitfalls\n\n**Don't wrap standard library types.** A single-use generic is indirection:\n\n```go\n\u002F\u002F Bad\ntype Set[T comparable] struct{ m map[T]struct{} }\n\n\u002F\u002F Better\nseen := map[string]struct{}{}\n```\n\n**Don't use generics for interface satisfaction.** If `T` is only used to\nsatisfy an interface, accept the interface — unless `T` is a slice element:\n\n```go\n\u002F\u002F Bad\nfunc Process[T io.Reader](r T) error\n\n\u002F\u002F Good\nfunc Process(r io.Reader) error\n\n\u002F\u002F Good: a []*os.File is not a []io.Reader, so the slice keeps T\nfunc ProcessAll[T io.Reader](rs []T) error\n```\n\n**Don't over-constrain.** `comparable` beats `interface{ ~int | ~string }` when\nyou only need `==`, but it admits interface types: `==` or a map key on `T`\npanics at run time when a caller passes `any` holding a slice or map.\n\n---\n\n## Related Skills\n\n- [go-interfaces](..\u002Fgo-interfaces\u002FSKILL.md): whether an interface already models the shared behavior.\n- [go-style-core](..\u002Fgo-style-core\u002FSKILL.md): declaration and scope mechanics for new types.\n- [go-documentation](..\u002Fgo-documentation\u002FSKILL.md): doc comments and examples for generic APIs.\n","# Go Generics and Type Parameters\n\n> Compatibility: Baseline Go 1.27 (see [COMPATIBILITY.md](..\u002Fgo-style-core\u002Freferences\u002FCOMPATIBILITY.md)). Generic **methods**\n> require Go 1.27+, and the compiler gates them by the `go` directive: at\n> `go 1.26` it rejects one with `generic method requires go1.27 or later`.\n> Inference in function-type conversions and composite-literal elements\n> (Go 1.27+) and self-referential constraints (Go 1.26+) are not gated — they\n> compile on a 1.27 toolchain at an older directive and fail on the older\n> toolchain itself, so verify them on the CI toolchain. Generic type aliases\n> Go 1.24+; generics themselves, Go 1.18+.\n\n## Resource Routing\n\n- `references\u002FCONSTRAINTS.md` - Read when composing constraints or supplying type arguments explicitly.\n\n## When to Use Generics\n\nStart with concrete types. Generalize only when a second type appears.\n\n```\nDo multiple types share identical logic?\n├─ No  → concrete types\n└─ Yes → Do they share a useful interface?\n         ├─ Yes → interface\n         └─ No  → generics\n```\n\n**Prefer generics when**: multiple types share identical logic (sort, filter,\nmap\u002Freduce); the alternative is `any` plus type switching; you are building a\nreusable container.\n\n**Avoid generics when**: only one type is ever instantiated; an interface\nalready models the shared behavior; the generic version is harder to read.\n\n---\n\n## Generic Methods (Go 1.27+)\n\nMethods may declare their own type parameters, independent of the receiver's.\nThis removes the old workaround of a package-level generic function taking the\nreceiver as its first argument.\n\n```go\ntype Store struct{ data map[string]any }\n\n\u002F\u002F Go 1.27: the type parameter belongs to the method\nfunc (s *Store) Get[T any](key string) (T, bool) {\n    v, ok := s.data[key].(T)\n    return v, ok\n}\n\nn, ok := store.Get[int](\"count\")\n```\n\nConstraints:\n\n- A generic method cannot satisfy an interface; [go-interfaces](..\u002Fgo-interfaces\u002FSKILL.md#generic-methods-cannot-satisfy-interfaces)\n  owns that rule and the assertion that catches it.\n- The receiver's own type parameters stay on the receiver; do not redeclare them.\n- Reflection does not see a generic method (`MethodByName`, `NumMethod`): a\n  method that `text\u002Ftemplate`, `html\u002Ftemplate`, or RPC calls by name stays\n  non-generic, or the template fails at run time.\n- Same restraint as generic functions: add the parameter when a second type\n  actually appears, not in anticipation.\n\nGo 1.27 extends function type inference to all assignments and conversions to\nmatching function types, composite-literal elements included (`S{f: id}`,\n`[]func(int) int{id}`). Assignment to a typed variable already supported\ninference in Go 1.21. Prefer inference when the call site remains clear.\n\n---\n\n## Constraints\n\nPrefer standard-library constraints over hand-written ones:\n\n| Need | Use |\n|---|---|\n| `\u003C`, `>` ordering | `cmp.Ordered` (Go 1.21+) — not `constraints.Ordered` |\n| Numeric union | Write a local union; the `constraints` module is still `x\u002Fexp` |\n\n```go\ntype Numeric interface {\n    ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~float32 | ~float64\n}\n```\n\nUse `~` for underlying types so named types (`type Celsius float64`) satisfy the\nconstraint, and `|` for unions.\n\n### Self-referential constraints (Go 1.26+)\n\nA type parameter may appear in its own constraint, so a constraint can require\nthat a type combines with, compares to, or produces its own kind:\n\n```go\ntype Adder[A Adder[A]] interface{ Add(A) A }\n\nfunc Sum[A Adder[A]](zero A, xs ...A) A {\n    for _, x := range xs {\n        zero = zero.Add(x)\n    }\n    return zero\n}\n```\n\nReach for it only when the method genuinely takes or returns the implementing\ntype; `comparable` or `cmp.Ordered` covers equality and ordering. Go 1.25\nrejects the declaration as an invalid recursive type, and the `go` directive\ndoes not gate it (see the Compatibility note).\n\n### Hashing generic keys\n\n`hash\u002Fmaphash.Hasher[K]` (Go 1.27+) is the seam between a hash-based container\nand its keys: one hash function plus the matching equivalence relation. It buys\nwhat the built-in map cannot do — keys that are not `comparable`, or an equality\nother than `==` — so constrain the container to `any`, not `comparable`.\n`maphash.ComparableHasher[K]{}` is the ready-made implementation for ordinary\nkeys; a custom `Hasher` is a two-method type.\n\n```go\ntype table[K, V any] struct {\n    hasher maphash.Hasher[K] \u002F\u002F maphash.ComparableHasher[string]{} at an ordinary call site\n    seed   maphash.Seed      \u002F\u002F maphash.MakeSeed() per table; a zero Seed panics in SetSeed\n}\n\nfunc (t *table[K, V]) hash(k K) uint64 {\n    var h maphash.Hash\n    h.SetSeed(t.seed)\n    t.hasher.Hash(&h, k)\n    return h.Sum64()\n}\n```\n\nA `Hasher` must be logically stateless, and `Equal(x, y)` must imply an equal\nhash — the container relies on both. `ComparableHasher` is itself constrained to\n`comparable`, so it can only be named where the key type is concrete: inside a\n`K any` container the field stays the `Hasher[K]` interface.\n\n---\n\n## Type Aliases vs Type Definitions\n\nType aliases (`type Old = new.Name`) are rare — use for package migration or\ngradual API refactoring. Generic type aliases (Go 1.24+) are legal\n(`type Set[T comparable] = map[T]struct{}`) and carry the same caution: an alias\nadds a name, not a type, so it buys nothing but a migration path.\n\n---\n\n## Common Pitfalls\n\n**Don't wrap standard library types.** A single-use generic is indirection:\n\n```go\n\u002F\u002F Bad\ntype Set[T comparable] struct{ m map[T]struct{} }\n\n\u002F\u002F Better\nseen := map[string]struct{}{}\n```\n\n**Don't use generics for interface satisfaction.** If `T` is only used to\nsatisfy an interface, accept the interface — unless `T` is a slice element:\n\n```go\n\u002F\u002F Bad\nfunc Process[T io.Reader](r T) error\n\n\u002F\u002F Good\nfunc Process(r io.Reader) error\n\n\u002F\u002F Good: a []*os.File is not a []io.Reader, so the slice keeps T\nfunc ProcessAll[T io.Reader](rs []T) error\n```\n\n**Don't over-constrain.** `comparable` beats `interface{ ~int | ~string }` when\nyou only need `==`, but it admits interface types: `==` or a map key on `T`\npanics at run time when a caller passes `any` holding a slice or map.\n\n---\n\n## Related Skills\n\n- [go-interfaces](..\u002Fgo-interfaces\u002FSKILL.md): whether an interface already models the shared behavior.\n- [go-style-core](..\u002Fgo-style-core\u002FSKILL.md): declaration and scope mechanics for new types.\n- [go-documentation](..\u002Fgo-documentation\u002FSKILL.md): doc comments and examples for generic APIs.\n",{"name":5,"description":7},"main","59ac8ac7a0dd394878aaa0a7a7ecbc227356739f","skills\u002Fgo-generics",2,"https:\u002F\u002Fgithub.com\u002Fh0rn3t\u002Fgolang-skills","https:\u002F\u002Fgithub.com\u002Fh0rn3t","2026-11-14 04:23:12",{"source":8,"skillName":5,"commands":32,"prompt":42,"zipAvailable":43},[33,36,39],{"tool":34,"command":35},"npx","npx skills add h0rn3t\u002Fgolang-skills --skill go-generics",{"tool":37,"command":38},"bunx","bunx skills add h0rn3t\u002Fgolang-skills --skill go-generics",{"tool":40,"command":41},"pnpm","pnpm dlx skills add h0rn3t\u002Fgolang-skills --skill go-generics","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002Fh0rn3t\u002Fgolang-skills\u002Fblob\u002Fmain\u002Fskills\u002Fgo-generics\u002FSKILL.md",true,[45],{"path":46,"resourceType":47,"resourceTypeDesc":48,"sizeBytes":49,"sourceHtmlUrl":50},"references\u002FCONSTRAINTS.md","REFERENCE","参考文档",1276,"https:\u002F\u002Fgithub.com\u002Fh0rn3t\u002Fgolang-skills\u002Fblob\u002Fmain\u002Fskills\u002Fgo-generics\u002Freferences\u002FCONSTRAINTS.md",[52,59,66,73,80,87],{"skillNo":53,"name":54,"nameZh":6,"description":55,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":56,"categories":57,"keywords":58,"publishedAt":19},"01M7BQEMJBPJWY6CCGVKW27SH0","go-troubleshooting","Use when investigating a bug or ticket in a Go service whose cause is unknown — wrong or missing results, tenant-specific behavior, a regression, stage\u002Fprod\u002FCI differences, a panic, hang, deadlock, leak, unexplained CPU\u002FRSS growth, or a flaky test. Also use for pasted stack traces, profiles, logs, or requests to find a Go issue's root cause, even without the word debug. Does not cover rewriting ticket text, implementing an already-diagnosed fix alone (use its topic skill), or optimizing a known bottleneck (see go-performance).","https:\u002F\u002Fgithub.com\u002Fh0rn3t\u002Fgolang-skills\u002Fblob\u002Fmain\u002Fskills\u002Fgo-troubleshooting\u002FSKILL.md",[],[],{"skillNo":60,"name":61,"nameZh":6,"description":62,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":63,"categories":64,"keywords":65,"publishedAt":19},"01M7BQEFVSRHNDND750SB69WD8","go-testing","Use when writing, reviewing, or improving Go test code — including table-driven tests, subtests, parallel tests, test helpers, test doubles, and assertions with cmp.Diff. Also use when a user asks to write a test for a Go function, even if they don't mention specific patterns like table-driven tests or subtests. Does not cover benchmark performance testing (see go-performance).","https:\u002F\u002Fgithub.com\u002Fh0rn3t\u002Fgolang-skills\u002Fblob\u002Fmain\u002Fskills\u002Fgo-testing\u002FSKILL.md",[],[],{"skillNo":67,"name":68,"nameZh":6,"description":69,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":70,"categories":71,"keywords":72,"publishedAt":19},"01M7BQEA2DRH6H6WFYNJYJJRQQ","go-style-core","Use when resolving Go style or language mechanics such as formatting, nesting, declarations, initialization, scope, shadowing, loops, switches, or enum zero values. API design, naming, errors, and tests have specialized skills.","https:\u002F\u002Fgithub.com\u002Fh0rn3t\u002Fgolang-skills\u002Fblob\u002Fmain\u002Fskills\u002Fgo-style-core\u002FSKILL.md",[],[],{"skillNo":74,"name":75,"nameZh":6,"description":76,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":77,"categories":78,"keywords":79,"publishedAt":19},"01M7BQE3D1H45ANCXAWCS55CDK","go-security","Use when writing or reviewing Go code that touches untrusted input, secrets, or credentials — SQL, shell, or template injection, path traversal, SSRF, cookies and security headers, password hashing, TLS settings, token comparison, or what may appear in a log line. Also use when a handler accepts a file name, URL, or command argument from a client, or when asked to find \"vulnerabilities\", even if the user never says \"security\". Does not cover the os.Root and crypto\u002Frand mechanics (see go-defensive) or the dependency CVE scan in the gate (see go-linting).","https:\u002F\u002Fgithub.com\u002Fh0rn3t\u002Fgolang-skills\u002Fblob\u002Fmain\u002Fskills\u002Fgo-security\u002FSKILL.md",[],[],{"skillNo":81,"name":82,"nameZh":6,"description":83,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":84,"categories":85,"keywords":86,"publishedAt":19},"01M7BQDZYHG2YRJA3J26HFPKR7","go-resilience","Use when handling Go dependency failures or overload with retries, backoff\u002Fjitter, budgets, idempotency, duplicate delivery, circuit breakers, bulkheads, rate limits, backpressure, or degradation. Unknown root causes belong to go-troubleshooting.","https:\u002F\u002Fgithub.com\u002Fh0rn3t\u002Fgolang-skills\u002Fblob\u002Fmain\u002Fskills\u002Fgo-resilience\u002FSKILL.md",[],[],{"skillNo":88,"name":89,"nameZh":6,"description":90,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":91,"categories":92,"keywords":93,"publishedAt":19},"01M7BQDVG7DH2G9Z97Y8H3P3E9","go-performance","Use when optimizing slow or performance-critical Go code, allocations, string concatenation in loops, or benchmarks. Also use when writing or reviewing a cache, including implementing one from a stub — its key, its expiry, and how a miss loads. Concurrent code patterns belong to go-concurrency.","https:\u002F\u002Fgithub.com\u002Fh0rn3t\u002Fgolang-skills\u002Fblob\u002Fmain\u002Fskills\u002Fgo-performance\u002FSKILL.md",[],[]]