AGENTS.md template for Go
A ready AGENTS.md for a Go service or CLI. It tells agents your package layout, the vet, lint and test commands, and the Go idioms models break when they write Go like another language.
# AGENTS.md A Go service using the standard library where it can, with modules for dependency management. ## Commands - `go build ./...`: compile everything - `go test ./...`: run all tests - `go test -race ./...`: run tests with the race detector; use for anything concurrent - `go vet ./...`: static checks - `golangci-lint run`: lint - `gofmt -l .`: list unformatted files; should print nothing ## Project structure - `cmd/<name>/main.go`: entry points; wiring only - `internal/`: application packages, not importable from outside the module - `internal/<domain>/`: one package per domain, named for what it provides - `go.mod, go.sum`: dependencies. Change only through `go get` and `go mod tidy` ## Rules for agents - Handle every error or return it wrapped with context (`fmt.Errorf("...: %w", err)`). Never discard one with `_`. - Define an interface where it is used, and only when there are two implementations or a test needs one. - Every goroutine has a way to stop. Pass `context.Context` as the first argument to anything that blocks. - Prefer the standard library. Ask before adding a dependency. ## Conventions ### Go - Go 1.22+ features are fair game. Prefer the standard library. - Errors as values — wrap with `fmt.Errorf("context: %w", err)` and check with `errors.Is/As`. - Return early. Avoid nested `if err == nil` pyramids. - Accept interfaces, return structs. Keep interfaces small — define them where consumed. - Context propagation: every I/O function takes `ctx context.Context` as the first arg. - Table-driven tests with `t.Run(name, ...)` subtests. ## Avoid - Panicking in library code for recoverable errors. - Stuffing everything in `package main`. ## Guardrails - Never commit secrets, API keys, or credentials. If you spot one in a diff, stop and flag it. - Do not invent dependencies. If a package is not already in the manifest, confirm before adding it. - When a command fails, read the actual error. Do not retry blindly or bypass with `--force`. - Prefer editing existing files over creating new ones. - Run the tests and the linter before saying a task is done, and say what you ran.
The commands and folders are the common defaults for Go. Change them to match your project before you commit the file: an agent will run exactly what is written here.
How to use it
Save the file as AGENTS.md in the root of your repository and commit it. Then check each tool you use:
| Tool | What to do |
|---|---|
| Claude Code | Reads AGENTS.md only when no CLAUDE.md exists in or above the folder. If you have a CLAUDE.md, put @AGENTS.md on its first line. |
| Cursor | Reads AGENTS.md from the project root, in the desktop app and the terminal. In the app, rules in .cursor/rules must be .mdc files. The terminal reads only plain .md files there. |
| Codex | Reads AGENTS.md from the root down to the folder you are in, with a shared 32 KiB limit across all of them. |
| GitHub Copilot | Reads AGENTS.md on some surfaces only. Keep a .github/copilot-instructions.md as well if your team relies on Copilot. |
Details and sources: the AGENTS.md compatibility matrix.
What agents get wrong in Go projects
These are the reasons the “Rules for agents” section says what it says.
Ignored errors
result, _ := doThing() is how generated Go makes the compiler happy. The template requires every error to be handled or returned with context.
Java-shaped Go
Agents create an interface for every struct, plus getters, setters and a utils package. The template asks for interfaces at the point of use and only when there are two implementations or a test needs one.
Goroutines with no exit
Generated concurrency often starts a goroutine with nothing to stop it and no context passed in. The race detector command is there for this.
A new dependency for a standard-library job
Models pull in a router, a logger and an assertion library by reflex. Say what you already use.
Questions
Where does AGENTS.md go in a Go project?
In the root of the repository, next to your manifest file. Tools read it from the folder you start them in and, for most tools, from parent folders too.
Does Claude Code read AGENTS.md?
Only when there is no CLAUDE.md in or above the working folder. If you already have a CLAUDE.md, keep it and make its first line @AGENTS.md so both tools read the same rules.
How long should the file be?
Short enough that every line is something the agent would get wrong without it. Codex shares a 32 KiB budget across all the AGENTS.md files it loads, and long files get followed less closely by every tool.
Should generated code be mentioned?
Yes. If you use go generate, sqlc, protobuf or mocks, list the output folders and the command that regenerates them, and say they are never edited by hand.