Skip to content
Back to skills

Design

ASecurity

Core pattern for flexible, testable Go APIs

  • 3 stars
  • 0 votes
  • 0 copies
  • 14 views
  • Added February 7, 2026
developmentgoapi

Works with

  • api

Security analysis

A100/100

Scanned February 12, 2026

npx -y skills add JamesPrial/claudefiles --skill design --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Design?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Design
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/jamesprial-design/badge)](https://www.skillsdirectory.com/skills/jamesprial-design)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: accept-interfaces-return-structs
description: Core pattern for flexible, testable Go APIs
---

# Accept Interfaces, Return Structs

The fundamental Go interface design principle: functions should accept interfaces but return concrete types.

## The Pattern

**CORRECT** - Accept interface, return concrete
```go
type Storage interface {
    Save(data []byte) error
}

func NewProcessor(s Storage) *Processor {
    return &Processor{storage: s}
}

func (p *Processor) Process(input string) (*Result, error) {
    // Returns concrete *Result, accepts Storage interface
    return &Result{Value: input}, nil
}
```

**WRONG** - Return interface unnecessarily
```go
func NewProcessor(s *FileStorage) Storage {
    // Locks caller into interface, prevents direct method access
    return &Processor{storage: s}
}
```

## Why This Works

**Accepting interfaces:**
- Caller controls abstraction
- Easy to mock/test
- Flexible composition

**Returning concrete types:**
- No hidden behaviors
- All methods visible
- Can add methods without breaking compatibility

## When to Deviate

**Return interface when:**
```go
func NewLogger(env string) io.Writer {
    // Valid: stdlib interface, multiple implementations
    if env == "prod" {
        return &fileLogger{}
    }
    return &consoleLogger{}
}
```

Return interfaces only when:
- Using stdlib interfaces (io.Writer, io.Reader)
- Multiple implementations chosen at runtime
- Interface already well-established in ecosystem

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.