Go Code Style
Make code easier to understand without turning preferences into universal rules. Preserve behavior, public APIs, and repository conventions unless the user explicitly asks to change them.
Establish the local style
Before editing:
- Read repository instructions,
go.mod, formatter and linter configuration, and nearby code.
- Identify whether the task is implementation, focused review, or project-wide policy design, and keep the change at that scale.
- Use
gofmt as the formatting baseline. Use stricter formatters only when the project already configures them.
- Prefer established local conventions when several idiomatic forms are equally clear.
Clarity heuristics
Apply these with judgment:
- Handle exceptional paths early when that makes the main flow flatter. Keep an
else when it expresses a real two-way decision more clearly.
- Give intermediate values names when they reveal domain meaning or make a condition auditable. Do not extract trivial expressions merely to satisfy an operand count.
- Keep a value's scope narrow, including
if initializers when the value is used only by that branch.
- Split functions when the extracted operation has a coherent responsibility, invariant, or test boundary—not because of an arbitrary line threshold.
- Group parameters into a request or options type when they form a stable concept or evolution point. A long signature alone is not sufficient reason.
- Use
switch when it clarifies one discriminant or a set of mutually exclusive cases; do not replace a short, natural if mechanically.
- Prefer explicit initialization when nil and empty have observably different semantics, such as a JSON contract. Otherwise preserve useful zero values.
- Use keyed fields for literals of exported or evolving structs. Positional literals can remain appropriate for small, local, intentionally tuple-like types.
- Pass values or pointers according to mutation, identity, optionality, copying cost, and existing API contracts. Benchmark copy concerns on hot paths rather than relying on size folklore.
Comments and organization
Comments should explain constraints, invariants, compatibility decisions, or why an apparently simpler approach is wrong. Remove comments that only narrate syntax. Preserve required exported documentation and generated-code conventions.
Within a file, keep related declarations close enough to navigate naturally. Avoid reorganizing an entire package during a focused change unless the current layout blocks the requested work.
Keep public surface area intentional. Do not export, unexport, rename, or move a symbol without checking callers, interface satisfaction, generated code, serialization contracts, and compatibility requirements.
Review standard
Report only changes that materially improve comprehension or reduce maintenance risk. Separate:
- correctness or behavioral risks;
- repository-enforced conventions;
- optional readability suggestions.
For each non-obvious recommendation, explain the concrete reading or maintenance problem it solves. Do not require explanatory comments solely because code chooses a reasonable alternative style.
When changes are requested, format and run the repository's existing focused checks. Keep formatter, linter, dependency, and project-wide policy changes separate from a focused style edit.
1---2name: golang-code-style3description: Write or review readable, idiomatic Go code with attention to control flow, declarations, comments, file organization, and API clarity. Use for focused style work or project coding conventions; use dedicated naming, lint, documentation, or architecture guidance when those are the main task.4license: MIT5---67# Go Code Style89Make code easier to understand without turning preferences into universal rules. Preserve behavior, public APIs, and repository conventions unless the user explicitly asks to change them.1011## Establish the local style1213Before editing:14151. Read repository instructions, `go.mod`, formatter and linter configuration, and nearby code.162. Identify whether the task is implementation, focused review, or project-wide policy design, and keep the change at that scale.173. Use `gofmt` as the formatting baseline. Use stricter formatters only when the project already configures them.184. Prefer established local conventions when several idiomatic forms are equally clear.1920## Clarity heuristics2122Apply these with judgment:2324- Handle exceptional paths early when that makes the main flow flatter. Keep an `else` when it expresses a real two-way decision more clearly.25- Give intermediate values names when they reveal domain meaning or make a condition auditable. Do not extract trivial expressions merely to satisfy an operand count.26- Keep a value's scope narrow, including `if` initializers when the value is used only by that branch.27- Split functions when the extracted operation has a coherent responsibility, invariant, or test boundary—not because of an arbitrary line threshold.28- Group parameters into a request or options type when they form a stable concept or evolution point. A long signature alone is not sufficient reason.29- Use `switch` when it clarifies one discriminant or a set of mutually exclusive cases; do not replace a short, natural `if` mechanically.30- Prefer explicit initialization when nil and empty have observably different semantics, such as a JSON contract. Otherwise preserve useful zero values.31- Use keyed fields for literals of exported or evolving structs. Positional literals can remain appropriate for small, local, intentionally tuple-like types.32- Pass values or pointers according to mutation, identity, optionality, copying cost, and existing API contracts. Benchmark copy concerns on hot paths rather than relying on size folklore.3334## Comments and organization3536Comments should explain constraints, invariants, compatibility decisions, or why an apparently simpler approach is wrong. Remove comments that only narrate syntax. Preserve required exported documentation and generated-code conventions.3738Within a file, keep related declarations close enough to navigate naturally. Avoid reorganizing an entire package during a focused change unless the current layout blocks the requested work.3940Keep public surface area intentional. Do not export, unexport, rename, or move a symbol without checking callers, interface satisfaction, generated code, serialization contracts, and compatibility requirements.4142## Review standard4344Report only changes that materially improve comprehension or reduce maintenance risk. Separate:4546- correctness or behavioral risks;47- repository-enforced conventions;48- optional readability suggestions.4950For each non-obvious recommendation, explain the concrete reading or maintenance problem it solves. Do not require explanatory comments solely because code chooses a reasonable alternative style.5152When changes are requested, format and run the repository's existing focused checks. Keep formatter, linter, dependency, and project-wide policy changes separate from a focused style edit.