Skip to content
OpenAgentSkill
Browsing is open · Catalog captured 2026-11-21 · Automatic updates and account services are paused.
Development

go-code-organization

Guides the agent to write well-organized Go code and projects. Use when writing or reviewing Go code involving variable scoping, nested control flow, init functions, getters/setters, interfaces, generics, type embedding, functional options, package structure, utility packages, package naming, code documentation, or linter configuration. Covers the 16 most common code and project organization mistakes in Go.

v0lka · Static checks on file

Installation

Read the source and dependencies before running a command. Copying does not install or execute anything.

npx skills add v0lka/skills --skill go-code-organization

About this skill


name: go-code-organization description: > Guides the agent to write well-organized Go code and projects. Use when writing or reviewing Go code involving variable scoping, nested control flow, init functions, getters/setters, interfaces, generics, type embedding, functional options, package structure, utility packages, package naming, code documentation, or linter configuration. Covers the 16 most common code and project organization mistakes in Go.

Go Code and Project Organization — Rules and Patterns

1. Variable shadowing

A variable declared with := in an inner block shadows the outer variable of the same name. The outer variable remains unchanged, which is almost always a bug.

Wrong:

var client *http.Client
if tracing {
    client, err := createClientWithTracing() // shadows outer client
    if err != nil {
        return err
    }
    log.Println(client)
} else {
    client, err := createDefaultClient() // shadows outer client
    if err != nil {
        return err
    }
    log.Println(client)
}
// client is still nil here

Fix — pre-declare err, use = instead of :=:

var client *http.Client
var err error
if tracing {
    client, err = createClientWithTracing()
} else {
    client, err = createDefaultClient()
}
if err != nil {
    return err
}

Alternative: assign := to a temporary variable (c), then client = c after the error check.

Detection: Run go vet -vettool=$(which shadow) (install with golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow).

2. Unnecessary nesting

Keep the happy path left-aligned. Reduce nesting by returning early on errors and flipping conditions.

Rules:

  • When an if block returns, omit the else.
  • If the non-happy path is in the else, flip the condition.
  • Aim for a maximum of two indent levels inside a function body.

Wrong:

if s != "" {
    // ... long happy path
} else {
    return errors.New("empty string")
}

Correct:

if s == "" {
    return errors.New("empty string")
}
// ... happy path at top level

Apply this recursively: replace every if ... { return } else { ... } chain with guard clauses that return early, keeping the happy path at the lowest indent level.

3. init functions

Avoid init when:

  • The initialization can fail — init cannot return errors, so the only signal is log.Fatal/panic, removing the caller's ability to retry or fall back.
  • It sets global mutable state — makes testing harder and any function in the package can alter the global.
  • The side effect is not needed by every test in the file.

Acceptable uses of init:

  • Registering static, infallible configuration (e.g., http.HandleFunc with non-nil handlers).
  • Side-effect imports (_ "image/png") for codec registration.

Wrong — DB connection in init:

var db *sql.DB

func init() {
    d, err := sql.Open("mysql", os.Getenv("DSN"))
    if err != nil {
        log.Panic(err) // caller can't handle this
    }
    db = d
}

Correct — explicit constructor:

func NewDB(dsn string) (*sql.DB, error) {
    db, err := sql.Open("mysql", dsn)
    if err != nil {
        return nil, err
    }
    if err = db.Ping(); err != nil {
        return nil, err
    }
    return db, nil
}

4. Getters and setters

Go does not require getters and setters. Do not add them unless they provide value (validation, computed values, mutex wrapping, debugging interception, or forward compatibility).

Naming convention when used:

  • Getter: Balance() (not GetBalance())
  • Setter: SetBalance(v int)
currentBalance := customer.Balance()
if currentBalance < 0 {
    customer.SetBalance(0)
}

The standard library exposes struct fields directly when appropriate (e.g., time.Timer.C). Follow the same pragmatism.

5. Interface pollution

Core principle: Abstractions should be discovered, not created. Do not define an interface until you have a concrete need.

Valid reasons to create an interface:

  1. Common behavior — multiple types share the same method set (e.g., sort.Interface).
  2. Decoupling — swap implementations for testing or Liskov substitution.
  3. Restricting behavior — expose only a subset of a type's methods (e.g., read-only config getter from a read-write config struct).

Do not create interfaces preemptively "in case we need them later." If it is unclear how an interface improves the code, remove it.

Keep interfaces small. "The bigger the interface, the weaker the abstraction." (Rob Pike)

6. Interface on the consumer side

Interfaces should live in the package that uses them, not in the package that implements them.

  • The producer exports the concrete struct.
  • Each consumer defines only the interface it needs (possibly a single-method interface), keeping it unexported.

Wrong — producer-side interface:

// package store
type CustomerStorage interface {  // forces all consumers into this abstraction
    StoreCustomer(Customer) error
    GetCustomer(id string) (Customer, error)
    // ... 4 more methods
}

Correct — consumer-side interface:

// package client
type customersGetter interface {          // unexported, minimal
    GetAllCustomers() ([]store.Customer, error)
}

Exception: An interface on the producer side is acceptable when you know (not foresee) it will be used by many consumers (e.g., encoding.BinaryMarshaler). Keep it as small as possible.

7. Returning interfaces

Functions should return concrete types, not interfaces. Returning an interface:

  • Creates a dependency from the implementation package to the client package.
  • Forces every consumer into the same abstraction level.

Guideline (Postel's law applied to Go):

  • Accept interfaces.
  • Return structs.

Exceptions: The error interface (ubiquitous), and up-front abstractions proven to be universally useful (e.g., io.LimitReader returns io.Reader).

Read the complete source ↗