/golang:go-interfacesfrom golang
Guides Go interface design and the typed-nil-error gotcha — accept interfaces and return concrete structs, keep interfaces small (1–3 methods, `-er` names), define them on the consumer side not the producer, avoid `any`/`interface{}` as a parameter type, compose with embedding, and never return a concrete `*MyError`/pointer type where the value can be nil. Auto-invokes when writing or editing interface definitions, function signatures that take or return interfaces, `any`/`interface{}` parameters, interface embedding, or functions returning concrete error/pointer types — and on "why is this nil check failing", "is this always non-nil", or "should this be an interface". An interface holds a (type, value) pair, so a nil pointer inside one is not a nil interface.
Ships in golang. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
"The bigger the interface, the weaker the abstraction." · "interface{} says nothing." — Go Proverbs
"Interfaces in Go provide a way to specify the behavior of an object: if something can do this, then it can be used here." — Effective Go
"Go interfaces generally belong in the package that uses values of the interface type, not the package that implements those values." — Go Code Review Comments
A Go interface is a set of method signatures, satisfied structurally — a type implements it just by having the methods, with no implements keyword. That makes interfaces cheap to add and easy to over-add. The discipline below is about adding them where they earn their place, keeping them small, and understanding the one representation fact (an interface is a (type, value) pair) that turns a returned nil pointer into a non-nil error.
Take the narrowest interface you need as input; return the concrete type as output. The Google Style Guide states it directly: "Functions should take interfaces as arguments but return concrete types" (Google Go Style Guide — Decisions). The reason is asymmetric: accepting an interface lets every caller — including a test fake — pass whatever satisfies it, while returning a concrete type means "new methods can be added to implementations without requiring extensive refactoring" (CodeReviewComments).
// WRONG — returns an interface, hiding the concrete type and freezing the method set
func NewStore() StoreInterface { return &store{} }
// RIGHT — accept the small interface you use; return the concrete *Store
func NewStore() *Store { return &Store{data: map[string]string{}} }
func describe(g itemGetter, id string) (string, error) { // accepts an interface
v, err := g.Get(id)
if err != nil {
return "", fmt.Errorf("describing %s: %w", id, err)
}
return "item=" + v, nil
}
| Rule | The discipline | Source |
|---|---|---|
| Accept interfaces, return structs | Narrow interface in; concrete type out | "Functions should take interfaces as arguments but return concrete types" (Google Decisions) |
| Keep interfaces small | 1–3 methods; -er names | "one-method interfaces are named by the method name plus an -er suffix ... Reader, Writer, Formatter" (Effective Go); "The bigger the interface, the weaker the abstraction" (Proverbs) |
| Define on the consumer side | The user declares it, not the implementor | "Go interfaces generally belong in the package that uses values of the interface type" (CodeReviewComments) |
| Not before they're used | No interface without a real second use | "Do not define interfaces before they are used" (CodeReviewComments) |
| Not "for mocking" | Test against the real API | "Do not define interfaces on the implementor side of an API 'for mocking'" (CodeReviewComments) |
any says nothing | Avoid any/interface{} params | "interface{} says nothing" (Proverbs) |
| Compose by embedding | Build big interfaces from small ones | io.ReadWriter = Reader + Writer (Effective Go) |
Declare error returns as error | Never return concrete *MyError | "use the error type in their signature ... rather than a concrete type such as *MyError" (Go FAQ) |
-er ConventionIdiomatic Go interfaces are tiny. "Interfaces with only one or two methods are common in Go code, and are usually given a name derived from the method" (Effective Go); "one-method interfaces are named by the method name plus an -er suffix ... Reader, Writer, Formatter, CloseNotifier" (Effective Go). The canonical example is io.Reader — one method, used everywhere:
// io.Reader, the most-used interface in the standard library:
type Reader interface {
Read(p []byte) (n int, err error)
}
A small interface is a strong abstraction because almost anything can satisfy it, and almost nothing breaks when an implementation changes. A large one — a "manager" interface mirroring a whole struct — is the opposite: "The bigger the interface, the weaker the abstraction" (Go Proverbs). Prefer the stdlib interfaces (io.Reader, io.Writer, fmt.Stringer, error) before inventing your own.
The package that uses a behavior declares the interface; the package that provides it returns a concrete type. "The consumer of the interface should define it (not the package implementing the interface), ensuring it includes only the methods they actually use" (Google Go Style Guide — Decisions).
The most common LLM anti-pattern here is the producer-side interface created "for mocking." Go rejects it twice: "Do not define interfaces on the implementor side of an API 'for mocking'; instead, design the API so that it can be tested using the public API of the real implementation"; and "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, let alone what methods it ought to contain" (CodeReviewComments). "Avoid creating interfaces until a real need exists" (Google Decisions).
// WRONG — producer package exports an interface mirroring its own struct
package producer
type Thinger interface{ Thing() bool } // DO NOT DO IT
func NewThinger() Thinger { return defaultThinger{} }
// RIGHT — producer returns a concrete type; the CONSUMER declares the small interface it needs
package producer
type Thinger struct{ /* ... */ }
func (t Thinger) Thing() bool { /* ... */ }
package consumer
type thinger interface{ Thing() bool } // only the method this package uses
func Foo(t thinger) string { /* ... */ }
Because satisfaction is structural, the consumer's interface needs no cooperation from the producer — a test fake in the consumer's own package satisfies it for free.
any / interface{} Says NothingThe empty interface carries no behavior, so a parameter typed any tells the caller and the compiler nothing about what is allowed. "interface{} says nothing" (Go Proverbs). Reach for a concrete type, a small behavioral interface, or — when the logic is genuinely identical across types — a generic type parameter, not any.
// WRONG — `any` defeats the type system; every use needs a runtime type assertion
func Write(w io.Writer, v any) error { /* type-switch on v ... */ }
// RIGHT — name the behavior you require
func Write(w io.Writer, v fmt.Stringer) error {
_, err := io.WriteString(w, v.String())
return err
}
The interface-vs-type-parameter decision is owned by go-generics: if all you do is call a method, an interface is simpler than a type parameter.
Build larger interfaces by embedding smaller ones rather than re-listing methods. "it's easier and more evocative to embed the two interfaces to form the new one" (Effective Go):
// from the standard library:
type ReadWriter interface {
Reader
Writer
}
"A ReadWriter can do what a Reader does and what a Writer does; it is a union of the embedded interfaces" (Effective Go). Embedding keeps each piece small and lets a type satisfy the composite by satisfying the parts.
A type satisfies an interface implicitly, just by having the methods. To guarantee a type still satisfies an interface (and to get a clear compile error the moment it stops), use the blank-identifier assignment. "To guarantee that the implementation is correct, a global declaration using the blank identifier can be used" — var _ json.Marshaler = (*RawMessage)(nil) — and "that property will be checked at compile time" (Effective Go).
// Compile-time proof that *Handler implements http.Handler:
var _ http.Handler = (*Handler)(nil)
"The statement var _ http.Handler = (*Handler)(nil) will fail to compile if *Handler ever stops matching the http.Handler interface" (Uber Go Style Guide). The right-hand side is the zero value of the asserted type: nil for pointers, slices, and maps; an empty struct literal for struct types. Use it for exported types whose interface contract is part of their API.
Whether a type satisfies an interface depends on which receiver its methods use. "Methods with value receivers can be called on pointers as well as values. Methods with pointer receivers can only be called on pointers or addressable values" (Uber Go Style Guide). So a value-receiver method puts the method in both the value's and the pointer's method set, but a pointer-receiver method is in the pointer's method set only:
type F interface{ f() }
type S1 struct{}
func (s S1) f() {} // value receiver
type S2 struct{}
func (s *S2) f() {} // pointer receiver
var i F
i = S1{} // ok — value receiver
i = &S1{} // ok
i = &S2{} // ok — pointer
// i = S2{} // DOES NOT COMPILE: S2 value has no f in its method set
This is why "accept interfaces, return structs" usually returns a *T: a single pointer value satisfies interfaces whether the methods use value or pointer receivers.
!= nil Check Lies)An interface value is a pair. "Under the covers, interfaces are implemented as two elements, a type T and a value V" (Go FAQ); "A variable of interface type stores a pair: the concrete value assigned to the variable, and that value's type descriptor" (The Laws of Reflection). And critically: "An interface value is nil only if the V and T are both unset" (Go FAQ).
So if you return a concrete *MyError that happens to be nil, the error interface that receives it holds (T=*MyError, V=nil) — a non-nil interface. "Such an interface value will therefore be non-nil even when the pointer value V inside is nil" (Go FAQ). The caller's if err != nil is then unexpectedly true on the success path.
// WRONG — returns the concrete *MyError; on success p is a nil *MyError,
// but the returned error interface is (T=*MyError, V=nil) — NON-nil.
func buggyValidate(bad bool) error {
var p *MyError
if bad {
p = &MyError{Msg: "bad input"}
}
return p // caller's `err != nil` is ALWAYS true
}
// RIGHT — declare the return as error, and return an explicit nil on success.
func correctValidate(bad bool) error {
if bad {
return &MyError{Msg: "bad input"}
}
return nil
}
This is proven, not asserted: a test that calls buggyValidate(false) (nothing bad happened) finds err == nil is false — BUG reproduced: nothing bad happened, yet (err != nil) == true — while a type assertion confirms the dynamic value is a nil *MyError. The fixes, both from the FAQ: "the function must return an explicit nil," and "It's a good idea for functions that return errors always to use the error type in their signature ... rather than a concrete type such as *MyError" (Go FAQ). The standard library follows this: os.Open returns error, never the concrete *os.PathError. Declaring error returns (not concrete types) is owned by go-error-handling.
if err != nil { return err }, sees the error is "set," and burns an hour before learning the function returned a typed nil — the != nil was lying the whole time (Section 9).StoreInterface → store → mockStore maze generated "for testing," when a concrete return and a three-line consumer interface would have done the job (Section 4). "The bigger the interface, the weaker the abstraction" (Proverbs) is the empathy rule: every method on a too-big interface is a method the next person must understand to mock or change.func(any), who gets no compiler help and must read the body to learn which types are actually allowed (Section 5).go-idiomatic-discipline — the policy root. Producer-side interface pollution is its axis 2 (over-abstraction); this skill holds the depth.go-generics — the interface-vs-type-parameter decision. "If all you need to do with a value of some type is call a method on that value, use an interface type, not a type parameter."go-error-handling — declaring error (not *MyError) returns, wrapping with %w, and the typed-nil error in the context of error values.go-zero-values-and-construction — "accept interfaces, return structs" on the construction side: returning a useful concrete zero value rather than an interface.High-frequency interface anti-patterns in LLM-generated Go, each with wrong/right code and citations:
Source provenance for every claim in this skill:
What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub ctoth/golang-skills-plugin --plugin golang25 skills in this plugin
/go-channels-selectGuides Go channel mechanics — declare directionality in signatures (chan less than - send-only, less than -chan receive-only), let only the sender close and only when sends are done (closing is a broadcast, not cleanup), read the comma-ok / range-ends-on-close idiom so a closed channel's zero value is not mistaken for data, choose unbuffered (synchronization) vs buffered (decoupling) deliberately, use chan struct{} for pure signals, and drive select with default for non-blocking and the nil-channel trick to disable a case. Auto-invokes when writing or editing channels, chan declarations, close(), select statements, or buffered channels, and on "send on closed channel panic" / "why does this select block" / "why does my range over a channel never end" requests. The depth behind the policy root's "don't fight Go" for the communication primitive./go-concurrency-goroutinesGuides goroutine lifetime and ownership — never start a goroutine you can't stop, give every goroutine a defined exit (context cancellation, a closed channel, or bounded work), avoid the blocked-forever leak, use sync.WaitGroup correctly (Add before the go, or wg.Go in 1.25) and golang.org/x/sync/errgroup for fallible fan-out and SetLimit worker pools, and keep library functions synchronous so the caller owns concurrency. Auto-invokes when writing or editing `go` statements, goroutines, sync.WaitGroup, errgroup, worker pools, or on "is this goroutine leaking" / "how do I wait for these" requests. The depth behind the policy root's "never start a goroutine you can't stop."/go-contextGuides Go context.Context discipline — pass it as the first parameter named ctx and never store it in a struct, derive children with WithCancel/WithTimeout/WithDeadline and ALWAYS defer cancel() (go vet's lostcancel flags a missing one), check ctx.Done()/ctx.Err() in loops and before expensive work, start from context.Background() at entry points and context.TODO() when you don't have one yet, attach reasons with WithCancelCause/Cause and decouple with WithoutCancel/AfterFunc (1.20/1.21), and carry only request-scoped values behind an unexported key type. Auto-invokes when writing or editing functions taking context.Context, WithCancel/WithTimeout/WithValue, ctx.Done()/Err(), or on "where should ctx go" / "is this context leaking" requests. The propagation-and-cancellation depth behind the policy root's "never start a goroutine you can't stop."/go-defer-panic-recoverGuides Go's defer, panic, and recover — the LIFO ordering and arguments-evaluated-at-the-defer-statement semantics, why defer is near-free since open-coded defers (so don't avoid it except in tight loops), the loop-defer handle-leak pitfall, panic only for programmer bugs and unrecoverable states (not ordinary failure), and recover only inside a deferred function at a boundary to convert a panic into an error. Auto-invokes when writing or editing defer, panic, recover, or deferred cleanup, and on "why does this defer run in the wrong order", "should this panic", or "how do I stop a panic from crashing the process". Routes ordinary error-as-value handling to go-error-handling./go-error-handlingGuides Go error handling as values — wrap with fmt.Errorf and %w to preserve the chain, inspect with errors.Is (sentinel) and errors.As / errors.AsType (typed) instead of string-matching, choose sentinel vs custom error types and when they become API, combine with errors.Join, write lowercase un-punctuated messages, handle each error exactly once, and decorate or close-with-error via named returns and defer. Auto-invokes when writing or editing error returns, fmt.Errorf, errors.Is/As, custom error types, or on "handle this error" / "why is this error not matching" requests. The depth behind the policy root's "errors are values, never silently discarded."/go-genericsGuides Go generics restraint and constraint design — reach for a type parameter only when you would otherwise write the exact same code for multiple types (a container, or an algorithm over a slice/map/channel element type like map/filter/reduce), never when you only call a method on the value (that is an interface), and prefer the stdlib slices/maps/cmp helpers over hand-rolled generics. Covers constraints (any, comparable, cmp.Ordered, the ~T underlying-type element, unions like int | int64), type inference, generic type aliases (1.24), and self-referential constraints (1.26). Auto-invokes when writing or editing type parameters ([T any]), type constraints, generic functions or types, and on "should this be generic", "generic or interface here", or "make this generic". A type parameter earns its place only on real duplication; clarity first.