agentleFS
muratmirgunmuratmirgun/gophers/skills/go-code-style/SKILL.md

go-code-style

Skill9 starsChanged 4 months agoMITView on GitHub

Use when writing or reviewing Go code for clarity, formatting, control flow, variable declarations, switch usage, and function design. Covers the priority order (clarity > simplicity > concision > maintainability > consistency), gofmt rules, early-return / no-else patterns, switch over if-else chains, slice/map initialization, and value-vs-pointer choices. Apply proactively to every Go change, even when the user has not asked about style.

The file

We split this skill into the rules it gives an agent

8 rules. About 0 are common, 0 are distinctive, and 8 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 8

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Run gofmt/goimports: No exceptions, no debates.

    line 15
  2. Very specific:

    Eliminate unnecessary else: when the if body ends in return/break/continue.

    line 17
  3. Very specific:

    When the if body unconditionally exits (return/break/continue), drop the else.

    line 57
  4. Very specific:

    When all branches compare the same value, switch makes intent explicit and exhaustive can verify completeness:

    line 69
  5. Very specific:

    When an if has 3+ operands, hoist into named booleans so the names document the business rule:

    line 88
  6. Very specific:

    Use := for non-zero initializers, var when the zero value is the start.

    line 102
  7. Very specific:

    Do not speculatively preallocate large capacities — make([]T, 0, 1000) wastes memory when the common case is 10.

    line 121
  8. Very specific:

    Add to .golangci.yml and run golangci-lint run in CI.

    line 173

≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Go Code Style

gofmt handles the mechanical layout. This skill covers the decisions a formatter cannot make: where to break complexity, when to invert an if, when a switch beats an else if chain, and how to pick value vs pointer arguments. The rule of thumb is the Go Proverb: clear is better than clever.

Core Rules

  1. Run gofmt/goimports. No exceptions, no debates.
  2. Handle errors and edge cases first, return early, keep the happy path at minimum indentation.
  3. Eliminate unnecessary else when the if body ends in return/break/continue.
  4. Prefer switch over if/else if chains that compare the same value.
  5. Initialize slices and maps explicitly, never let a nil map reach a write.
  6. Pass small values; pass pointers when mutating or when the type is large (~128B+).

Style Priority Order

Apply in this order when two principles collide.

PriorityQuestionBeats
1. ClarityCan a reader understand intent without extra context?everything else
2. SimplicityIs this the simplest expression of the idea?concision, consistency
3. ConcisionDoes every line earn its place?consistency
4. MaintainabilityWill this be safe to modify?consistency
5. ConsistencyDoes it match nearby code?—

A "consistent" piece of bad code is still bad code. Clarity wins.

Read references/formatting-and-layout.md for line-breaking, multi-line signatures, file/declaration order.

Control Flow

Early return: keep the happy path flat

func process(data []byte) (*Result, error) {
    if len(data) == 0 {
        return nil, errors.New("empty data")
    }
    parsed, err := parse(data)
    if err != nil {
        return nil, fmt.Errorf("parsing: %w", err)
    }
    return transform(parsed), nil
}

No unnecessary else

When the if body unconditionally exits (return/break/continue), drop the else. For assignments, prefer default-then-override:

// Good
lvl := slog.LevelInfo
if debug {
    lvl = slog.LevelDebug
}

Switch over if/else chains

When all branches compare the same value, switch makes intent explicit and exhaustive can verify completeness:

switch status {
case StatusActive:
    activate()
case StatusInactive:
    deactivate()
case StatusPaused:
    pause()
default:
    panic(fmt.Sprintf("unexpected status: %d", status))
}

A tagless switch replaces a chain of unrelated if/else if conditions — first matching case wins.

Extract complex conditions

When an if has 3+ operands, hoist into named booleans so the names document the business rule:

isAdmin := user.Role == RoleAdmin
isOwner := resource.OwnerID == user.ID
if isAdmin || isOwner || permissions.Has(PermOverride) {
    allow()
}

Read references/control-flow.md for tagless switch, guard clauses, and labelled break/continue.

Variable Declarations

Use := for non-zero initializers, var when the zero value is the start.

var count int          // start at 0
name := "default"      // non-zero
var buf bytes.Buffer   // zero value is ready to use

Initialize slices and maps explicitly

A nil map panics on write; a nil slice JSON-encodes to null (a UX surprise for API consumers).

users := []User{}                       // explicit empty
m := map[string]int{}                   // explicit empty
users = make([]User, 0, len(ids))       // preallocate when size is known
m = make(map[string]int, len(items))    // preallocate map buckets

Do not speculatively preallocate large capacities — make([]T, 0, 1000) wastes memory when the common case is 10.

Composite literals: name the fields

srv := &http.Server{
    Addr:         ":8080",
    ReadTimeout:  5 * time.Second,
    WriteTimeout: 10 * time.Second,
}

Positional fields break the moment the type adds or reorders a field.

Function Design and Argument Passing

  • Short and focused, ≤ 4 parameters. Beyond that, use an options struct or functional options.
  • Parameter order: ctx context.Context first, then inputs, then output destinations.
  • range over index loops (range n since Go 1.22).
  • Pass small values, pointers for mutation / large structs (~128B+) / meaningful nil. *string, *int parameters add indirection with no real saving. sync.Mutex and types embedding one are never copied — go vet copylocks catches it.

Anti-Patterns

Anti-patternWhy it hurtsDo this instead
Deeply nested if chainsHappy path scrolls off-screenInvert, return early
if ok { return a } else { return b }else is dead weightDrop the else
if x == A else if x == B else if x == CReader has to verify all comparisons share xswitch x { ... }
Positional struct literalBreaks silently on field reorderNamed fields
Nil map writeRuntime panicmake(...) or map literal
*string, *int parameter to "save copy"Adds indirection, no real savingPass value
Long parameter listsHard to call, hard to extendOptions struct or WithXxx opts

Verification Checklist

  • gofmt -l . produces no output and goimports -l . is clean.
  • No function body indented past three tab stops without justification.
  • No if ... return; else ... in modified code.
  • All maps and slices declared without literal use make with a sensible capacity.
  • Composite literals use named fields for non-trivial struct types.
  • ctx context.Context is the first parameter on every function that takes one.
  • golangci-lint run passes with gocritic, revive, and gocyclo enabled.

Enforce With Linters

These rules are largely mechanical and a linter will catch them in CI:

  • gofmt, gofumpt, goimports — formatting.
  • gocritic, revive — style heuristics, including if-return, early-return.
  • gocyclo, funlen — function complexity / length.
  • wsl_v5 — whitespace lines for separation between blocks.

Add to .golangci.yml and run golangci-lint run in CI.

References

  • references/formatting-and-layout.md — gofmt, line breaks, multi-line signatures, file order
  • references/control-flow.md — early returns, switch patterns, labelled loops
  • references/function-and-data-init.md — declarations, composite literals, value-vs-pointer

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.