agentleFS
narenaryannarenaryan/agent-skills/skills/go/go-data-idioms/SKILL.md

go-data-idioms

Skill0 starsChanged 40 days agoMITView on GitHub

Use when working with Go slices, maps, defer, or named returns — covers new vs make, slice capacity and backing-array aliasing, append reallocation, defer LIFO and argument-capture timing, named returns mutated by defer, and zero-value-useful design

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 0 are distinctive, and 1 is 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.

  • 1

    Very specific

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

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

    defer in a loop: all defers run at function exit, not per iteration — handles accumulate.

    line 83

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 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 Data Idioms

Slices are (ptr, len, cap) headers over a shared backing array; aliasing is the default. defer evaluates args at the defer site, not at return. Design types so the zero value is usable.

new vs make

CallReturnsFor
new(T)*T, zeroedstructs, ints, arrays
make(T, …)T, initializedslices, maps, channels

new([]int) yields *[]int pointing to a nil slice — rarely useful. Use make([]int, n).

Slices: aliasing and append

s := []int{1, 2, 3, 4}
t := s[1:3]            // shares backing; t[0]=9 mutates s[1]
u := append(s, 5)      // may or may not reallocate

s = append(s, x)       // MUST reassign
append(s, x)           // lost — returned header discarded

Reallocation when len + added > cap. Before realloc, original and returned share memory; after, they don't. Treat the return as authoritative.

Maps: comma-ok for absence

v, ok := m[k]    // ok=false means absent
_, ok := m[k]    // presence-only

Missing key silently returns zero value; without ok you cannot tell "absent" from "present and zero".

Defer: LIFO and argument capture

for i := 0; i < 3; i++ {
    defer fmt.Println(i)    // args evaluated NOW → 2, 1, 0
}

x := 1
defer func() { fmt.Println(x) }()
x = 2                       // prints 2 — closure reads at call time

Direct-call defer captures args at the defer statement. A func(){} literal reads variables when it runs.

Named returns + defer = post-return mutation

func parse() (out T, err error) {
    defer func() {
        if err != nil { err = fmt.Errorf("parse: %w", err) }
    }()
    ...
    return   // defer sees and rewrites err
}

Works only with named returns. Defer runs after return operands evaluate, before control leaves the frame.

Zero-value-useful

type Buf struct {
    mu  sync.Mutex       // zero: unlocked
    buf bytes.Buffer     // zero: empty, usable
}
var b Buf                // no constructor needed

Pitfalls

  • Forgetting to reassign append: silent element loss when cap is exceeded.
  • Slice retention: big[0:1] keeps the whole backing array alive. copy into a fresh slice when keeping a small prefix.
  • defer in a loop: all defers run at function exit, not per iteration — handles accumulate. Wrap iteration in a helper.
  • Closure vs argument capture: defer f(x) captures x now; defer func(){ f(x) }() captures x at execution.
  • Nil map write panics: var m map[string]int; m["k"] = 1 panics. Always make before writing.
  • Two-dim slice allocation: one contiguous make([]T, x*y) sliced into rows beats per-row make when rows have fixed size.

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.