
Go Interfaces
Shape Go packages with small consumer-defined interfaces, concrete constructors, and compile-time compliance checks so backends stay testable without leaky abstractions.
Install
npx skills add https://github.com/cxuu/golang-skills --skill go-interfacesWhat is this skill?
- Applies accept-interfaces-return-concrete-types: consumers declare minimal interfaces, producers return structs or point
- Covers compile-time interface checks via var _ I = (*T)(nil) with a bundled compliance scanner script.
- Documents type assertions, type switches, and embedding patterns aligned with Effective Go and Google/Uber style.
- Explicitly excludes generics-based polymorphism—points you to go-generics for that path.
- Runnable check: bash scripts/check-interface-compliance.sh for exported interfaces missing compliance checks.
Adoption & trust: 665 installs on skills.sh; 110 GitHub stars; 3/3 security scanners passed (skills.sh audits).
Recommended Skills
Journey fit
Interface design and composition are core backend architecture work while you are building services, libraries, and agents in Go. The skill targets package boundaries, constructors, embedding, and mock-friendly seams—not frontend, deploy, or ops tooling.
Common Questions / FAQ
Is Go Interfaces safe to install?
skills.sh reports 3 of 3 security scanners passed. Review the Security Audits panel on this page before installing in production.
SKILL.md
READMESKILL.md - Go Interfaces
# Go Interfaces and Composition ## Available Scripts - **`scripts/check-interface-compliance.sh`** — Finds exported interfaces missing compile-time compliance checks (`var _ I = (*T)(nil)`). Run `bash scripts/check-interface-compliance.sh --help` for options. --- ## Accept Interfaces, Return Concrete Types Interfaces belong in the package that **consumes** values, not the package that **implements** them. Return concrete (usually pointer or struct) types from constructors so new methods can be added without refactoring. ```go // Good: consumer defines the interface it needs package consumer type Thinger interface { Thing() bool } func Foo(t Thinger) string { ... } ``` ```go // Good: producer returns concrete type package producer type Thinger struct{ ... } func (t Thinger) Thing() bool { ... } func NewThinger() Thinger { return Thinger{ ... } } ``` ```go // Bad: producer defines and returns its own interface package producer type Thinger interface { Thing() bool } type defaultThinger struct{ ... } func NewThinger() Thinger { return defaultThinger{ ... } } ``` **Do not define interfaces before they are used.** Without a realistic example of usage, it is too difficult to see whether an interface is even necessary. --- ## Generality: Hide Implementation, Expose Interface If a type exists only to implement an interface with no exported methods beyond that interface, return the interface from constructors to hide the implementation: ```go func NewHash() hash.Hash32 { return &myHash{} // unexported type } ``` Benefits: implementation can change without affecting callers, substituting algorithms requires only changing the constructor call. --- ## Type Assertions: Comma-Ok Idiom Without checking, a failed assertion causes a runtime panic. Always use the comma-ok idiom to test safely: ```go str, ok := value.(string) if ok { fmt.Printf("string value is: %q\n", str) } ``` To check if a value implements an interface: ```go if _, ok := val.(json.Marshaler); ok { fmt.Printf("value %v implements json.Marshaler\n", val) } ``` --- ## Type Switch It's idiomatic to reuse the variable name (`t := t.(type)`) — the variable has the correct type in each case branch. When a case lists multiple types (`case int, int64:`), the variable has the interface type. --- ## Embedding Avoid embedding types in public structs — the inner type's full method set becomes part of your public API. Use unexported fields instead. > Read [references/EMBEDDING.md](references/EMBEDDING.md) when using struct embedding for composition, overriding embedded methods, resolving name conflicts, applying the HandlerFunc adapter pattern, or deciding whether to embed in public API types. --- ## Interface Satisfaction Checks Use a blank identifier assignment to verify a type implements an interface at compile time: ```go var _ json.Marshaler = (*RawMessage)(nil) ``` This causes a compile error if `*RawMessage` doesn't implement `json.Marshaler`. Use this pattern when: - There are no static conversions that would verify the interface automatically - The type must satisfy an interface for correct behavior (e.g., custom JSON marshaling) - Interface changes should break compilation, not silently degrade **Don't** add these checks for every interface — only when no other static conversion would catch the error. > **Validation**: After defining interfaces or implementations, run `bash scr