c/backend › command
0votes

Cursor rules for Go

TemplateLow risk Tested
submitted by CommandVault editors to c/backend · 3 uses

A .cursor/rules/go.mdc for idiomatic errors, contexts, interfaces and tests.

Template · 1 variable
---
description: Go conventions
globs: ["**/*.go"]
alwaysApply: false
---

# Go

- Errors are values: return them, wrap with `%w`, and handle each one. No panics in library code.
- `context.Context` first argument for anything doing I/O; respect cancellation.
- Accept interfaces, return structs; define interfaces where they're used.
- Keep packages small and named for what they provide (no `utils`).
- Table-driven tests with `t.Run`; use `t.Helper()` in helpers.
- Run `go vet` and `golangci-lint run` before finishing.

## When you change code
- Follow the patterns in the nearest existing file before inventing new ones.
- Add or update a test for behavior changes; run `{{test_command}}`.
- Keep diffs small; explain anything surprising in your reply.
0 commentsreport

Guide

What you get: The agent writes idiomatic Go: ctx first, errors wrapped with %w, no panics, consumer-side interfaces, table-driven tests with t.Run, and it runs go vet and golangci-lint before finishing. Best for Go developers who want agents to follow standard Go error, context and testing idioms..

Before you start

  • A Go module
  • golangci-lint installed (the rules tell the agent to run it)
  • Fill test_command (we used go test ./...)
  • Cursor: .cursor/rules/go.mdc. Claude Code/Codex: the same text in CLAUDE.md / AGENTS.md

Worked example (Nov 14, 2026, Claude Code 2.1.318 (claude-opus-5-5); Codex 0.151.0 (gpt-5.6-sol))

Input

A small module (inventory service + in-memory warehouse client). Task: reserve a whole order (list of SKU + qty); if any item fails, release what was already reserved and return an error naming the SKU.

Result

Claude Code: ReserveOrder(ctx, []Item) with an ItemError type (Unwrap), release via context.WithoutCancel, errors.Join for release failures. Codex: ReserveOrder with fmt.Errorf("reserve order: sku %s: %w"), releases in reverse order.

Not tested in Cursor or Copilot: the rule text was saved verbatim (front matter included) as CLAUDE.md for Claude Code and AGENTS.md for Codex, the files those agents load at startup, plus a copy at its normal path. Go 1.27.4 and golangci-lint 2.15.0. Both ran go vet, go test and golangci-lint run themselves; our re-run: gofmt clean, vet clean, tests pass, golangci-lint 0 issues, no panics, table-driven tests with t.Run. Codex also renamed the existing exported Reserver interface to StockManager, which wasn't needed (a bigger diff than 'keep diffs small' suggests).

How to check it worked

  • Run go vet ./..., go test ./... and golangci-lint run
  • Check errors.Is still works through the new wrapping
  • Look for renamed exported identifiers in the diff

Common problems

  • golangci-lint not installed: the agent can't follow the last rule; it should say so
  • Agents may widen or rename an existing interface when they need a new method

How to add this Cursor rule

  1. Fill in the blank above; Copy puts your answers into the file.
  2. Save it in .cursor/rules/ with the .mdc extension (plain .md files there are ignored), e.g. .cursor/rules/nextjs.mdc.
  3. The front matter decides when it applies: alwaysApply: true for every chat, globs for matching files, or a description so Cursor applies it when relevant.
  4. Check it in Cursor's Rules settings, then ask something about a matching file and confirm the answer follows the rule.
  • {{test_command}} Test command

Checked against Cursor docs: rules.

More from c/backend

See all of c/backend →
0 comments

Log in to join the discussion.