agentleFS
wtsi-hgiwtsi-hgi/agentskills/skills/go-conventions/SKILL.md

go-conventions

Skill1 starsChanged 40 days agoMITView on GitHub

Shared conventions for Go projects. Copyright boilerplate, code quality, GoConvey and fuzz testing, architecture, and commands. Referenced by go-implementor, go-reviewer, and workflow skills.

4 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

10 rules. 0 are common, 5 are distinctive, and 5 are very specific to this project.

  • 0

    Common

    Most of the 3 edited versions we read keep these.

  • 5

    Distinctive

    This author's own choices. Worth a look.

  • 5

    Very specific

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

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    All new source files must start with:

    line 12
  2. Distinctive:

    Import grouping: stdlib | third-party | project (blank-line separated).

    line 63
  3. Very specific:

    Never put So() in loops >20 iterations.

    line 71
  4. Very specific:

    Use the matching assertion (ShouldWrap, ShouldHaveLength, ...), not So(cond, ShouldBeTrue).

    line 72
  5. Very specific:

    Never block on a bare channel receive.

    line 77
  6. Distinctive:

    Follow testing-principles for test intent.

    line 79
  7. Distinctive:

    Every spec.md acceptance test MUST have a corresponding GoConvey test.

    line 81
  8. Very specific:

    Commit the crasher. A failure writes its input to <pkg>/testdata/fuzz/FuzzX/<hash>, beside the package and not at the module root.

    line 119
  9. Very specific:

    Put genuinely shared internal helpers, mocks, and utilities in internal/.

    line 153
  10. Distinctive:

    Follow implementation-principles for reuse and abstraction decisions.

    line 154

“Common” is measured: kept by at least 6 in 10 of the 3 edited versions of this file we could read. “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 Conventions

Single source of truth for Go code standards. Other skills reference this.

Copyright Boilerplate

All new source files must start with:

/*******************************************************************************
 * Copyright (c) 2026 Genome Research Ltd.
 *
 * Author: Sendu Bala <sb10@sanger.ac.uk>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 ******************************************************************************/

Code Quality

Modern Go (1.25+)

  • Range over integers: for i := range n (no C-style loops in new code).
  • log/slog over log or fmt.Println for operational output.
  • fmt.Errorf with %w; sentinel errors as package-level var with errors.New; errors.Is, or errors.AsType[T] rather than errors.As with a target variable.
  • time unit constants, not raw nanosecond counts; for b.Loop() in benchmarks; defer for a function's final cleanup step.
  • slices/maps packages instead of hand-written loops.
  • Named return values only when they genuinely aid readability.
  • Goroutines must have clear exit paths; use context.Context for cancellation.

Style

  • Functions ~30 lines max (excluding error handling), but don't scatter logic across too many tiny helpers.
  • Early returns/guard clauses over nested if/else.
  • Doc comments on all exports.
  • One responsibility per file; filenames describe the responsibility.
  • Import grouping: stdlib | third-party | project (blank-line separated).

Testing (GoConvey)

  • Framework: github.com/smartystreets/goconvey/convey.
  • So(actual, ShouldEqual, expected) assertions - never bare if checks.
  • t.TempDir() for filesystem ops.
  • Each Convey block independent; no shared mutable state.
  • Never put So() in loops >20 iterations; count and assert final count.
  • Use the matching assertion (ShouldWrap, ShouldHaveLength, ...), not So(cond, ShouldBeTrue).
  • Undo state changes (permissions, env, globals) with Reset, registered before the change; a failing So skips any later defer. Pass cleanup functions directly: Reset(h.Cleanup).
  • Never block on a bare channel receive; select with a timeout so a missed signal fails the test instead of hanging it.
  • Follow testing-principles for test intent; this section covers GoConvey mechanics.
  • Every spec.md acceptance test MUST have a corresponding GoConvey test. No stubs, no hardcoded results, no swallowed failures, no build-tag exclusions.

Fuzzing

Reach for a fuzz target when a claim must hold across a whole input domain rather than at the worked examples a spec names: overflow and rounding safety over a numeric range, a round-trip through an encoder, a parser that must never panic. testing-principles decides when one is owed and what property to assert; this section covers the Go mechanics.

func FuzzScale(f *testing.F) {
    f.Add(int64(0), int64(1), int64(1))
    f.Add(int64(math.MaxInt64), int64(2), int64(2))

    f.Fuzz(func(t *testing.T, value, num, den int64) {
        if den == 0 {
            return // the documented panic has its own Convey test
        }

        got, want := mathx.Scale(value, num, den), slowExactScale(value, num, den)
        if got != want {
            t.Fatalf("Scale(%d,%d,%d) = %d, want %d", value, num, den, got, want)
        }
    })
}
  • Assert with plain if and t.Fatalf inside f.Fuzz, not So(). The body runs millions of times, so the loop rule above applies.
  • f.Add every boundary, zero and sign combination. Without -fuzz, go test runs the seed corpus and nothing else, so the seeds are what CI executes and they must stand on their own as a table test.
  • Assert a property, never a second copy of the implementation. A target that recomputes the function the same way proves only self-consistency.
  • Constrain the domain in the target and leave documented panics and sentinel errors to their Convey tests.
  • Commit the crasher. A failure writes its input to <pkg>/testdata/fuzz/FuzzX/<hash>, beside the package and not at the module root. That file is the regression test: it then runs under plain go test, so git add it with the fix.

Memory-Bounded Test Pattern

func TestStreamingMemory(t *testing.T) {
    // 1. Create large input (1M entries in t.TempDir())
    // 2. Measure baseline:
    runtime.GC()
    var before runtime.MemStats
    runtime.ReadMemStats(&before)
    // 3. Run the streaming operation
    // 4. Measure after:
    runtime.GC()
    var after runtime.MemStats
    runtime.ReadMemStats(&after)
    // 5. Assert (guard unsigned underflow):
    var growth uint64
    if after.HeapInuse > before.HeapInuse {
        growth = after.HeapInuse - before.HeapInuse
    }
    So(growth, ShouldBeLessThan, 20*1024*1024)
}

Architecture

  • cmd/*.go: CLI-only (flag parsing, wiring). No business logic.
  • New public packages for new functionality, fully tested via TDD.
  • internal/ for shared helpers, mocks, generic utilities.
  • main_test.go for integration tests (no mocks, real end-to-end).
  • Put genuinely shared internal helpers, mocks, and utilities in internal/. Follow implementation-principles for reuse and abstraction decisions.

Commands

# Tests
CGO_ENABLED=1 go test -tags netgo --count 1 ./<path> -v -run <TestFunc>

# Fuzz exploration (bounded; without -fuzz only the seed corpus runs)
go test ./<path> -fuzz <FuzzFunc> -fuzztime 60s

# Linter (check + autofix)
golangci-lint run --fix

# Clean ordering
cleanorder -min-diff <file>

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.