Skip to main content
skill CX Codex

go-interfaces — Claude Code skill: what it does, how to install

Guides coding agents through Go interface design, type composition, and compile-time checks. Prevents premature abstractions in public APIs.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add cxuu/golang-skills --skill go-interfaces -a claude-code
Open on GitHub

What go-interfaces does

This skill guides the agent in applying idiomatic Go design rules: define interfaces where values are consumed, and return concrete structs from constructors. Returning an interface from a constructor is restricted to cases where an unexported type exists strictly to hide implementation details. The reference also details safe comma-ok type assertions, type switch variable reuse, and consistent pointer receiver selection.

To safeguard public APIs, it advises against embedding inner types directly into exported structs, preventing unintended method exposure. For critical boundaries, it requires compile-time verification using var _ I = (*T)(nil). The package includes scripts/check-interface-compliance.sh and scripts/check-interface-compliance.go to flag exported types missing these static assertions via Bash.

Generics-based polymorphism is intentionally out of scope and delegated to go-generics. Unchecked type assertions cause panics, making compile-time verification the safest baseline.

What it helps with

  • design consumer-owned Go interfaces without premature abstraction
  • enforce compile-time interface compliance using var _ I = (*T)(nil)
  • prevent unintended method promotion when composing structs in public APIs
  • implement safe type assertions using the comma-ok idiom

Example prompts for the agent

  • Design a consumer-side interface for this Go storage repository
  • Add compile-time interface satisfaction checks to these exported structs
  • Run scripts/check-interface-compliance.sh to find missing interface assertions

How to install go-interfaces

1Skills CLI (Vercel)

npx skills add cxuu/golang-skills --skill go-interfaces -a claude-code

2Manually: put the files in place

~/.claude/skills/go-interfaces/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/go-interfaces/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about go-interfaces

How do I install go-interfaces in Claude Code?

Run npx skills add cxuu/golang-skills --skill go-interfaces -a claude-code. This adds the skill directly to your Claude Code configuration.

What does go-interfaces do?

It provides guidelines for Go abstractions, enforcing the accept interfaces and return concrete types principle, comma-ok assertions, and compile-time interface checks.

Does go-interfaces cover Go generics?

No, generics-based polymorphism is excluded. The author covers generic type parameters in a separate skill named go-generics.

Source text

SKILL.md · Apache-2.0
---
name: go-interfaces
description: Use when defining or implementing Go interfaces, designing abstractions, creating mockable boundaries for testing, or composing types through embedding. Also use when deciding whether to accept an interface or return a concrete type, or using type assertions or type switches, even if the user doesn't explicitly mention interfaces. Does not cover generics-based polymorphism (see go-generics).
allowed-tools: Bash(bash:*)
---

# Go Interfaces and Composition

## Resource Routing

- `scripts/check-interface-compliance.sh` - Run as a heuristic to find exported interfaces that may need compile-time assertions.
- `scripts/check-interface-compliance.go` - Implementation helper invoked by `check-interface-compliance.sh`; patch this when changing method-set analysis.
- `references/EMBEDDING.md` - Read when embedding interfaces or structs in public APIs.
- `references/RECEIVER-TYPE.md` - Read when pointer/value receivers affect interface satisfaction.

---

## 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.

---

## 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 scripts/check-interface-compliance.sh` to verify all concrete types have compile-time `var _ I = (*T)(nil)` checks.

---

## Receiver Type

If in doubt, use a pointer receiver. Don't mix receiver types on a single
type — if any method needs a pointer, use pointers for all methods. Use value
receivers only for small, immutable types (`Point`, `time.Time`) or basic types.

---

## Quick Reference

| Concept | Pattern | Notes |
|---------|---------|-------|
| Consumer owns interface | Define interfaces where used | Not in the implementing package |
| Safe type assertion | `v, ok := x.(Type)` | Returns zero value + false |
| Type switch | `switch v := x.(type)` | Variable has correct type per case |
| Interface embedding | `type RW interface { Reader; Writer }` | Union of methods |
| Struct embedding | `type S struct { *T }` | Promotes T's methods |
| Interface check | `var _ I = (*T)(nil)` | Compile-time verification |
| Generality | Return interface from constructor | Hide implementation |

---

## Related Skills

- **Interface naming**: See [go-naming](../go-naming/SKILL.md) when naming interfaces (the `-er` suffix convention) or choosing receiver names
- **Error types**: See [go-error-handling](../go-error-handling/SKILL.md) when implementing the `error` interface, custom error types, or `errors.As` matching
- **Generics vs interfaces**: See [go-generics](../go-generics/SKILL.md) when deciding whether generics are needed or an interface already suffices
- **Functional options**: See [go-functional-options](../go-functional-options/SKILL.md) when using an interface-based Option pattern for flexible constructors
- **Defensive boundaries**: See [go-defensive](../go-defensive/SKILL.md) when interface assertions are one part of a broader API-boundary hardening pass

© cxuu · Apache-2.0 · cxuu/golang-skills

Similar skills

All skills for Programming languages →

From the same set19

go-naming A skill that keeps Go names idiomatic: packages, types, functions, constants, receivers. Useful whenever an agent creates new types or exported APIs. CX Codex +1 1,200installs go-context Enforces idiomatic context.Context rules in Go, covering parameter placement, cancellation deadlines, and request data boundaries. CX Codex +1 1,200installs go-style-core A skill that lays out core Go style rules: formatting, nesting, naked returns, semicolons. Useful whenever the agent writes or edits Go code. CX Codex +1 1,200installs go-performance A skill that points your agent at Go hot paths: strconv over fmt, capacity hints for slices and maps, strings.Builder in loops. CX Codex +1 1,200installs go-declarations A skill that tells your coding agent how to declare and initialize Go variables, constants, structs and maps. Useful the moment you write a new struct or const block. CX Codex +1 1,000installs go-generics A skill that helps you decide when Go generics are worth it, how to pick constraints, and when a plain type or interface is the better call. CX Codex +1 1,000installs go-functions Organizes Go file structure, formats long function signatures, and enforces Printf naming conventions. Helpful when writing or refactoring Go methods. CX Codex +1 1,000installs go-logging A skill that steers your coding agent toward sane Go logging: slog, levels, structured fields, request-scoped context. Useful once a Go service grows more log lines than anyone reads. CX Codex +1 1,000installs

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor