Google Go Style Guide
Use this skill when you write or review Go code. Follow Google Go style so code
stays clear, simple, and easy to change.
Official overview: https://google.github.io/styleguide/go/index
Full link list: references.md
Assume knowledge of Effective Go.
1. Document roles
| Document |
Role |
Use when |
| Style Guide |
Normative and canonical. Foundation. |
All Go code. See guide.md. |
| Style Decisions |
Normative, not canonical. Detailed decisions. |
Naming, commentary, imports, errors, language, tests. See style-decisions.md. |
| Best Practices |
Not normative or canonical. Common patterns. |
Call-site naming, errors, docs, tests, APIs, global state. See best-practices.md. |
If Style Decisions conflict with the Style Guide, follow guide.md.
2. Definitions
- Canonical: Enduring rules. All code should follow them. They change rarely.
- Normative: Agreed style for reviews. Terms stay consistent. Rules may change.
- Idiomatic: Common Go patterns. Prefer idiomatic forms when they do the same job.
3. Style principles (order of importance)
Use this order from the Style Guide. Consistency does not override the other
principles. Use Consistency to break ties.
- Clarity — The reader sees purpose and reason.
- Simplicity — Use the simplest code that meets goals.
- Concision — Keep a high signal-to-noise ratio.
- Maintainability — Make correct change easy over time.
- Consistency — Match nearby and project code when principles do not decide.
4. Core guidelines (canonical)
- Formatting: Match
gofmt output. Format generated code too.
- MixedCaps: Use MixedCaps for exported names. Use mixedCaps for
unexported names. Do not use snake_case. Locals follow unexported rules.
- Line length: No fixed limit. Refactor before you wrap. Do not split a
line before an indentation change. Do not wrap a long string only to
shorten a line.
- Naming: Keep names short and clear. Use context. Do not repeat the obvious.
See style-decisions.md and
best-practices.md.
- Local consistency: If the guide is silent, match nearby code in the same
file or package. Do not spread existing deviations. Fix them or avoid them.
5. What these documents do
These documents:
- Set principles for style choices
- Record settled Go style
- Show canonical examples and idioms
- Explain style decisions
- Reduce surprise in reviews
- Keep reviewer terms consistent
These documents do not:
- Replace judgment
- List every review comment
- Justify large style-only rewrites
Write new code to current guidance. Fix nearby issues over time.
6. Application workflow
When you write or edit .go files:
- Apply this overview (principles and core guidelines).
- Apply guide.md for principles and core guidelines in full.
- Apply style-decisions.md for naming, commentary,
imports, errors, language, libraries, and tests.
- Apply best-practices.md for call-site naming, error
context, docs, variables, function args, tests, strings, and global state.
- Open references.md for official links.
7. Quick checklist
8. Support files
- Style Guide: guide.md
- Style Decisions: style-decisions.md
- Best Practices: best-practices.md
- Official links: references.md
1---2name: go-google-style-guide3description: Apply Google Go style principles, naming, formatting, errors, tests, and API patterns. Use when writing or reviewing Go code, formatting with gofmt, choosing names, structuring imports, handling errors, writing godoc, designing function options, or shaping table-driven tests.4---56# Google Go Style Guide78Use this skill when you write or review Go code. Follow Google Go style so code9stays clear, simple, and easy to change.1011Official overview: https://google.github.io/styleguide/go/index 12Full link list: [references.md](references.md)1314Assume knowledge of [Effective Go](https://go.dev/doc/effective_go).1516---1718## 1. Document roles1920| Document | Role | Use when |21| -------- | ---- | -------- |22| **Style Guide** | Normative and canonical. Foundation. | All Go code. See [guide.md](guide.md). |23| **Style Decisions** | Normative, not canonical. Detailed decisions. | Naming, commentary, imports, errors, language, tests. See [style-decisions.md](style-decisions.md). |24| **Best Practices** | Not normative or canonical. Common patterns. | Call-site naming, errors, docs, tests, APIs, global state. See [best-practices.md](best-practices.md). |2526If Style Decisions conflict with the Style Guide, follow [guide.md](guide.md).2728---2930## 2. Definitions3132- **Canonical**: Enduring rules. All code should follow them. They change rarely.33- **Normative**: Agreed style for reviews. Terms stay consistent. Rules may change.34- **Idiomatic**: Common Go patterns. Prefer idiomatic forms when they do the same job.3536---3738## 3. Style principles (order of importance)3940Use this order from the Style Guide. Consistency does **not** override the other41principles. Use Consistency to break ties.42431. **Clarity** — The reader sees purpose and reason.442. **Simplicity** — Use the simplest code that meets goals.453. **Concision** — Keep a high signal-to-noise ratio.464. **Maintainability** — Make correct change easy over time.475. **Consistency** — Match nearby and project code when principles do not decide.4849---5051## 4. Core guidelines (canonical)5253- **Formatting**: Match **`gofmt`** output. Format generated code too.54- **MixedCaps**: Use **MixedCaps** for exported names. Use **mixedCaps** for55 unexported names. Do not use snake_case. Locals follow unexported rules.56- **Line length**: No fixed limit. Refactor before you wrap. Do **not** split a57 line before an indentation change. Do **not** wrap a long string only to58 shorten a line.59- **Naming**: Keep names short and clear. Use context. Do not repeat the obvious.60 See [style-decisions.md](style-decisions.md) and61 [best-practices.md](best-practices.md).62- **Local consistency**: If the guide is silent, match nearby code in the same63 file or package. Do not spread existing deviations. Fix them or avoid them.6465---6667## 5. What these documents do6869These documents:7071- Set principles for style choices72- Record settled Go style73- Show canonical examples and idioms74- Explain style decisions75- Reduce surprise in reviews76- Keep reviewer terms consistent7778These documents do **not**:7980- Replace judgment81- List every review comment82- Justify large style-only rewrites8384Write **new** code to current guidance. Fix nearby issues over time.8586---8788## 6. Application workflow8990When you write or edit `.go` files:91921. Apply this overview (principles and core guidelines).932. Apply [guide.md](guide.md) for principles and core guidelines in full.943. Apply [style-decisions.md](style-decisions.md) for naming, commentary,95 imports, errors, language, libraries, and tests.964. Apply [best-practices.md](best-practices.md) for call-site naming, error97 context, docs, variables, function args, tests, strings, and global state.985. Open [references.md](references.md) for official links.99100---101102## 7. Quick checklist103104- [ ] Does `gofmt` accept the file?105- [ ] Do names use MixedCaps and avoid needless repetition?106- [ ] Is purpose clear to the reader (what and why)?107- [ ] Is the simplest mechanism enough (language, then stdlib, then project libs)?108- [ ] Are errors handled, returned, or clearly ignored?109- [ ] Is `context.Context` the first parameter where needed, and not stored in structs?110- [ ] Are interfaces owned by the consumer when used for substitution?111- [ ] Do tests use `testing` (and `cmp` if needed) without assertion libraries?112- [ ] Do failure messages identify inputs and show got before want?113- [ ] Are package-level globals avoided for client behavior?114115---116117## 8. Support files118119- **Style Guide:** [guide.md](guide.md)120- **Style Decisions:** [style-decisions.md](style-decisions.md)121- **Best Practices:** [best-practices.md](best-practices.md)122- **Official links:** [references.md](references.md)